Is it possible to change the order of a glob?

I am trying to show all instances of a particular message from the syslog in chronological order by doing something like the following:

grep squiggle /var/log/messages*

Unfortunately the glob pattern matches the currently active file first. eg.

/var/log/messages
/var/log/messages-20120220
/var/log/messages-20120227
/var/log/messages-20120305
/var/log/messages-20120312

This means that recent messages show up first followed by the historical messages in chronological order.

Is it possible to adjust the glob pattern behaviour somehow to make the empty match (ie. just messages) show up at the end of the list?

If not, what would be a good way to address this problem?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In zsh, you can control the order of matches (among other things) with a glob qualifier.

echo /var/log/messages*        # usual lexicographic order
echo /var/log/messages*(On)    # reverse lexicographic order
echo /var/log/messages*(om)    # reverse chronological order (ascending mtime)
echo /var/log/messages*(Om)    # chronological order order (descending mtime)

(See the manual for more possibilities.) You can even define your own sorting order by supplying a comparison function in recent versions, with oe or o+.

Here, the proper order of files is chronological order. You can emulate it easily based on the name, though, and that works even in bash:

grep squiggle /var/log/messages{-*,}

Method 2

I don’t know of a way to change the globbing order, but there’s an easy workaround for your case:

grep squiggle /var/log/messages-* /var/log/messages

i.e. don’t match the messages files in your glob pattern, and add it to the end of grep‘s argument list.

Method 3

You can use backticks combined with ls -tr (sort by mod time and in reverse) like this:

grep squiggle `ls -tr /var/log/messages*`


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x