awk variable assignment-- inside braces or not?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk variable assignment-- inside braces or not?
# 1  
Old 08-24-2011
awk variable assignment-- inside braces or not?

So, in awk, I've always put my variable assignments inside of the curly braces, just like dad, and grandpa, and the 26 generations before them. But today I came upon an awk statement that had them outside the braces-- blasphemy!

Seriously, though, is there any practical difference? I was surprised to see that the following trivial commands worked identically:

Code:
ls | awk 'x = $0 {print x}'
ls | awk '{x = $0 ; print x}'

I'm showing my ignorance here, but if I'd been asked whether those would work, I would have predicted that the first would fail and the second would work; but, they both work.

I'm simply wondering if there's a difference that this example doesn't bring to light-- some slightly more subtle gotcha, perhaps? Or are they functionally and logically identical?
# 2  
Old 08-24-2011
They are not identical.

Code:
$printf '%s\n' 0 1 '' 'not empty' | awk '{x = $0; print x}
0
1

not empty
$printf '%s\n' 0 1 '' 'not empty' | awk 'x = $0 {print x}
1
not empty

In AWK, the value of an assignment statement is the value assigned. The later version is using the value of $0 to determine if the action will be executed. In the former, the action is always performed.

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 3  
Old 08-24-2011
There is a difference:
Code:
% touch 0 1 2 3

% ls | awk 'x = $0 {print x}'
1
2
3

% ls | awk '{x = $0 ; print x}'
0
1
2
3

Assignment is an expression in awk. These two commands are the same only when $0 is true - not 0 or "". In this case x=$0 gives the true value and the action part is executed.

Last edited by yazu; 08-24-2011 at 02:32 PM.. Reason: changed $1 to $0
This User Gave Thanks to yazu For This Post:
# 4  
Old 08-24-2011
Ah, thank you, both. I'd never considered that. So, when outside, the operation is simultaneously making an assignment and establishing a true/false condition. Slight variations of the example given...

Code:
seq -2 2 | awk 'x = $0 {print x}'

   and...

( seq -3 -1 ; echo "" ; seq 0 3 ) | awk 'x = $0 {print x}'

Again, thanks. It's a new little bit I'd never encountered before.

Last edited by treesloth; 08-24-2011 at 02:50 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Optimize multiple awk variable assignment

how can i optimize the following: TOTALRESULT="total1=4 total2=9 total3=89 TMEMORY=1999" TOTAL1=$(echo "${TOTALRESULT}" | egrep "total1=" | awk -F"=" '{print $NF}') TOTAL2=$(echo "${TOTALRESULT}" | egrep "total2=" | awk -F"=" '{print $NF}') TOTAL3=$(echo... (4 Replies)
Discussion started by: SkySmart
4 Replies

2. Shell Programming and Scripting

[awk] printing value of a variable assignment from a file

Heyas Me try to print only the value of a (specific) variable assignment from a file. What i get (1): :) tui $ bin/tui-conf-get ~/.tui_rc TUI_THEME dot-blue "" "$TUI_DIR_INSTALL_ROOT/usr" "$TUI_DIR_INSTALL_ROOT/etc/tui" "$TUI_PREFIX/share/doc/tui" "$TUI_PREFIX/share/tui"... (2 Replies)
Discussion started by: sea
2 Replies

3. Shell Programming and Scripting

Variable assignment inside awk

Hi, Was hoping someone could help with the following: while read line; do pntadm -P $line | awk '{if (( $2 == 00 && $1 != 00 ) || ( $2 == 04 )) print $3,$5}'; done < /tmp/subnet_list Anyone know if it is possible to assign $3 and $5 to separate variables within the {} brackets? Thanks... (14 Replies)
Discussion started by: CiCa
14 Replies

4. Shell Programming and Scripting

Passing awk variable argument to a script which is being called inside awk

consider the script below sh /opt/hqe/hqapi1-client-5.0.0/bin/hqapi.sh alert list --host=localhost --port=7443 --user=hqadmin --password=hqadmin --secure=true >/tmp/alerts.xml awk -F'' '{for(i=1;i<=NF;i++){ if($i=="Alert id") { if(id!="") if(dt!=""){ cmd="sh someScript.sh... (2 Replies)
Discussion started by: vivek d r
2 Replies

5. Shell Programming and Scripting

HELP with AWK one-liner. Need to employ an If condition inside AWK to check for array variable ?

Hello experts, I'm stuck with this script for three days now. Here's what i need. I need to split a large delimited (,) file into 2 files based on the value present in the last field. Samp: Something.csv bca,adc,asdf,123,12C bca,adc,asdf,123,13C def,adc,asdf,123,12A I need this split... (6 Replies)
Discussion started by: shell_boy23
6 Replies

6. Shell Programming and Scripting

AWK Variable assignment issue Ksh script

Hi There, I am writing a ksh script which assigns variable values from file "A" and passes that variables to file "B". While passing the parameters an additional "$" sign is being assigned to awk -v option. Could any one help me with this please. #!/bin/ksh head -1... (3 Replies)
Discussion started by: Jeevanm
3 Replies

7. Shell Programming and Scripting

Automatic variable assignment inside a for loop

cs1=`echo "scale=8;($css1/$css0)*100"|bc` cs2=`echo "scale=8;($css2/$css0)*100"|bc` cs3=`echo "scale=8;($css3/$css0)*100"|bc` cs4=`echo "scale=8;($css4/$css0)*100"|bc` cs5=`echo "scale=8;($css5/$css0)*100"|bc` cs6=`echo "scale=8;($css6/$css0)*100"|bc` cs7=`echo "scale=8;($css7/$css0)*100"|bc`... (3 Replies)
Discussion started by: thulasidharan2k
3 Replies

8. Shell Programming and Scripting

Using variable inside awk

I am trying to print the lines with pattern and my pattern is set to a variable express awk '/$express/{where=NR;print}' test2.log I am not getting any data even though i have the data with the pattern. Can seomeone correct me with the awk command above? (20 Replies)
Discussion started by: rdhanek
20 Replies

9. Shell Programming and Scripting

variable assignment using awk

Guys, Could you please help me out. I need two values in two variables using awk from the o/p of grep. example:- grep sdosanjh <filename> sdosanjh myhostname myfilename NOW WHAT I WANT IS :- sdosanjh should be in variable (say NAME) myhostname should be in variable (say... (8 Replies)
Discussion started by: sdosanjh
8 Replies

10. Shell Programming and Scripting

awk variable assignment in a file

Hi All, I have a little awk script which uses a variable (x): awk -v x=0 'NF != 6 { ++x } END { print "This batch had " x " errors out of ", NR" records"}' But when I've tried to put the command in a file I can't seem to declare the variable. I've managed to simplify the code so that I... (4 Replies)
Discussion started by: pondlife
4 Replies
Login or Register to Ask a Question