We can print date string using shell command date with format flags.
date +%y-%m-%d
for example will print today's date in short format as
20-05-13
sometimes, the generate date strings can be used to construct other shell commands with ``.
The date command wrapping inside `` will get executed first, the result string then concatenated with the rest of the strings to form a shell command.
For example, when the following command is executed
echo filename.`date +%y-%m-%d`.log
`date +%y-%m-%d` will execute first, generate a string 20-05-13. Then string 20-05-13 is concatenated with rest of the command to get a new shell command:
echo filename.20-05-13.log
the above command's running result is
filename.20-05-13.log
demo>date
Wed May 13 15:05:10 EDT 2020
demo>date +%y-%m-%d
20-05-13
demo>echo filename.`date +%y-%m-%d`.log
filename.20-05-13.log
==================
Similarly, we can use the same technique to construct more sophisticated grep shell command:
ls myapp.`date +%y-%m-%d`*.log | xargs -I {} cat{} | grep -i 'exception\|error' -A 2 -B 2
the above command, list all myapp log files generated today. Among though files, we search for exception and error messages, print out the line with 2 lines before and 2 lines after.
No comments:
Post a Comment