Skip to main content
added 12 characters in body
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

Assuming that you want to print just two lines for each match, with GNU or FreeBSD grep, you can pass A.txt as a list of patterns to grep and tell it to print the line after the match. This produces extra lines with just -- between each match, you can easily remove them with an extra call to grep.

grep -A1 -Ff A.txt B.txt | grep -vx -- '--'

If the number of lines per section isn't fixed, you can use awk. First read the file containing the text to look for, then for each line in B.txt that starts a new section, either start or stop printing depending on whether the section head is in A.txt or not.

awk -v patterns_file=A.txt '
  BEGIN {
    while (getline <patterns_file) patterns_array[$0] = 1;
    close(patterns_file);
  }
  /^/ { matching = $0 in patterns_array }
  matching { print }
' B.txt

Assuming that you want to print just two lines for each match, with GNU or FreeBSD grep, you can pass A.txt as a list of patterns to grep and tell it to print the line after the match. This produces extra lines with just -- between each match, you can easily remove them with an extra call to grep.

grep -A1 -Ff A.txt B.txt | grep -vx -- '--'

If the number of lines isn't fixed, you can use awk. First read the file containing the text to look for, then for each line in B.txt that starts a new section, either start or stop printing depending on whether the section head is in A.txt or not.

awk -v patterns_file=A.txt '
  BEGIN {
    while (getline <patterns_file) patterns_array[$0] = 1;
    close(patterns_file);
  }
  /^/ { matching = $0 in patterns_array }
  matching { print }
' B.txt

Assuming that you want to print just two lines for each match, with GNU or FreeBSD grep, you can pass A.txt as a list of patterns to grep and tell it to print the line after the match. This produces extra lines with just -- between each match, you can easily remove them with an extra call to grep.

grep -A1 -Ff A.txt B.txt | grep -vx -- '--'

If the number of lines per section isn't fixed, you can use awk. First read the file containing the text to look for, then for each line in B.txt that starts a new section, either start or stop printing depending on whether the section head is in A.txt or not.

awk -v patterns_file=A.txt '
  BEGIN {
    while (getline <patterns_file) patterns_array[$0] = 1;
    close(patterns_file);
  }
  /^/ { matching = $0 in patterns_array }
  matching { print }
' B.txt
Source Link
Gilles 'SO- stop being evil'
  • 865.3k
  • 205
  • 1.8k
  • 2.3k

Assuming that you want to print just two lines for each match, with GNU or FreeBSD grep, you can pass A.txt as a list of patterns to grep and tell it to print the line after the match. This produces extra lines with just -- between each match, you can easily remove them with an extra call to grep.

grep -A1 -Ff A.txt B.txt | grep -vx -- '--'

If the number of lines isn't fixed, you can use awk. First read the file containing the text to look for, then for each line in B.txt that starts a new section, either start or stop printing depending on whether the section head is in A.txt or not.

awk -v patterns_file=A.txt '
  BEGIN {
    while (getline <patterns_file) patterns_array[$0] = 1;
    close(patterns_file);
  }
  /^/ { matching = $0 in patterns_array }
  matching { print }
' B.txt