3

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

4
  • 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?
    – Morven
    Sep 18, 2009 at 18:18
  • 2
    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. Sep 18, 2009 at 18:21
  • 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! Sep 18, 2009 at 18:45
  • 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. Sep 18, 2009 at 19:26

3 Answers 3

7
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.
1
  • with * it works, great. Until now i always used . and the results wasn't ordered correctly.
    – LucScu
    Sep 19, 2023 at 8:08
1

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.

0

Chalk this one up to wrongheadedness. I was piping grep through an additional grep that I did not notice, and by combining everything into a single search, I was able to get the order I was looking for.

Thanks for the suggestions.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .