Need help in scanning text files and find all the words between two patterns. Like say if we have a .sql file, Need to scan and find all words between from\' and \'where\'.
You could use ed for this, it allows positive and negative offsets for the regex range. If the input is:
seq 10 | tee > infile
1
2
3
4
5
6
7
8
9
10
Pipe in the command to ed:
<<< /3/,/6/p | ed -s infile
i.e. print everything between lines containing 3 and 6.
Result:
3
4
5
6
To get one more line at each end:
<<< /3/-1,/5/+1p | ed -s infile
Result:
2
3
4
5
6
7
Or the other way around:
<<< /3/+1,/6/-1p | ed -s infile
Result:
4
5