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

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

Related

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

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.

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 Print fields with new values if match, else print line

I have two files - FileA and FileB. FileA will be changed. FileB contains the new values. FileB has 3 fields. The first two fields will be compared with FileA's first two fields. If the fields match, Field3 should be changed. The code below is working in this manner: "If the two values match, change field3 and print the line. If there is no match, next." The behavior I want is, "If there is no match, print the line unchanged." The "else" part of the code is not working and I've tried so many variations.
awk -F'\t' -v OFS='\t' '
# first, read in data from file B
NR == FNR { values[$1 FS $2] = $3; next }
# then, output modified lines from matching lines in file A
($1 FS $2) in values { $3 = values[$1 FS $2]; print } else { print $0 }
' fileB fileA
FileA
PROVDSRJ02.RD.RI ae0.0 16
PROVDSRJ02.RD.RI ae1.1 1000
PROVDSRJ02.RD.RI ae2.0 5000
PROVDSRJ02.RD.RI ae3.0 5000
ASHBBBRJ01.RD.AS ae39.0 16
ASHBBPRJ01.RD.AS ae2.0 16
ASHBBPRJ02.RD.AS ae1.0 16
ASHBBPRJ02.RD.AS ae2.0 16
ASHBBBRJ01.RD.AS ae0.0 16
ASHBBBRJ01.RD.AS ae11.0 16
FileB
ASHBBBRJ01.RD.AS ae10.0 524
ASHBBBRJ01.RD.AS ae11.0 235
ASHBBBRJ01.RD.AS ae39.0 2096
ASHBBBRJ01.RD.AS ae6.0 183
ASHBBBRJ01.RD.AS ae7.0 1141
ASHBBBRJ02.RD.AS ae11.0 88
ASHBBBRJ02.RD.AS ae13.0 333
ASHBBBRJ02.RD.AS ae20.0 374
ASHBBBRJ02.RD.AS ae9.0 1885
Desired Output (** indicate changed lines and should not be included in code)
PROVDSRJ02.RD.RI ae0.0 16
PROVDSRJ02.RD.RI ae1.1 1000
PROVDSRJ02.RD.RI ae2.0 5000
PROVDSRJ02.RD.RI ae3.0 5000
**ASHBBBRJ01.RD.AS ae39.0 2096**
ASHBBPRJ01.RD.AS ae2.0 16
ASHBBPRJ02.RD.AS ae1.0 16
ASHBBPRJ02.RD.AS ae2.0 16
ASHBBBRJ01.RD.AS ae0.0 16
**ASHBBBRJ01.RD.AS ae11.0 235**
Your syntax is off. Check the tag info for some learning resources.
In any case, you don't need an else as such. You can conditionally set $3 to the new value (as you already are doing), and then always print the line (which may have been modified or not).
Here we use the shortcut 1 to always print the line. 1 is an always-true pattern that invokes the default action, which is to print the current line. If that doesn't make sense now, it will soon.
$ awk 'BEGIN {FS=OFS="\t"}
NR == FNR {values[$1 FS $2] = $3; next}
($1 FS $2) in values {$3 = values[$1 FS $2]}1' fileB fileA
PROVDSRJ02.RD.RI ae0.0 16
PROVDSRJ02.RD.RI ae1.1 1000
PROVDSRJ02.RD.RI ae2.0 5000
PROVDSRJ02.RD.RI ae3.0 5000
ASHBBBRJ01.RD.AS ae39.0 2096
ASHBBPRJ01.RD.AS ae2.0 16
ASHBBPRJ02.RD.AS ae1.0 16
ASHBBPRJ02.RD.AS ae2.0 16
ASHBBBRJ01.RD.AS ae0.0 16
ASHBBBRJ01.RD.AS ae11.0 235

remove lines that do not match specific digits in list file using awk

I am trying to use awk to remove the lines in file that do not match the digits after the NM_ but before the . in $2 of list. Thank you :).
file
204 NM_003852 chr7 + 138145078 138270332 138145293
204 NM_015905 chr7 + 138145078 138270332 138145293
list
TRIM24 NM_015905.2
awk
awk -v OFS="\t" '{ sub(/\r/, "") } ; NR==FNR { N=$2 ; sub(/\..*/, "", $2); A[$2]=N; next } ; $2 in A { $2=A[$2] } 1' list file > out
current output
204 NM_003852 chr7 + 138145078 138270332 138145293
204 NM_015905.2 chr7 + 138145078 138270332 138145293
desired output (line 1 removed as that is the line that does not match)
204 NM_015905.2 chr7 + 138145078 138270332 138145293
awk 'NR==FNR{split($2,f2,".");a[f2[1]];next} $2 in a' list file
$ awk -F'[ .]' 'NR==FNR{a[$2];next}$2 in a' list file
204 NM_015905 chr7 + 138145078 138270332 138145293

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