-1

I have an alphabetically sorted file names.txt containing names:

Dio Brando
Erina Pendleton
Jonathan Joestar
Mario Zeppeli
....

and a folder containing files:

a1.txt
a2.txt
....
zn.txt

each having lines with Name: phone number. Assuming recursion goes though files alphabetically, I'd like to grep each line from names.txt and switch to the next name whenever it fails to find a match in the next one.

For example: I grep for "Dio Brando" and it reaches in file, say, D2.txt following lines:

Dio Brando: phone number 1
Dio Brando: phone number 2
Dio Brando: phone number 3
<not Dio Brando>: phone number 1

When I hit the <not Dio Brando> line, I want it to start searching for "Erina Pendleton" where it left off (from D2.txt onward). It's guarantied to find at least one match, and when it fails after that, it should switch to "Jonathan Joestar" etc. Is it possible without some serious scripting?

If resuming with the next name is not doable, just performing grep on each line through the whole folder will do.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
Renox92
  • 69
  • 1
  • 3
  • The fallback strategy, `grep -r pattern ...` is listed here: https://stackoverflow.com/a/1987928/667820. What you want to do can be done with some advanced `awk` programming, where you feed `awk` with a list of files, and use the names as variable patterns. But that would be serious scripting. – Henk Langeveld Jul 18 '21 at 15:30

1 Answers1

1

It sounds like this might be all you need:

awk '
BEGIN { nameNr = 1 }
NR==FNR {
    names[NR] = $0
    next
}
{
    if ( index($0,names[nameNr]) ) {
        print FILENAME, FNR, $0
        found = 1
    }
    else if ( found ) {
        nameNr++
        found = 0
    }
}
' *.txt

but since you didn't provide sample input/output we could test with that's just an untested guess.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185