2010-01-01 85 views
0

第一個命令不按預期方式工作,但不是第二個的。 我想在開頭或結尾添加一些文字。一些文本添加到一堆線

# grep `date +'%y%m%d'` /var/log/mysqld.log 
100101 10:56:00 mysqld started 
100101 10:56:02 InnoDB: Started; log sequence number 1 2052750649 

# sed 's/^/computer /g' < grep `date +'%y%m%d'` /var/log/mysqld.log 
bash: grep: No such file or directory 

# expected output 
computer 100101 10:56:00 mysqld started 
computer 100101 10:56:02 InnoDB: Started; log sequence number 1 2052750649 

回答

6

當你與輸入重定向寫的,它會尋找一個叫做grep在當前目錄下的文件,並試圖閱讀其內容,不執行它。您需要使用管道來代替:

grep `date +'%y%m%d'` /var/log/mysqld.log | sed 's/^/computer /' 

我也去掉了g修飾符從您的sed,因爲它是完全不必要的。

0

只是一個awk命令

awk -vd=$(date +'%y%m%d') '$0~d{ print "computer "$0 }' /var/log/mysqld.log