awk : Need Help in Understanding a command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk : Need Help in Understanding a command
# 1  
Old 09-11-2015
awk : Need Help in Understanding a command

Hello

I am working on a Change request and Stuck at a point. The below awk command is used in the function.

Code:
float_test ( ) {
  echo | awk 'END { exit ( !( '"$1"')); }'
}

I understand that awk 'END' is used to add one line at the end and exit is used to end the script with an error code but I am not able to understand the command as a whole.

Could someone please help me on this.

Thanks
Rahul
# 2  
Old 09-11-2015
So... I get the idea that "float_test" is passed an awk expression.

so what happens (I think this is poorly written)...

1. echo - programmer wanted to use awk's expression evaluator but didn't use /dev/null which is probably better (?)

2. awk doesn't add a line with END, it just performs something at the end of stream of data (in this case "echo", but again probably could have used /dev/null.

3. The "$1" comes from the shell, in the case the argument passed to float_test where is will become a piece of awk text to be processed.

Since you place inline awk in single quotes he's breaking out the single quotes to insert the shell variable "$1". This is how you did things a long time ago or you could say this is how you do things now to be portable across different vendor version OS levels of awk like things.

So it's called "float_test", but you could use this for just about anything. Might be interesting to see how float_test gets called in the script.

Try:
Code:
float_test a=1
echo $?

float_test whatever-really-just-type-whatever
echo $?

but the following also works

Code:
float_test not-a-number
echo $?

float_test 3.14
echo $?

Which is probably close to how it's used in context
This User Gave Thanks to cjcox For This Post:
# 3  
Old 09-14-2015
Thanks cjcox for your help.
# 4  
Old 09-15-2015
Assuming that the string passed in as the first positional parameter forms a valid arithmetic or logical expression, the float_test shell function returns exit status 0 if the expression evaluates to a non-zero value and returns exit status 1 if the expression evaluates to zero.

It could be written without the echo (and without redirection) if it used a BEGIN clause instead of an END clause:
Code:
float_test ( ) {
	awk 'BEGIN{exit(!('"$1"'))}'
}

If you're using a 1993 or later version of the Korn shell as your shell, you could evaluate similar expressions just using shell built-ins:
Code:
float_test ( ) {
	return $((!($1)))
}

But note that awk performs floating point arithmetic by default while ksh performs integer arithmetic by default. So, where float_test "1/3" using awk gives exit code 0 because 1/3 evaluates to 0.3333...; but when using ksh93 gives exit code 1 because 1/3 evaluates to 0. To reverse the behaviors, int(1/3) evaluates to 0 and exits with code 1 with awk and 1./3 evaluates to 0.3333... and exits with code 0 with ksh93.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 09-15-2015
Thanks Don for the indepth analysis..It really helped.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Understanding awk 'expansion'

Heyas Recently i wanted to help someone with an awk script, but the end-script didnt work as expected. He wanted, if HOME was empty, to get the HOME of the current USER from /etc/passwd. At first i tried hardcoded with root: awk -F: '/^root/ {print $6}' /etc/passwd As that worked, i've... (4 Replies)
Discussion started by: sea
4 Replies

2. UNIX for Dummies Questions & Answers

Understanding awk

I found this on an awk site and would like to know what it does: /CARS/{x="";next} {if(x)print x;x=$0} END{if(x)print x}' Does it mean if it finds the word cars it skips that line and then prints the next one? (4 Replies)
Discussion started by: newbie2010
4 Replies

3. Shell Programming and Scripting

Help in Understanding awk if ( F ) syntax -

Hi Experts, I was looking at the below link, for finding words next to it, And unable to understand this syntax: Can any one please explain , what is meaning of this code: if ( F ) s = s ? s OFS $i : $i from:... (4 Replies)
Discussion started by: rveri
4 Replies

4. Shell Programming and Scripting

Help in understanding awk expression

Hi, Could somebody help me in understanding the following awk expression: awk -v n="POINT" '/%/{print $0 "\n" n ;next}1' < file name Thanks, Arun (6 Replies)
Discussion started by: arun_maffy
6 Replies

5. UNIX for Dummies Questions & Answers

understanding sed command

Hi Friends, I need a small help in understanding the below sed command. $ cat t4.txt 1 root 1 58 0 888K 368K sleep 4:06 0.00% init 1 root 1 58 0 888K 368K sleep 4:06 0.00% init last $ sed 's/*$//' t4.txt 1 root 1 58 0 888K ... (3 Replies)
Discussion started by: forroughuse
3 Replies

6. UNIX for Dummies Questions & Answers

Understanding / Modifying AWK command

Hey all, So I have an AWK command here awk '{if(FNR==NR) {arr++;next} if($0 in arr) { arr--; if (arr == 0) delete arr;next}{print $0 >"list2output.csv"}} END {for(i in arr){print i >"list1output.csv"}}' list1 list2 (refer to image for a more readable format) This code was submitted... (1 Reply)
Discussion started by: Aussiemick
1 Replies

7. Solaris

Understanding 'du' command

Hi I have a questions related 2 commands : 'du' and 'ls'. Why is the difference between output of 'du' and 'ls' cmd's ? Command 'du' : ------------------ jakubn@server1 /home/jakubn $ du -s * 4 engine.ksh 1331 scripts 'du -s *' ---> shows block count size on disk (512 Bytes... (5 Replies)
Discussion started by: presul
5 Replies

8. UNIX for Advanced & Expert Users

understanding awk in this script

i am analyzing a query written by another developer ,need to understand part of script am looking at a code ..and it converts comma files to pipe delimited and also takes away quotes from any columns, source field format: 2510,"Debbie",NewYork changes to target: 2510|Debbie|NewYork ... (1 Reply)
Discussion started by: coolrock
1 Replies

9. Shell Programming and Scripting

understanding mv command

hi i was moving a file from one directory to another with the following cmmand mv /home/hsghh/dfd/parent/file.txt . while doing so i i accidently mv /home/hsghh/dfd/dfd . although i gave ctrl c and terminate the move command some of the file are missing in the parent directory and... (1 Reply)
Discussion started by: saravanan71184
1 Replies

10. Shell Programming and Scripting

Understanding Awk and Cat

Hi Guys, I was recently come across some code to hopefully learn a little bit about putting Shell commands into PHP application to run on a Linux server. However, I don't understand the command AT ALL... and was wondering if anyone can interpret it: cat userIDs.dat | awk '{s=s+1; if... (1 Reply)
Discussion started by: jordRiot
1 Replies
Login or Register to Ask a Question