When doing the project today, we encountered a problem, that is, how to use the pipeline under the Linux command. We will certainly use the simple pipeline, just like this form
<span style="font-size:18px;">ls -a | grep *test</span>
Today’s problem is a bit complicated. It is to scan the system log in real time, from the last line after scanning to the end of the file, and match keywords. If they match, an alarm will be given. I use awk with grep command to achieve
Awk command is used to select log segment output as the input of grep command
awk 'NR>= beginLineNum&& NR<endLineNum logFileName | grep -nE pattern ;
But how to get the total row number of the current log becomes a problem. The command to get the total row number is
wc -l logFileName
But the result after WC is to be placed in the position of Ender linenum, which can not be realized by | method. After query, it is found that it can be realized by using the ‘` symbol (` symbol is the key under the ESC key on the keyboard)
So the way to do it is
awk 'NR>= beginLineNum&& NR<`wc -l logfileName` logFileName | grep -nE pattern ;
When executing commands in Linux, the commands in ‘will be executed first, and then other commands will be executed
“As standard input, the return value of the command inside is input into the command outside.