How can I use awk to count matched lines
by DV100 from LinuxQuestions.org on (#5EEVW)
greeting
Someone wrote this code on stackoverflow. The code works but I need to know how to add a count for matched lines and which line in a.txt found the match.
Contents of a.txt
01 02 03 04
11 12 13 14
04 02 03 01
19 20 21 25
11 15 39 01
Contents of b.txt
01 02 03 04 05
11 15 33 31 12
01 02 05 25 17
03 02 01 04 21
The Code
Code:#!/bin/bash
awk 'NR==FNR { a[++n] = $1; b[n] = $2; c[n] = $3; d[n] = $4; next }
{ for (i=1; i<=n; ++i)
if ($0 ~ a[i] && $0 ~ b[i] && $0 ~ c[i] && $0 ~ d[i]) {
print; break }
}' a.txt b.txtThe output of script
Code:$ awk-testmatch
01 02 03 04 05
03 02 01 04 21
How can I count the matches made, eg. 2 in above and which line in a.txt file
did the match?
I appreciate any help or suggestions, Thanks


Someone wrote this code on stackoverflow. The code works but I need to know how to add a count for matched lines and which line in a.txt found the match.
Contents of a.txt
01 02 03 04
11 12 13 14
04 02 03 01
19 20 21 25
11 15 39 01
Contents of b.txt
01 02 03 04 05
11 15 33 31 12
01 02 05 25 17
03 02 01 04 21
The Code
Code:#!/bin/bash
awk 'NR==FNR { a[++n] = $1; b[n] = $2; c[n] = $3; d[n] = $4; next }
{ for (i=1; i<=n; ++i)
if ($0 ~ a[i] && $0 ~ b[i] && $0 ~ c[i] && $0 ~ d[i]) {
print; break }
}' a.txt b.txtThe output of script
Code:$ awk-testmatch
01 02 03 04 05
03 02 01 04 21
How can I count the matches made, eg. 2 in above and which line in a.txt file
did the match?
I appreciate any help or suggestions, Thanks