0

I'm trying to find how to search for two strings in a text file within n lines of each other. For example, given the file:

abc def
ssssss
ssssss
ssssss
ssssss
ssssss
pqr xyz

I want "myscript abc pqr 5" to return nothing but "myscript abc pqr 7 (or greater)" to return the filename in which it was found and (bonus) the matching lines and the intervening ones. It should work with any strings.

Just to make it interesting, I'm working on Solaris 10 and do not have access to any gnu commands or extensions. It's like swimming in treacle.

TIA

Nigel.

prosmart
  • 54
  • 7
  • I'm afraid the offered solution didn't really help. The most pertinent part is the "within 'n' lines of each other. The rest of it is simple. – prosmart Apr 29 '14 at 04:27

1 Answers1

0

This script gets you almost all the way there:

#!/bin/bash
if [[ $(pcregrep -M "$1.*(\n|.)*$2" test.txt | wc -l) == $3 ]]
then
  echo "found"
else
  echo "not found"
fi

You can modify this to find the name of the file instead of just printing "found".

This script was inspired by this answer.

Community
  • 1
  • 1
msound
  • 445
  • 3
  • 7