Why does
echo foo bar..baz bork | awk 'BEGIN{RS=".."} {gsub(OFS,"\t");}1'
seem to do the same thing as
echo foo bar..baz bork | awk 'BEGIN{RS=".."} {gsub(OFS,"\t");} {print;}'
?
In fact any number that isn't zero (including decimals and negatives) will do the same thing. However, leaving off the digit, using a text character, or using zero prints nothing. I didn't see this documented anywhere, although I could have missed something.
curly brackets Definitions and Synonyms noun plural informal. DEFINITIONS1. the symbols { }, used especially in mathematics and computer programs for showing that things written between them should be considered together. Synonyms and related words.
Parameter expansion. Here the braces {} are not being used as apart of a sequence builder, but as a way of generating parameter expansion. Parameter expansion involves what it says on the box: it takes the variable or expression within the braces and expands it to whatever it represents.
In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true.
To match literal curly braces, you have to escape them with \ . However, Apex Code uses \ as an escape, too, so you have to "escape the escape". You'll need to do this almost every time you want to use any sort of special characters in your regexp literally, which will happen more frequently than not.
If you remember, awk
is a language which has a series of <pattern> <action>
operations. Each pattern is evaluated for each line (at least conceptually), and when the pattern matches, the action is executed. Either the pattern or the action can be omitted. An omitted pattern matches every line; an omitted action defaults to {print $0}
(aka {print}
). The 'pattern' might be a simple regex match, or some other more complicated and general condition, which must evaluate to true if the action is to be executed (as noted by Ed Morton in his comment).
In your example, the 1
is a pattern; it evaluates to true. The action is not specified, so the default action is invoked, which is {print}
or {print $0}
. Any value other than zero or an empty string evaluates to true and will invoke the print. (Note that if you mention an uninitialized variable (for example, c
), then it is autocreated and set to zero and therefore evaluates to false. Hence awk 'c' <<<"Hi"
prints nothing.)
The actions associated with the BEGIN and END patterns are handled specially, of course.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With