Parsing messages with grep and awk
When handlind hundreds of log files for inspection, knowing how to use grep to parse and find the relevant errors saves a lot of time. I usually use grep for that, and in this note, I've saved some awk notes.
GREP
-
Returns all the directories that "message" appears.
-
Grep by time and "message".
AWK
'{}' = Action Block
NR = Number of Records (built-in variable) ---> Used to specify the line number of a set of text
Examples
- Assign variable to awk capture of word
- FOR loop to interact with 2 columns of values as variables
- Arrays + awk
- Filtering by field
$ awk -F "|" '$5 < 4000 ' file.txt OR $ awk -F "|" -v low_salary="4000" '$5 < low_salary ' file.txt $ awk -F "|" -v low_salary="4000" '$5 < low_salary { print $4 } ' file.txt OR awk -F "|" -v low_salary="4000" -v high_salary=4500 -v header="------my header-------" 'BEGIN { print header } $5 >= high_salary || $5 <= low_salary { print $2, $5}' pipe_example.txt 08|garca|branca|[email protected]|1000 09|micael|o gato|[email protected]|2000 /// file.txt 06|amazonia|mosquiteira|[email protected]|4500 07|jacare|pantanoso|[email protected]|4000 08|garca|branca|[email protected]|1000 09|micael|o gato|[email protected]|2000