Open GitWasAMistakeItsNothingButTrash opened 1 year ago
Well, that's a fun one! The 7 causes the records to be printed, right? How did you discover that?
I think the 7 is interpreted as a boolean expression, which evaluates to true. So it is a pattern, just like NR == 1
in your example. It seems that there is an implicit {print}
after the pattern, which causes the records to be printed. If you replace 7
with {print}
, the result will be the same. And if you change 7
to NR % 2 == 1
, only oddly-numbered records are printed. Why can you leave out the {print}
then? I have no idea.
I wonder why they chose "7" for that, it strikes me as obfuscating the code more than anything else.
I found it on Stack Exchange, after my instinctive expression 'NR == 1 { print "hello world"; }'
didn't work.
Hmm, interesting. The author of the question uses the same trick, but with 1 instead of 7:
How to add lines to the beginning of the file? I tried so
for i in $(ls); do awk 'NR==1{print "first_line" }'1 "$i" > "$i"; done
(The 1
is outside the quotes, but that does not make a difference here. Try echo 'high'5
to see that Bash merges it into a single argument.)
I also found someone describing this trick as a code golf technique here:
Remember that the default action for a matching pattern is
print $0
, so if you get what you want printed into$0
, just use1
Fascinating. So is there any difference between the print commands 1
and 7
? And if not, can any number be used?
How to parse
{$0="hello world"RS$0}7
, as inawk 'NR == 1 {$0="hello world"RS$0}7' input > output
? In particular, what does the "7" do?