我試圖在日誌文件中刪除一些特殊行,所以我在嵌入式linux系統上使用了busybox的sed。sed命令在shell終端下工作正常,但在C代碼下調用'system()'時失敗
# sed
BusyBox v1.18.4 (2013-01-16 16:00:18 CST) multi-call binary.
Usage: sed [-efinr] SED_CMD [FILE]...
Options:
-e CMD Add CMD to sed commands to be executed
-f FILE Add FILE contents to sed commands to be executed
-i Edit files in-place (else sends result to stdout)
-n Suppress automatic printing of pattern space
-r Use extended regex syntax
If no -e or -f, the first non-option argument is the sed command string.
Remaining arguments are input files (stdin if none).
下殼執行以下命令,一切工作正常:
export MODULE=sshd sed "/$MODULE\[/d" logfile
,但如果我嘗試使用下面的C代碼來實現:
char logfile[] = "logfile"; char module_str[] = "sshd"; char env_str[64] = {0}; int offset = 0; strcpy(env_str, "MODULE="); offset += strlen("MODULE="); strcpy(env_str + offset, module_str); putenv(env_str); system("sed \"/$MODULE\[/d\" logfile");
執行a.out時,我得到錯誤消息:
sed: unmatched '/'
我的'system()'調用有什麼問題?我完全是文本處理中的新手,所以任何人都可以給我一些線索?謝謝。
最好的問候, dejunl
你需要使用'strcat'還是'+'而不是$ MODULE? – PeppyHeppy
我想在sed命令中使用env變量,使用'strcat'可以解決這個問題嗎? – dejunl
你爲什麼要將你的sed命令包裝在C程序中? –