There are several files in a directory. I am trying to search and find all such strings ending with a given string. Instead of searching in all the files present in the directory, I would like to search only in a given set of file names. Finally the output should be each filename along with the string occurrences found in that file with a semicolon separator.
The simplified test case is: there are 5 files in the directory:
file.a.txt
file.b.txt
file.c.txt
file.d.txt
file.e.txt
There is also a file named searchFiles.txt
that contains the first 3 filenames from above list. So I want to search for the string only in the filenames listed in searchFiles.txt
.
I have tried:
for i in $(cat searchFiles.txt); do grep -o '[^ ]*_XYZ' /dev/null $i ; done | awk -F: '{a[$1]=a[$1]";"$2;} END{for (x in a) print x ":" substr(a[x],2);}'
but the output says
: No such file or directory
: No such file or directory
file.c.txt:FOUND1_XYZ;FOUND2_XYZ
So somehow it is able to search only in the last file name given in searchFiles.txt but the other initial files couldn't be found and thus throws the error 'No such file or directory'
The output I am expecting is:
file.a.txt:FOUNDSTR_XYZ
file.b.txt:FOUNDSTR1_XYZ;FOUNDSTR2_XYZ;FOUNDSTR3_XYZ
file.c.txt:FOUND1_XYZ;FOUND2_XYZ
I was also trying to find if the "-name" flag of "find" command could be helpful here, but couldn't quite get it on how exactly to feed the list of files from searchFiles.txt here. The below attempt went in vain.
find . -type f -name `cat searchFiles.txt` -exec grep -o '[^ ]*_XYZ' /dev/null {} \;
Also:
There can be upto few 1000s of files in a directory and the search file names in searchFiles.txt can be few 100s of file names.
The file names can be anything and doesn't follow any pattern as such.
The file names provided in searchFiles.txt can be partial names for instance a.txt instead of file.a.txt which means the initial static portion of the file name "file." may or may not be present in searchFiles.txt.
preferably looking for a one-liner commands instead of a shell script
Any help on this please?
searchFiles.txt
have DOS-style (CRLF) line endings by any chance?