Get results from grep in the order they appear?

6,676

Solution 1

grep SESSNUM=4437 *

Grep normally returns thing in order. Are you sure you are getting the results from the correct log file? Does it work correctly if you specify the logfile you want instead of just *?

Correction! There should only be one file in the directory that contains the session number, but I don't necessarily know which one when I'm grepping.

I would suggest you use the -H option of grep, so you can be sure you are getting the content from only one file. This should be the default behavior, but what you posted didn't include the filenames, unless you stripped those out.

   -H, --with-filename
          Print the file name for each match.  This is  the  default  when
          there is more than one file to search.

Solution 2

Is it possible that grep is aliased to something that messes with the order?

However, I think it's more likely that you have multiple files in the directory.

Share:
6,676

Related videos on Youtube

IVR Avenger
Author by

IVR Avenger

Updated on September 17, 2022

Comments

  • IVR Avenger
    IVR Avenger almost 2 years

    I'm trying to grep a log file to only show lines that match a certain session ID. Thus far, it works great. However, when I get the results of my grep command, I'm not getting the entries in the order they appear.

    If the log file in the directory has this data:

    SESSNUM=4437 login.jsp
    SESSNUM=4437 welcome.jsp
    SESSNUM=4437 info.jsp
    SESSNUM=4437 logout.jsp
    

    And I enter this:

    grep SESSNUM=4437 * 
    

    I get this information:

    SESSNUM=4437 logout.jsp
    SESSNUM=4437 welcome.jsp
    SESSNUM=4437 login.jsp
    SESSNUM=4437 info.jsp
    

    Is there a way to make grep display matching lines in the order they appear in the log file(s)?

    Thanks!
    IVR Avenger

    • Randomclik
      Randomclik almost 15 years
      If there's more than one file in that directory, your grep output would have filenames in front of each line on every UNIX I've used. Are you sure you're in the right place? What UNIX is this?
    • Denilson Sá Maia
      Denilson Sá Maia almost 15 years
      I don't understand. grep always return the matching lines in the order that they are in the file. Also, I didn't understand if you were grepping one file or multiple files.
    • IVR Avenger
      IVR Avenger almost 15 years
      Correction! There should only be one file in the directory that contains the session number, but I don't necessarily know which one when I'm grepping. Hence the grep for *. I'm using a bash shell on RHEL ES4. It's possible that I'm doing something else that's hokey; I've been away from Unix for a while!
    • James Sneeringer
      James Sneeringer almost 15 years
      GNU grep normally prefixes the output with the filename if more than one are specified on the command line. However, you can force it to always show the filename with "grep -H" (or never with "grep -h"). As others have noted, grep by itself will show matching lines in the order the appear inside the file. I would guess you're either grepping multiple files, or a single file that is changing in between your "cat" and "grep" commands.