add some text to a bunch of lines

2019-08-24 05:07发布

The first command does work as expected but not the second one. I want to add some text either at the beginning or the end.

# 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

标签: sed
2条回答
SAY GOODBYE
2楼-- · 2019-08-24 05:46

As you wrote it with input redirection, it looks for a file called grep in the current directory and tries to read its contents, not execute it. You need to use a pipe instead:

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

I also removed the 'g' modifier from your sed as it is completely unnecessary.

查看更多
疯言疯语
3楼-- · 2019-08-24 05:50

Just one awk command

awk -vd=$(date +'%y%m%d') '$0~d{ print "computer "$0 }' /var/log/mysqld.log
查看更多
登录 后发表回答