Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling awk script inside shell script

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 11855
187
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Andrew for the explanation.

Regards
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic