Find the answer to your Linux question:
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. ...
  1. #1
    Just 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:

    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
    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.

    Any help will be greatly appreciated!
    Thanks

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    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 problem
    Linux and me it's a love story

  3. #3
    Just Joined!
    Join Date
    Jun 2008
    Posts
    2
    Yes! it works! :

    Code:
    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"
    done

  4. #4
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    good news !
    Linux and me it's a love story

  5. #5
    Linux 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

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    Quote Originally Posted by ghostdog74 View Post
    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
    i dont think his/her objective is to output .i thought that s/he is gonna use the result for further processing, that is why i suggested while and read. if its just to display then of course you dont need all that.
    for the second part i agree with you.
    Linux and me it's a love story

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by ghostdog74 View Post
    Secondly, too many unnecessary greps. Unix enables you to use pipes, which is good, but too much of it waste processor times.
    I'd certainly agree with that, but against that you have to consider whether a few greps doing simple pattern matching is actually faster than a single (say awk) command doing a much more complicated one. And I've had at least one of those!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...