When curly braces needed?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting When curly braces needed?
# 1  
Old 05-11-2016
When curly braces needed?

Hello, i was trying to find get a command to list duplicated files so i tried

Code:
ls dir1 dir2 | awk '{x[$0]++}'

and it didnt work.

After a bit of searching online i found that it works without the curly braces

Code:
ls dir1 dir2 | awk 'x[$0]++'

I thought the curly braces were needed in awk so dont understand why it only works without them.

Can anyone explain?

Thanks
# 2  
Old 05-11-2016
Curly braces in awk enclose the action part of the pattern {action} pair. As you don't supply any pattern, the result is FALSE and nothing is acted. Without the braces, it's a pattern which on second encounter is TRUE, so the default action (print $0) is executed.
# 3  
Old 05-11-2016
Quote:
Originally Posted by RudiC
Curly braces in awk enclose the action part of the pattern {action} pair. As you don't supply any pattern, the result is FALSE and nothing is acted. Without the braces, it's a pattern which on second encounter is TRUE, so the default action (print $0) is executed.
If you do not supply a pattern the DEFAULT pattern is TRUE (or match every line)
Code:
'{x[$0]++}'

The action is performed, however, that action is not a program that would display any of it.
You can see the result after awk has read every record by:
Code:
ls dir1 dir2 | awk '{x[$0]++}; END{ for (i in x){print i, " seen ", x[i], " time(s)"}}'

Code:
'x[$0]++'

without braquets is a pattern. Actually, x[$0] is the pattern, the ++ is to be added after the effect of evaluating if x[$0] contains a value. If there is a value, then, evaluates to TRUE and the default action, if not explicitly declared, would be to print $0.

To summarize the principle:

The invocation of AWK always means (whether, implicitly or explicitly) the following:
Code:
awk pattern {action}

You can omit one, either pattern or action, but you can not omit both.
If you omit the pattern, the action is performed on each line read.
If you omit the action, print $0 is the action for every time that the pattern evaluates no zero or no empty (FALSE)

Last edited by Aia; 05-11-2016 at 06:34 PM.. Reason: Correct mispelling
These 4 Users Gave Thanks to Aia For This Post:
# 4  
Old 05-11-2016
Aia, your explanation is correct, the {action} is performed but without printing!
Rather than the lazy
Code:
ls dir1 dir2 | awk 'x[$0]++'

I suggest
Code:
ls dir1 dir2 | awk '{if ($0 in x) print; else x[$0]}'

that shows that the action needs an explicit print (the print is short for print $0). Also it does not assign values to the x array (saves some memory); the lookup ($0 in x) only needs the keys not the values.
# 5  
Old 05-11-2016
And, of course, the single awk statement in:
Code:
ls dir1 dir2 | awk '{if ($0 in x) print; else x[$0]}'

can be split into two awk statements (one with a default action and one with a default pattern) producing the same output with less text:
Code:
ls dir1 dir2 | awk '$0 in x; {x[$0]}'

But, of course, if instead of printing each line that has appeared more than once you just want to print each line (once) that appears two or more times, you would need to keep a count, but the code is still short:
Code:
ls dir1 dir2 dir3 ...| awk 'x[$0]++ == 1'

# 6  
Old 05-11-2016
... should have thought before posting - I'm using the TRUE for missing pattern every day. Sorry for that.
# 7  
Old 05-12-2016
It's not that awk demands { } sometimes and doesn't demand them other times, it's that they mean different things.

awk's syntax can be difficult to grasp at first because there's a lot assumed. When you put a single expression on a line by itself without braces, i.e. X[$0]++ what awk understands that as, is
if(X[$0]++) then print_entire_line

When you use braces, you can put an expression optionally before and after: EXPR1 { STATEMENTS } EXPR2 which turns out to mean:
Code:
if(EXPR1)then STATEMENTS
if(EXPR2) then print_entire_line

EXPR1 is assumed to be always true when omitted, EXPR2 is assumed to be always false when omitted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Check string end with curly braces

file.txt apple apples{ applepicture apple9 apple cake{ abple apple_and_cake appleapple apple apple( and my script while read line; do if ]; then echo "$line" fi done <file.txt read (10 Replies)
Discussion started by: cmdcmd
10 Replies

2. Shell Programming and Scripting

Curly braces in sed

Hi, I have below command in one of the script. Can you please let me know what does the curly braces do over here \{1,\}. The remaining part of the code atleast I am able to understand. sed -n 's/.*\-\()\{1,\}\)\-.*/\1/p' (13 Replies)
Discussion started by: tostay2003
13 Replies

3. Shell Programming and Scripting

How to remove newline, tab, spaces in curly braces.. :( Pls Help?

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or more newlines between the curly braces. My desired output should be ... (6 Replies)
Discussion started by: NY_777
6 Replies

4. Shell Programming and Scripting

** EMERGENCY ** Having trouble with curly braces.. :( Pls Help

Hi Everyone, in the below "xyz (Exception e)" part... after the curly braces, there is a new line and immediately few tabs are present before closing curly brace. xyz (Exception e) { } note: there can be one or... (1 Reply)
Discussion started by: NY_777
1 Replies

5. UNIX for Dummies Questions & Answers

How do I pull the value between curly braces?

Hi everyone, I've got a file that looks like this: uid{508}pid{22224}pname{/PPROGRAM/pprgramx -profile:LIVE -serv:as ... I want to pull the value of pid between the curly braces, or 22224 in this example. pid is always the second pair of curly braces, but the length of the number is... (7 Replies)
Discussion started by: Scottie1954
7 Replies

6. Shell Programming and Scripting

tar --exclude with curly braces

I'm having trouble understanding the exclude option in tar. From some web sites, it seems one is able to exclude several strings by enclosing them in curly brackets. However it seems to be "random" what gets excluded when using the curlies. I've been using the exclude-from=myfile option in a... (12 Replies)
Discussion started by: majest
12 Replies

7. Shell Programming and Scripting

sed in windows does not parse curly braces

Hi everyone: I'm stuck at this point, could you guys please give me some hints about what I am doing wrong in the following script, I'm using sed for windows: sed ^"$ {^ a^ STRINGTABLE DISCARDABLE^ BEGIN^ #define CLIENT_MODULE, "%CLIENT_MODULE%"^ #define CLIENT_ID, "%CLIENT_ID%"^... (1 Reply)
Discussion started by: edgarvm
1 Replies

8. Shell Programming and Scripting

find -regex not recognizing curly braces

Must be a bug or something. Whether I escape them or not, it will not work. No matter what I set the minimum and maximum to nothing gets caught. For instance: find / -regex "/.{0, 50}.*" -maxdepth 1 or find / -regex "/.\{0, 50\}.*" -maxdepth 1 should pretty much catch everything residing within... (4 Replies)
Discussion started by: stevensw
4 Replies

9. Shell Programming and Scripting

Curly braces assigned to variables

Hi, Im pretty new to Unix. I came across a script which was using PLSQL inside a script and there was an unusual thing mentioned. there was a variable assigned as P_CUR=${1} and one more as V_TAGFILE="$1" Couldnt find the difference. Also the variables were used in PLSQL... (1 Reply)
Discussion started by: njks68
1 Replies

10. Shell Programming and Scripting

Use of curly braces with variables

Hi, I am new to shell scripting.I have worked somewhat with Perl though. I am not able to find what the second line does and how does it do. <code> FP_RUNNING=`service filepool status` FP_RUNNING=${FP_RUNNING%% *} <\code> After the first line,the variable FP_RUNNING stores '1 FilePool... (2 Replies)
Discussion started by: abhinavsinha
2 Replies
Login or Register to Ask a Question