aspose file tools*
The moose likes Linux / UNIX and the fly likes Calling awk script inside shell script Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "Calling awk script inside shell script" Watch "Calling awk script inside shell script" New topic
Author

Calling awk script inside shell script

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
I have the following command line working:

awk'{if ($2 != $6 ){print $0}}' test.txt | wc -l

Since I need to call awk script with bit of variations, I thought of a shell script with echo but the following script doesn't work.



I tried putting awk statement inside backquotes `len = awk'{if ($2 != $6 ){print $0}}' test.txt | wc -l`. But doesn't seem to work. May I know the reson? Thanks.
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11509
    
  95

Your backquotes should be around the expression you want evaluated, not around the assignment expression. So you should have:

The backquotes allow this to be read as "evaluate everything in the backquotes (the "awk'{if ($2 != $6 ){print $0}}' test.txt | wc -l" statement)" and then assign the output to len.

Some people find it easier to read if you use $() instead of backquotes, which would allow your code to become:

Technically speaking, you could write this without the wc command:
Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Thanks Andrew for the explanation.

Regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling awk script inside shell script