Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read an input file one line at a time to pass as input to grep

My requirement is to read a file and then run a grep command on the read line one at a time for all the lines in the file.

Filtering the required file which matches a pattern

find . -name *.ini -exec grep -w HTC {} \; -print | grep ini > input.files

cat input.files

./PLATFORM/android/build/integration/android/suites/android_Prefs_Devices_Comms_Suite/ini/android_0019.ini
./PLATFORM/android/build/integration/android/suites/android_Prefs_Devices_Comms_Suite/ini/android_0150.ini
./PLATFORM/android/build/integration/android/suites/android_Prefs_Devices_Comms_Suite/ini/android_0616_1.ini
./PLATFORM/android/build/integration/android/suites/android_SI_Query_Suite/ini/android_0547_4.ini
./PLATFORM/android/build/integration/android/suites/android_SI_Query_Suite/ini/android_0578.ini
./PLATFORM/android/build/integration/android/suites/android_PDL_Suite/ini/android_5203_1.ini
./PLATFORM/android/build/integration/android/suites/android_System_Maintenance_Suite/ini/android_0579_2.ini

Any idea how to read one line at a time from input.files and execute a grep command on that ?

cat input.files -exec grep -w HTC_One {} \;
like image 832
rajeshk Avatar asked Nov 27 '25 06:11

rajeshk


1 Answers

There won't be any difference in the result if you say:

grep pattern file1
grep pattern file2

or

grep pattern file1 file2

Simply, use xargs:

cat input.files | xargs grep -w HTC_One
like image 174
devnull Avatar answered Nov 28 '25 21:11

devnull



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!