0

This is a continuance to Multiple line, repeated occurence matching

I have many test*.txt files with contents as per previous thread.

test1.txt

blah blah..
blah blah..
blah abc blah1
blah blah..
blah blah..
blah abc blah2
blah blah..
blah efg1 blah blah
blah efg2 blah blah
blah blah..
blah blah..

blah abc blah3
blah blah..
blah blah..
blah abc blah4
blah blah..
blah blah blah

blah abc blah5
blah blah..
blah blah..
blah abc blah6
blah blah..
blah efg3 blah blah

blah efg4 blah blah
blah abc blah7
blah blah..
blah blah..
blah abc blah8
blah blah..

Now I wanted to modify the output to run the sed command on all files, but also displaying the filename together with line number (if possible) with the output of the sed command...

I run below command

ls test*.txt | xargs sed -n -f findMatch.txt

findMatch.txt content

/abc/h;/efg/!b;x;/abc/p;z;x

output is

blah abc blah2
blah abc blah6
blah abc blah2
blah abc blah6
blah abc blah2
blah abc blah6

I need a bit more detailed output as per below

test1.txt ln6 blah abc blah2
test1.txt ln23 blah abc blah6
test2.txt ln6 blah abc blah2
test2.txt ln23 blah abc blah6
test3.txt ln6 blah abc blah2
test3.txt ln23 blah abc blah6
Community
  • 1
  • 1
user3663854
  • 463
  • 2
  • 8
  • 21
  • This operation is a bit complex. Though it should be do-able with just sed (or pipelines of sed), I would suggest that you move to `awk`. It would be much easier with awk. The operation is possible with awk. Plus, awk has variables like `FILENAME` & `NR/FNR` which can be used here. – anishsane Feb 25 '16 at 05:15

1 Answers1

1

grep command used to search the particular pattern to all files.

  grep -f patten_file -Rn *.txt

-R Recursive

-n Line number

Patten_file
hai
hello
this

Output:

1.txt:1:hai
1.txt:2:hello
2.txt:1:hai
2.txt:2:this
loganaayahee
  • 809
  • 2
  • 8
  • 13