Tags

, , , , , , , , , , ,

Print columns 1, 5 and then 2, put a colon, and then print column 10 of a line that matches pattern from the file columns.txt:

$ awk '/pattern/ {print $1, $5, $2, ": ", $10}' columns.txt

.
Protecting the special meaning of the single quotes and curly braces
I came across this situation when I was trying to watch the above command as well as two other commands (say, command1 and command2) at the same time. The correct way to do that is by properly protecting the meaning of the special characters in the above awk invocation:

$ watch -d -n 20 "command1; command2; awk '"'/pattern/ {print $1, $5, $2, ": ", $10}'"' columns.txt"

.
N.B. The “-d”flag highlights the changes, whereas the “-n 20” flag causes the above to watch every 20 seconds.

Just for the heck of it, let me put the full command that I was actually using:

$ watch -d -n 20 "ps aux | grep -i columbus | grep -v grep && echo; tail WORK/ciudgsm && echo && grep bond output.log | tail -13 && echo && awk '"'/state # 1/ {print $3,$4,$5,": ", $10}'"' output.log && echo; awk '"'/state # 2/ {print $3,$4,$5,": ", $10}'"' output.log && head curr_iter"

(suggestions for making the above shorter, other than by aliasing, are most welcome!).

Reference: here.

Advertisements