Results 1 to 7 of 7
Hi,
I try to grep a file (syslog file) and display the result using a loop. Unfortunatly, there is an issue with the code below because of the libefeed/carriage return.
...
- 06-13-2008 #1Just Joined!
- Join Date
- Jun 2008
- Posts
- 2
Grep result in a loop and linefeed issue
Hi,
I try to grep a file (syslog file) and display the result using a loop. Unfortunatly, there is an issue with the code below because of the libefeed/carriage return.
I try to find a way to have each grep result into a single line so I can use it into a the loop:
Right now, each 'res' results is a single word from the grep result... I can't find a way to catch the end of line and put the the line into the 'res' variable.Code:for res in $(grep 'pam_unix' $LOG_DIR/$server--$YEAR-$MONTH_D-$DAY_D_ZERO.log | grep 'session opened' | grep -v 'closed'); d echo "res : $res" done
Any help will be greatly appreciated!
Thanks
- 06-13-2008 #2
hi,
i think this not a grep problem but rather a for loop problem. for considers the white space as separator. so it takes one word after the other and puts it in res. using while and read should solve the problemLinux and me it's a love story
- 06-13-2008 #3Just Joined!
- Join Date
- Jun 2008
- Posts
- 2
Yes! it works! :
doneCode:grep 'pam_unix' $LOG_DIR/$server--$YEAR-$MONTH_D-$DAY_D_ZERO.log | grep 'session opened' | grep -v 'closed' | while read res ; do echo "res : $res"
- 06-13-2008 #4
good news !
Linux and me it's a love story
- 06-14-2008 #5Linux User
- Join Date
- Aug 2006
- Posts
- 458
grep pipes to standard output by default. Therefore, there's really no need for a while loop to specifically echo the results out. (unless there are further processing on the lines itself)
Secondly, too many unnecessary greps. Unix enables you to use pipes, which is good, but too much of it waste processor times.
Code:awk '/pam_unix/ && /session opened/ && !/closed/' $LOG_DIR/$server--$YEAR-$MONTH_D-$DAY_D_ZERO.log
- 06-14-2008 #6Linux and me it's a love story
- 06-14-2008 #7Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044


Reply With Quote
