I am trying to search for a particular string within zipped files but cannot get the 'xargs' syntax correct.
The files unzip/zip correctly but xargs is actually searching for nothing (we are looking for failed TLS EMails).
Can anyone give me some pointers about the correct xargs syntax?
for filename in $( ls -1 ${HOST}-mail-2018${1}[0-9][0-9]* )
do
filetype=${filename##*.}
case $filetype in
bz2)
unzipper="bzip2 -d "
zipper="bzip2"
unzfile=${filename%.${filetype}}
;;
gz)
unzipper="gzip -d "
zipper="gzip "
unzfile=${filename%.${filetype}}
;;
xz)
unzipper="xz -d "
zipper="xz "
unzfile=${filename%.${filetype}}
;;
*)
echo "Unknown compression type for file $filename"
break
;;
esac
# Testing: echo $unzipper $zipper $unzfile
echo $unzipper $zipper $filename $unzfile
eval ${unzipper} ${filename}
grep 'Cannot .*TLS' ${unzfile} | sed 's/^.*]: //' | sed 's/:.*//' | xargs fgrep
eval ${zipper} ${unzfile}
done
exit 0
xargs
at all. Isn'tgrep PATTERN | sed ...
enough? Also, you shouldn't zip and unzip back the files; simplygzip -cd file | grep PATTERN
orbzip -cd file | grep ..
(orzgrep
,bzgrep
) will do. And you shouldn't determine file type from the extension: usetype=\
file -i filename`` thencase $type in application/gzip;*) ...;; application/x-bzip2;*) ... esac
. – Oct 16 '18 at 10:02