开发者

Multiple greps in pipeline not terminating after completion

I'm seem to be having a problem with a simple grep statement not finishing/terminating after it's been completed.

For example:

 grep -v -E 'syslogd [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: restart' |
 grep -v 'printStats: total reads from cache:' /var/log/customlog.log >\
 /tmp/filtered_log.tmp

开发者_JS百科The above statement will strip out the contents and save them into a temp file, however after the grep finishes processing the entire file, the shell script hangs and cannot proceed anymore. This behavior is also triggered when manually running the command within the command line. Essentially combining multiple grep statements causes a PAGER like action (more/less).

Does anyone have any suggestions to overcome this limitation? Ideally I wouldn't want to do the following giving that the customlog.log file might get huge at times.

cat /var/log/customlog.log |
grep -v -E 'syslogd [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: restart' |
grep -v 'printStats: total reads from cache:' > /tmp/filtered_log.tmp

Thanks,

Tony


As explained above, you need to move here your file name:

grep -v -E \
    'syslogd [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: restart' /var/log/customlog.log
| grep -v 'printStats: total reads from cache:' > /tmp/filtered_log.tmp

But you can also combine the two greps:

grep -v -E \
    -e 'syslogd [0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}: restart' \
    -e  'printStats: total reads from cache:' /var/log/customlog.log > \
    /tmp/filtered_log.tmp

Saves a bit of CPU and will fix your error at the same time.

BTW, another possible issue: What if two instances of this script are run at the same time? Both will be using the same temp file. This probably isn't an issue in this particular case, but you might as well get used to developing scripts for that situation. I recommend that you use $$ to put the process ID in your temporary file:

tempFileName="/tmp/filtered_log.$$.tmp"
grep -v -E -e [blah... blah.. blah] /var/log/customlog.log > $tempFileName

Now, if two different people are running this process, you won't get them using the same temp file.


Appended

As pointed out by uwe-kleine-konig, you're actually better off using mktemp:

tempFileName=$(mktemp filtered_log.XXXXX)
grep -v -E -e [blah... blah.. blah] /var/log/customlog.log > $tempFileName

Thanks for the suggestion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜