LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Get awk to work in bash script (https://www.linuxquestions.org/questions/linux-newbie-8/get-awk-to-work-in-bash-script-4175625013/)

silver007 03-05-2018 06:41 PM

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

keefaz 03-05-2018 06:58 PM

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...

scasey 03-05-2018 07:37 PM

edit: I should learn to read....never mind.

X-LFS-2010 03-05-2018 10:04 PM

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

AwesomeMachine 03-05-2018 10:37 PM

You can put the awk part in an awk script and call it from the bash script.

syg00 03-05-2018 10:51 PM

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.

astrogeek 03-05-2018 11:31 PM

@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.


All times are GMT -5. The time now is 06:57 AM.