LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-05-2018, 06:41 PM   #1
silver007
LQ Newbie
 
Registered: Oct 2007
Posts: 27

Rep: Reputation: 0
Get awk to work in bash script


Ok so I realize running awk within a bash script may not work because the shell itself tries to interpret the awk options. But I can't figure out how to get my command to work.

Code:
#!/bin/bash
ipscan -f:range 192.168.11.2 192.168.11.15 -o ips.txt -q
awk 'FNR > 7 { print $1, $2 }' ips.txt > ipstr.txt
awk 'length($0)>5 { print }' ipstr.txt > ipsfinal.txt
So I generate a list with ipscan, then modify it with two consecutive awk commands. ipscan works, neither awk executes.

Debian Jessie
 
Old 03-05-2018, 06:58 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,230

Rep: Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724Reputation: 724
Did you check if files meet awk condition? ie if ips.txt has more than 7 lines, if ipstr.txt contains lines with more than 5 chars...
 
Old 03-05-2018, 07:37 PM   #3
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.6
Posts: 3,805

Rep: Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280Reputation: 1280
edit: I should learn to read....never mind.
 
Old 03-05-2018, 10:04 PM   #4
X-LFS-2010
Member
 
Registered: Apr 2016
Posts: 423

Rep: Reputation: Disabled
bash is a commandline interpreter

you should see a prompt such as "#" for root or "$" for user (which you didn't show above)

$ echo "hi" | awk '{print}' ; echo "lo" | awk '{print}'

OR

$ echo "hi" | awk '{print}'
$ echo "lo" | awk '{print}'

OK - so maybe you saw a prompt and pressed enter (bash would tell you if the command didn't execute, awk would tell you if you had a syntax error. What your telling me is either or both of the .txt had no data. (you should know which, but you didn't say).

What that says is awk didn't find what you searched for.

'FNR > 7 {...}' is meaningless in awk as far as I know

awk's manpage says: "pattern {action}", it does not say "pattern | expression {action}"

$ cat foo | awk '(FNR>0){print $1}'
# apparently, to pass off an expressions instead of using pattern, you need parenthesis (evaluation of expression occurs in parenthesis may occur and decide outcome - in some languages when this happens is tricky)

$ cat foo | awk '{if(FNR > 0) {print $1}}'
# in this example no pattern (means all records), FNR is tested once per record, which is really the same as the above

Last edited by X-LFS-2010; 03-05-2018 at 10:07 PM.
 
Old 03-05-2018, 10:37 PM   #5
AwesomeMachine
LQ Guru
 
Registered: Jan 2005
Location: USA and Italy
Distribution: Debian testing/sid; OpenSuSE; Fedora; Mint
Posts: 5,513

Rep: Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004Reputation: 1004
You can put the awk part in an awk script and call it from the bash script.
 
Old 03-05-2018, 10:51 PM   #6
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 18,143

Rep: Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935Reputation: 2935
I wouldn't do it as posted, but it should work fine if data are provided correctly. awk honours (some) short-forms, so the FNR test is ok.
 
Old 03-05-2018, 11:31 PM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=14, FreeBSD_12{.0|.1}
Posts: 5,223
Blog Entries: 11

Rep: Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173Reputation: 3173
@silver007:

In order for anyone to see what may be happening you need to supply a few example lines from each file, and tell us what you expect the output of awk to be in each case.

If your data in ips.txt matches the awk condition, then it should write to ipstr.txt according to the awk expression, similarly for the second awk statement. If it is not doing what you want then it is likely that the data are not what you expect, or your awk expressions do not match them... either way, we need to see some example data and know what you expect the result to be.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] call awk from bash script behaves differently to awk from CLI = missing newlines titanium_geek Programming 4 05-26-2011 09:06 PM
[SOLVED] Bash/awk script question chogall Programming 7 12-23-2010 01:54 AM
bash script and awk phonebooth Programming 8 03-27-2010 12:51 AM
Bash script is enterpreting $1, $2 values in awk script ... praveen_218 Programming 4 09-14-2009 03:38 PM
can awk see bash script arguments ? sharapchi Programming 7 12-14-2006 08:03 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:43 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration