I have the following file:
$ cat files
foo.txt
bar.js
I am looking to use bash awk to only print files not ending with .js
.
This works fine when using single quoted awk:
$ awk '$1 !~ /.js$/ {print $1}' files
foo.txt
However, when using double quoted awk:
$ awk "\$1 !~ /.js$/ {print \$1}" files
-bash: !~: event not found
awk -v foo=bar 'BEGIN{print foo}'
– Gilles Quénot Mar 28 '23 at 11:24awk
? It would be easier to usegrep -v '\.js$'
– Chris Davies Mar 28 '23 at 11:24awk
and single quotes work perfectly fine for me in WSL. – muru Mar 28 '23 at 11:52$
signs, or other double quotes inside the awk script (if you have string constants there). Disabling history expansion would fix the issue with!
. But if you can't do that either, for whatever reason, you could also rewrite the awk exprassion as! ($1 ~ /.../)
as!<space>
doesn't trigger history expansion. – ilkkachu Mar 28 '23 at 11:57!
and you've had to escape$
s. So we're left with what appears to be an XY Problem as you appear to be asking how to implement something you should never do rather than asking about how to do whatever it is you want to do and so it got downvoted and closed. – Ed Morton Mar 28 '23 at 13:20awk
warriors. If you are interested in how to improve yourawk
coding, I suggest to ask a new question for this. Anyhow, stay happy and move on. – Philippos Mar 28 '23 at 13:28