johan
johan

Reputation: 45

linux script shell : grep a part of path in a list of path

In my script shell, i have 2 files. The first one is a file containing only names of files with part of the path :

list1:

aaa/bbb/file1.ext
ccc/ddd/file2.ext
eee/fff/file3.ext

The second one is a list of every files of the extension ".ext" with the absolute path before them:

list2:

/home/.../aaa/bbb/file1.ext
...
...
...
/home/...ccc/ddd/file2.ext
...

And I am trying to extract the lines of the second file list2, containing the lines of the first one with grep.

For now I tried :

while read line
do
    grep "$line" "list1" 
done < list2

But this command doesn't ouptut anything, however the command

grep "aaa/bbb/file1.ext" "list1"

have the output I am waiting for

/home/.../aaa/bbb/file1.ext

Anyone sees what I am missing on this script? Thanks

Upvotes: 2

Views: 535

Answers (1)

fedorqui
fedorqui

Reputation: 289585

This is one of the cases where -f option from grep comes very handy:

grep -f f1 f2

For your given input returns:

/home/.../aaa/bbb/file1.ext
/home/...ccc/ddd/file2.ext

From man grep:

-f FILE, --file=FILE

Obtain patterns from FILE, one per line. The empty file contains zero patterns, and therefore matches nothing. (-f is specified by POSIX.)

Upvotes: 3

Related Questions