Get results from grep in the order they appear

grepshellunix-shell

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

Best Answer

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