Search and print matching string

2019-09-03 15:46发布

问题:

have a folder structure as shown below ./all_files

-rwxrwxrwx reference_file.txt drwxrwxrwx file1.txt drwxrwxrwx file2.txt drwxrwxrwx file3.txt

reference_file.txt has filenames as shown below

file1.txt file2.txt

data in file1.txt and file2.txt are as shown below:

step_1 step_2 step_3

Now, I have to take particular step say step2 from each file

Note1: file name must present in reference_file.txt

Note2: step2 is not line no:2 always.

Note3: search should perform recursively.

I have used below scripts:

  1. which is not giving what i expected. All blank and unwanted lines displayed

#!/bin/sh

while read f; do find . -type f -name "${f}" | xargs grep -l -F 'step_2' done <reference_file.txt

  1. It is searching in only one folder but not searching recursively

#!/bin/sh

for i in reference_file.txt do find . -type f -name "${f}" | xargs grep -l -F 'step_2' done

Please help me on this.

回答1:

With removing -l option it works for me.

for f incat reference_file.txt; do find . -type f -name $f | xargs grep 'step_2' ; done

I can see it works recursively as well. See the output below when I run it from outside of dir1 (directory structure created by me)

./dir1/file1:step_2:
./dir1/file2:step_2:

Please share more information like directory structure and file names if issue in recursion still exists.



标签: shell