I have a log file, name out.log. I need to search a list of string (filelist.txt) from this log file.
I need each matching line from "out.log" for every file in filelist.txt. it can appear multiple times.
cat out.log | grep -i I466030.CXR910E >> search_result.txt
this works, it will search for I466030.CXR910E and output to search_result.txt
for i in `cat filelist.txt`; do grep -i "$i" out.log >> result.txt; done
for i in `cat filelist.txt`;
do
echo $i
cat out.log | grep -i $i >> result2.txt
grep $i out.log >> result3.txt
done
Above code the echo will output the search string but none of the grep not writing output to any result*.txt
What am I missing here ? Thanks for your advice.
In response to comments:
xxxxxm@cxxxrtrls01[DEV][rerun] $ grep -i -f filelist.txt out.log > result.txt
xxxxxm@cxxxrtrls01[DEV][rerun] $ ll
total 564
-rw-r----- 1 cnrlsadm cnrlsadm 4600 Aug 19 09:36 filelist.txt
-rw-r----- 1 cnrlsadm cnrlsadm 216 Aug 19 10:14 file.sh
-rw-r--r-- 1 cnrlsadm cnrlsadm 557403 Aug 19 10:10 out.log
-rw-r--r-- 1 cnrlsadm cnrlsadm 0 Aug 19 12:14 result.txt
-rw-r----- 1 cnrlsadm cnrlsadm 213 Aug 19 12:10 srch.sh
xxxxxm@cxxxrtrls01[DEV][rerun] $
size of result.txt is zero , it doesnt have anything.
-f
option) without needing a shell for loop. trygrep -i -f filelist.txt out.log > result.txt
– cas Aug 19 '19 at 04:03set -x
before the loop so it'll print out what the shell thinks is happening and you can see what's different from what you expect. – Gordon Davisson Aug 19 '19 at 04:35filelist.txt
andout.log
it's impossible to tell why neither the for loop nor the grep are working. @GordonDavisson's guess about dos/windows text files is a pretty good one and may be the source of the problem. – cas Aug 19 '19 at 04:41filelist.txt
being a DOS text file. The patterns (filenames) in that file would then have an extra carriage-return character at the end, which would never match in theout.log
file. OR there are simply no matches inout.log
(you haven't shown an example of a log entry that ought to be matched). – Kusalananda Aug 19 '19 at 05:56