2

I have Sample file s1.txt like this:

  Sample.log.54
  Sample.log.56
  Sample.log.57
  Sample.log.58
  Sample.log.59
  Sample.log.110
  Sample.log.113
  Sample.log.114
  Sample.log.115
  Sample.log.116
  Sample.log.117
  Sample.log.118
  Sample.log.119
  Sample.log.120
  Sample.log.121
  Sample.log.122
  Sample.log.112
  Sample.log.123
  Sample.log.124
  Sample.log.140

In the folder i have many other files. but i need to grep particular string from the files that are listed in s1.txt. I should not read all the files which are present in the folder.

Naresh
  • 191
  • 1
  • 11

4 Answers4

3

If you need to grep for foo in the files and are using bash, you can use:

grep foo $(cat s1.txt)
Jesusaur
  • 354
2

If each line within s1.txt represents a file name and file names have no funky characters in them:

<s1.txt xargs -d '\n' grep string
iruvar
  • 16,725
0

This question is answered on stack overflow ([https://stackoverflow.com/questions/14655717/grepping-from-a-text-file-list][1]).

xargs grep "your pattern" < my-file-list.txt
bjerre
  • 46
-1

This will work:

for i in `cat s1.txt`; do cat $i | grep "your-string-to-search"; done
Rui F Ribeiro
  • 56,709
  • 26
  • 150
  • 232