Extract filenames from file

grep for word in file | remove spaces | remove unwanted text | do op on file

grep whatever whatever.log | sed 's/ /\\ /g' | sed 's/whatever//g' | xargs ls -l

Example: List infected files

clamscan -r -lclamscan.log / recursively scans all files and writes output to clamscan.log

grep FOUND clamscan.log gives a file that contains the scan report. We want to look for “FOUND” to see get lines about infected files, e.g..

/home/steph/laptop/.thunderbird/iogvxkc9.default/Mail/pop.somemail-6.com/Trash: Email.Trojan-2 FOUND

We can now use a combo of grep and sed on that file to extract the filenames.

grep FOUND clamscan.log | sed 's/ /\\ /g' | sed 's/:\\ [^:]* FOUND//g' | xargs ls -l

Which will produce something like the following:

-rw-r--r-- 1 steph    steph     9342885 2009-11-19 15:18 /home/steph/.thunderbird/iogvxkc9.default/Mail/pop.somemail-6.com/Trash

non greedy matching

After escaping the spaces, we removed whatever matched :\\ [^:]* FOUND. This simply looked for ‘colon backslash space’ followed by ‘any character except colon’ followed by ‘space FOUND’.

Because the filenames may contain colons, matching for ‘any character except colon’ with [^:]* instead of ‘any character’ with .* allowed us to remove everything from the last occurrance of the colon.

Last modified: 16/10/2011 Tags: , , ,

This website is a personal resource. Nothing here is guaranteed correct or complete, so use at your own risk and try not to delete the Internet. -Stephan

Site Info

Privacy policy

Go to top