Match pattern and print the line number of occurence using awk

Hi,

I have a simple problem but i guess stupid enough to figure it out. i have thousands rows of data. and i need to find match patterns of two columns and print the number of rows. for example:

inputfile

abd       abp      123
abc       abc      325
ndc       ndc      451
mjk       lkj      415
ndc       ndc      100

There are 5 rows above and i match $1 and $2. Those that match will print out all columns together with the number of rows. I know i can use grep -n to do this, but when i combine grep -n with awk (for matching the two columns) it gives me the sequence no of match pattern which not that i wanted. I want the result like below:-

3: ndc   ndc   451
5: ndc   ndc   100

my codes as follows:-

awk -F"\t" 'match($1,/ndc/) && match($2,/ndc/){print $0}' inputfile | grep -w -n "ndc"

which gives me this result that i do not want:-

1: ndc   ndc   451
2: ndc   ndc   100

The number in red color is the number for the rows number. I can see this is simple but i just could not get it. pls help. thanks

Try:

awk -F"\t" '$1==$2&&$1=="ndc"{print NR": "$0}' inputfile
1 Like
awk -F" " '{if($1=="ndc" && $2=="ndc") {print NR"\t"$0}}' inputFile

Change your Column patterns in place of "ndc" ......

Output:
3 ndc ndc 451
5 ndc ndc 100

1 Like

Hi,

Both codes worked perfectly.. Thanks so much :slight_smile: