Watch with awk command

6,201

There are four approaches.

1. Place the command in double-quotes

Both the double-quotes and the dollar-signs need to be escaped:

watch "awk 'NR%2==0 {printf \"%s %8.0f\", \$1, \$5}'" filename.txt

2. Place the command in single-quotes

Inside single-quotes, neither " nor $ need to be escaped. It is, however, not possible to include single-quotes within a single-quoted string. If we need a single-quote to appear, the work-around is to end the single-quoted string and add an escaped single-quote, \'. It looks like this:

watch 'awk '\''NR%2==0 {printf "%s %8.0f", $1, $5}'\' filename.txt

3. Hybrid approach

Combining both of the above, we put the first part of the string in double-quotes and the second part in single-quotes:

watch "awk 'NR%2==0 {printf "'"%s %8.0f", $1, $5}'\' filename.txt

4. Unquoted but fully escaped

If we don't enclose the command in quotes, we will need to escape all the needed shell-active characters. That includes spaces, quotes, curly-braces, and dollar-signs:

watch awk\ \'NR%2==0 \{printf\ \"%s\ %8.0f\",\ \$1,\ \$5\}\' filename.txt
Share:
6,201

Related videos on Youtube

user1271772
Author by

user1271772

Updated on September 18, 2022

Comments