Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does a number do after curly braces?

Tags:

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.

like image 802
shadowtalker Avatar asked Jul 08 '14 23:07

shadowtalker


People also ask

What do curly brackets in math mean?

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.

What are {} used for in bash?

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.

What do curly braces mean in C++?

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.

How do you match curly braces with regular expressions?

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.


1 Answers

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.

like image 187
Jonathan Leffler Avatar answered Sep 22 '22 09:09

Jonathan Leffler