awk if else statement syntax error. What is causing it? - awk

There's a syntax error in awk if else statement which I got the code from another question and unable to fix it.
Bash one-liner code to output unique values.
Can someone correct the statement.
awk 'BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3} {print 0} else {if(output==0} {print 3} else {print output}}'
debug output
awk 'BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}'
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
awk: cmd. line:1: BEGIN {output=0} /Slave_IO_Running.*No/ {output+=1} /Slave_SQL_Running.*No/ {output +=2} END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error

if(output==0} and if(output==3} should end with close paren ), not close brace }.
You should use else if for nested if statements, and those braces are only necessary for multiple operations.
END {if(output==3) print 0; else if(output==0) print 3; else print output}
Just for fun:
END {print output==3? 0: output==0? 3: output}

There are clues in the error messages, e.g.:
awk: cmd. line:1: ... END {if(output==3}{print 0} else {if(output==0} {print 3} else {print output}}
awk: cmd. line:1: ^ syntax error
It really couldn't be much clearer.

Related

AWK: Printing a Variable Containing a Percentage Sign %

I'm trying to call a variable which contains a percentage sign in it, but when I do I receive the following error. Even when only trying to create it, it errors. When I exclude it, my script works fine, but I want it to print this variable value in the 3rd field.
awk -v postutil="$postutil"
Partial Output of Error:
awk: cmd. line:2: postutil=66%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=68%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=63%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=38%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=30%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=29%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=91%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=0%
awk: cmd. line:2: ^ unexpected newline or end of string
awk: cmd. line:2: postutil=0%
awk: cmd. line:2: ^ unexpected newline or end of string
Script:
while IFS=$'\t' read -r hostname interface preutil postutil criticality; do
awk -v hostname="$hostname" -v interface="$interface" -v postutil="$postutil" '$0~ hostname "\t" interface{print hostname, interface, postutil, $0}' OFS='\t' temp/post_lsp_interfaces_02.txt
done < temp/comparison_interfaces_high_med.txt
Partial of post_lsp_interfaces_02.txt
ASHBBPRJ01-CHNDDSRJ01-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae9.0 MCDLBBRJ01 ae9.0 CHNDBBRJ01 ae0.0 CHNDDSRJ01 3740.81
ASHBBPRJ01-DUKEDSRJ02-BE ASHBBPRJ01 ae1.0 ASHBBBRJ01 ae10.0 DUKEBBRJ02 ae6.0 DUKEDSRJ02 8182.02
ASHBBPRJ01-HMRDRCRJ01-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae0.0 MRFDBBRJ01 ae4.0 NRFKBBRJ01 ae0.0 NRFKDSRJ01 ae17.0 HMRDRCRJ01 4444.66
ASHBBPRJ01-HMRDRCRJ02-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae0.0 MRFDBBRJ01 ae4.0 NRFKBBRJ01 ae6.0 VBCHBBRJ01 ae0.0 VBCHDSRJ01 ae18.0 HMRDRCRJ023125.79
ASHBBPRJ01-MCDLDSRJ01-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae9.0 MCDLBBRJ01 ae0.0 MCDLDSRJ01 3862.34
ASHBBPRJ01-MRFDDSRJ02-10-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae1.0 MRFDDSRJ02 2110.26
ASHBBPRJ01-MRFDDSRJ02-11-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae1.0 MRFDDSRJ02 2110.26
ASHBBPRJ01-MRFDDSRJ02-12-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae1.0 MRFDDSRJ02 2110.26
ASHBBPRJ01-MRFDDSRJ02-13-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae1.0 MRFDDSRJ02 2110.26
ASHBBPRJ01-MRFDDSRJ02-14-BE ASHBBPRJ01 ae2.0 ASHBBBRJ02 ae11.0 MRFDBBRJ02 ae1.0 MRFDDSRJ02 2110.26
Partial of comparison_interfaces_high_med.txt
ASHBBBRJ02 ae5.0 9% 31% medium_increase
DALSBBRJ02 ae10.0 34% 0% medium_decrease
DALSBBRJ02 ae4.0 3% 44% medium_increase
DUKEBBRJ01 ae0.0 24% 75% high_increase
DUKEBBRJ01 ae5.0 56% 0% high_decrease
DUKEBBRJ02 ae2.0 5% 57% high_increase
DUKEBBRJ02 ae6.0 15% 73% high_increase
I ended up just using sed to remove the percent sign and then re-added it in the awk statement.

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 and if they match and the value in $10 is > 30 and $11 is > 49, then print the line to a output file. The below awk has syntax errors in it though shellcheck didn't return any. Both the input and output are tab-delimited. I think the below is close, but not sure what is wrong. Thank you :).
awk
awk -F'\t' -v OFS='\t' 'NR==FNR{A[$2];next}$2 in A
{if($10 >.5 OFS $11 > 49)
print ; next
' file1 file2
awk: cmd. line:2: {if($10 >.5 OFS $11 > 49)
awk: cmd. line:2: ^ syntax error
awk: cmd. line:3: print ; next
awk: cmd. line:3: ^ unexpected newline or end of string
file1
Missing in IDP but found in Reference:
2 166848646 G A exonic SCN1A 68 13 16;20 0;0 17;15 0;0 0;0 0;0 c.[5139C>T]+[=] 52.94
file2
chr2 166245425 SCN2A AMPL5155065355 SNP Het C/T C T 54 100 50 23 27
chr2 166848646 SCN1A AMPL1543060606 SNP Het G/A G A 52.9411764706 100 68 32 36
desired output
2 166848646 G A exonic SCN1A 68 13 16;20 0;0 17;15 0;0 0;0 0;0 c.[5139C>T]+[=] 52.94
edit with new awk
awk -F'\t' -v OFS='\t' 'NR==FNR{A[$2];next}$2 in A {
if($10 >.5 OFS $11 > 49) >>> if($10 >.5 && $11 > 49)
print }
' file1 file2 > out
awk: cmd. line:2: if($10 >.5 OFS $11 > 49) >>> if($10 >.5 && $11 > 49)
awk: cmd. line:2: ^ syntax error
here you go...
$ awk 'BEGIN{FS=OFS="\t"} NR==FNR{a[$2]; next}
($2 in a) && $10>30 && $11>49 ' file1 file2

Filter columns in Pig script

I am loading data in Pig from a CSV.
After having loaded data, I need to filter out columns .
exportAllProductsCleaned = FOREACH exportAllProducts
generate $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $26, $27, $28, $29, $30, $31, $32, $33
Is there a way wherein I can specify only
The columns I need to remove
OR
The range of columns I need for ex. $1-15 and then $18 - $30
Is it possible?
Yes, you can do so using '..' convention.Refer
Support project range expression
exportAllProductsCleaned = FOREACH exportAllProducts GENERATE $0, $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $26, $27, $28, $29, $30, $31, $32, $33;
exportAllProductsFiltered = FOREACH exportAllProductsCleaned GENERATE $1 .. $15,$18 .. $30;

Printing a string value in awk results in error

fatal: not enough arguments to satisfy format string
`%s SPT=80'
^ ran out for this one
This my code
for ((h = 1 ; h < 4 ; h++ )); do
x=$(awk -v i=h -v j=17 'FNR == 2 {printf "%s " $j}' newiptables.log)
echo $x
This is my file
Dec 26 09:17:51 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=313 TOS=0x00 PREC=0x00 TTL=64 ID=59334 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK PSH URGP=0
Dec 26 09:17:52 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=1440 TOS=0x00 PREC=0x00 TTL=64 ID=47303 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK URGP=0
Dec 26 09:17:52 localhost kernel: IN=eth0 OUT= MAC=00:10:c6:a8:da:68:00:90:7f:9c:50:5a:08:00 SRC=198.252.206.16 DST=10.128.1.225 LEN=1440 TOS=0x00 PREC=0x00 TTL=64 ID=47559 PROTO=TCP SPT=80 DPT=56506 WINDOW=46535 RES=0x00 ACK URGP=0
The problem is a missing comma in the printf command for awk:
awk -v i=h -v j=17 'FNR == 2 {printf "%s ", $j}' newiptables.log
^
|== This is needed
Quoting from the manual:
A simple printf statement looks like this:
printf format, item1, item2, ...

Unable to combine words in Files by Zsh/AWK

I have File1
A,B,C
and File2
D,E,F
I am trying to have
AD, AE, AF, BD, BE, BF, CD, CE, CF
unsuccessfully by
echo {`cat File1`}{`cat File2`}
giving
{A,B,C}{D,E,F}
How can you solve the problem by Zsh/AWK?
awk -F, '
NR==FNR {
# read lines from File1 into the array f1
f1[NR]=$0
next
}
{
# foreach line in File2
split(f1[FNR], words); # get words from corresponding line in File1
sep = ""
for (i in words) {
for (j=1; j<=NF; j++) {
printf("%s%s%s", sep, words[i], $j)
sep = ", "
}
}
print ""
}
' File1 File2
If File1 contains
A,B,C
1,2,3
and File2 contains
D,E,F
4,5,6
then the awk script outputs
AD, AE, AF, BD, BE, BF, CD, CE, CF
14, 15, 16, 24, 25, 26, 34, 35, 36
I don't know zsh, here's what I did with bash and sed:
echo "A,B,C" >a
echo "D,E,F" >b
for i in `cat a | sed -e "s#,#\n#g"`;
do for j in `cat b | sed -e "s#,#\n#g"`;
do echo -n "$i$j, ";
done ;
done | sed -e "s#,\s\$##"
The output then is:
AD, AE, AF, BD, BE, BF, CD, CE, CF