user3866091
user3866091

Reputation: 1

Extracting string from filenames within a file

new to bash scripts and trying to grep for a string in a list of directories:

directory.txt contains the following

/apps/work/txt.out
/apps/work/Monday.txt
/apps/garbage/howdo.file

What I want to do is for each line in directory.txt, read the file and grep for a string.

Can someone point me in the right direction?

Upvotes: 0

Views: 42

Answers (2)

Gilles Quénot
Gilles Quénot

Reputation: 184975

Try doing this :

cat directory.txt | xargs grep word

Replace word with your own pattern.

Upvotes: 0

arco444
arco444

Reputation: 22821

Should be as simple as this:

search="BLOCK"

while read -r filename; do
  [ -f "$filename" ] && grep "$search" "$filename"
done < directory.txt

The while loop gets its input from directory.txt, puts each line into the variable $filename, checks if the file actually exists and if so executes the grep command on it

Upvotes: 1

Related Questions