5

I looked a lot of places for this answer, but could not find it. Still learning AWK here, and was just wondering how to print the column number where a match is found.

I want the script to give me the field/column number where regexp match "/1" is found

INPUT

name1 0/0 0/0 0/1 0/0
name2 0/1 0/0 0/0 0/0
name3 1/1 0/0 0/0 0/0
name4 0/0 0/0 0/0 1/1

DESIRED OUTPUT

4
2
2
5
adrotter
  • 301
  • 3
  • 10
  • 1
    use a `for` loop to loop over the columns, and then print the value of the variable. – Barmar Mar 02 '15 at 23:03
  • 1
    We're not here to write your code for you. Please make an attempt, and post what you tried so we can help you fix it. – Barmar Mar 02 '15 at 23:04

1 Answers1

14
$ cat adr.txt
name1 0/0 0/0 0/1 0/0
name2 0/1 0/0 0/0 0/0
name3 1/1 0/0 0/0 0/0
name4 0/0 0/0 0/0 1/1

$ awk '{ for (i=1; i<=NF; ++i) { if ($i ~ "/1$") print i } }' adr.txt
4
2
2
5
jas
  • 10,715
  • 2
  • 30
  • 41