Print specific field when condition met

Hi All,

Seeking for your assistance to print all the specific field when the condition met.

Ex:
file1.txt
1|203|3|31243|5341|6452|623|22|00|01
3|45345|123214|6534|3423|6565|643|343|232|10

if field 1 = 1 and field 3 = 3 and field 5 = 5341 and field 6 = 6452
it will print from $1 to $10.

What i did was i used awk but it output first all $1 record then next is all $2 record which is wrong.

#!/bin/sh

type1=`awk -F "|" '{print $1}' file1.txt`
type2=`awk -F "|" '{print $3}' file1.txt`
type3=`awk -F "|" '{print $5}' file1.txt`
type4=`awk -F "|" '{print $6}' file1.txt`

if [[ $type1 == "1" && $type2 == "3"  && $type3 == "5341" && $type4 == "6452"  ]]; then
awk -F "|" '{print $1,$2,$3,$4,$5,$6,$7,$8,$9,$10}'
fi

Please advise,

Thanks,

Hello znesotomayor,

Following may help you in same.

cat file23
1|203|3|31243|5341|6452|623|22|00|01
3|45345|123214|6534|3423|6565|643|343|232|10

awk -F"|" '($1==1 && $3==3 && $5==5341)' file23
 

Output will be as follows.

1|203|3|31243|5341|6452|623|22|00|01
 

Hope this helps.

Thanks,
R. Singh

1 Like

Thanks Mr. R. Singh. It helps me a lot.