Watch with awk command
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
Related videos on Youtube
user1271772
Updated on September 18, 2022Comments
-
user1271772 over 1 year
How do you watch a command like?
awk 'NR%2==0 {printf "%s %8.0f", $1, $5}' filename.txt
Preceding this with "watch" gives this error:
awk: cmd. line:1: fatal: cannot open file `{printf' for reading (No such file or directory)
The answer to: Using the watch command with an argument that contains quotes was to escape the $ signs by replacing them with \$. But that gives me the error:
sh: -c: line 0: syntax error near unexpected token `(' sh: -c: line 0:
I wanted to ask this in the comment to that question but didn't have enough points to make comments.
Similar questions where the answers that worked for them did not work in this case:
1) https://askubuntu.com/questions/500217/how-to-properly-quote-piped-command-for-watch (answer was again to escape $ sign).
2), 3), and 4) are listed in the comments since I cannot post more than 2 links without 10 points reputation.-
user1271772 about 8 years
-
user1271772 about 8 years
-
user1271772 about 8 years
-
user1271772 about 8 yearsI could not even post more than 2 links in my question because need 10 point reputation for that. This is why I posted them as comments
-
Marc.2377 over 6 years
-
-
user1271772 over 3 yearsThe reason I didn't click "accept" is because none of these worked, but I found a solution which was to put the awk command in a script and then watch the script. I can write an answer about that.