Jump to content

grep and command substitution


kde-head
 Share

Recommended Posts

You probably already know that if you want to grep your messages log for messages on a certain date, you would cd to /var/log and do

grep "Mar 17" messages

 

 

But say you wanted to automate this, maybe looking for specific message types everyday (which could be either mailed to you or outputted to a report)?

 

Enter command substitution:

 

grep "$(date +"%b %d")" /var/log/messages

 

the bit between the quotes executes the date command and returns the date in the format "Mar 17" say - this is then passed into the grep.

Link to comment
Share on other sites

A little addition to your tip. If you are not english speaker, so you have a different locale set, you probably won't see anything if you run the command:

 

grep "$(date +"%b %d")" /var/log/messages

 

because date will return the output in your locale format, while the logs are usually in POSIX format. To avoid this, just do:

 

grep "$(LC_ALL=POSIX date +"%b %d")" /var/log/messages

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...