awk pattern matching problem -not recognizing a column

Hi all,
I am new to awk.

I want to print the line numbers if the column has a particular value.

For example I have:

cat FILE1
COL1 COL2
X114 0
X116 0
X117 0
X120 0
X121 0
X125 0
X126 0
X127 0
X131 1
X132 0
X135 0
X136 0
X137 0
X138 0
X140 0
X141 0
X143 0
X144 0
X146 0
X147 1
X150 0

I want to print the lines having value 1 in column 2.
My output should be

10
21

I tried:

awk -v a=2 -F '\t' 'BEGIN {x=1}; {if (1~$a) print x; x++}' FILE1

But it didnot produce any output.

After trying for a long time, I just added aother column to this file (in excel) with zeros. ie;

cat FILE2
COL1 COL2 0
X114 0 0
X116 0 0
X117 0 0
X120 0 0
X121 0 0
X125 0 0
X126 0 0
X127 0 0
X131 1 0
X132 0 0
X135 0 0
X136 0 0
X137 0 0
X138 0 0
X140 0 0
X141 0 0
X143 0 0
X144 0 0
X146 0 0
X147 1 0
X150 0 0
X152 0 0

Now when I do:

awk -v a=2 -F '\t' 'BEGIN {x=1}; {if (1~$a) print x; x++}' FILE2

I am getting the correct output.

can anyone help me on why awk is not working for FILE1. I have to do the same in 100 files. I am not sure what mistake I had made when working on FILE1 format

Your reply will be very helpful. Thanks in advance

try:

#  awk '$2==1{print NR}' file1
10
21

HTH

1 Like

Hi what happens if you leave out:

-F '\t'

If you specify the separation characters, then each tab separates a different field, so to consecutive tabs means one empty field. If you leave out the -F option multiple tabs or spaces or mixture thereof gets interpreted as one separator. If you are exporting from excel, perhaps this was not done consistently, or perhaps is was space separated he first time?

alternative :wink:

# cat colon
COL1 COL2
X114 0
X116 0
X117 0
X120 0
X121 0
X125 0
X126 0
X127 0
X131 1
.........
# sed -n '/ 1/=' colon
10
21
1 Like