If you used the find command in the last post to find files and direct them to a file like so:
find /some/root -name '*.*' -exec egrep -l -e 'string to find' {} \; > files.txt
you will now have a file that contains a list of files with full paths.
/some/root/file1.xyz
/some/root/file2.xyz
/some/root/file3.xyz
All these files contain the pattern you searched for in the egrep command. There may be duplicates in the list of files. You can get rid of them like so:
cat files.txt | sort | uniq > files2.txt
To see the files output on the screen just leave off the '> files2.txt'
these commands put together with the pipe '|' list all the files, then sort them, then remove the duplicates and redirect the clean output to a new file. This is very very useful for all sorts of applications. I probably use these commands several times a week.
You can also cat the files to some other script like so:
cat files.txt | xargs -n 1 perl myperlscript.pl
Here I use the xargs command to take the files names from the cat and turn them into command line arguments in the perl script.
Commands used in this post: find, cat, sort, uniq, egrep, xargs, perl
No comments:
Post a Comment