awkfiltersubstringpositionrow

Print substring of column based on position filter in awk


I have a file with following 3 columns

1   a1  abcd
2   b1  acdb
3   c1  abcd 

I need to extract/print rows based on substring of column3 on position filter (2=="b"), so the output should be

1   a1  abcd
3   c1  abcd 

Based on (Print substring of column in awk based on filter) I have tried:

awk -F '\t' -v OFS='\t' '{ $3=substr($3,2,1); print $0 }' a.txt 

Solution

  • You may use this awk:

    awk -F '\t' 'substr($3, 2, 1) == "b"' file
    
    1   a1  abcd
    3   c1  abcd