I've been working with a lot of flat files lately and found these commands quite handy when it comes to searching for specific text patterns, replacing those matches with alternatives and copying/deleting or executing any other action on the output.
Tested on OS X and openSuSE, changes might be necessary for different flavors of Linux
_ Note: Some of the commands below ( as flagged by @serverhorror ) can exceed the maximum allowed number of arguments for a given machine:
getconf ARG_MAX resulting in the failure of execution. Use the below with care. _
Get the list of files with a matching pattern in a given directory
egrep [options] PATTERN [FILE...]
egrep -Rl 'IP445A' ./* -l, --files-with-matches Suppress normal output; instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match. -R, -r, --recursive Read all files under each directory, recursively; this is equiv-alent to the -d recurse option.
Get the number of matching files
Returns the number of files containing text matching the pattern.
egrep [options] PATTERN [FILE...] | wc -l -l The number of lines in each input file is written to the standard output.
egrep -Rl 'IP445A' ./* | wc -l
Store the names of each matching file into another text file
egrep [options] PATTERN [FILE...] > container.txt
egrep -Rl 'IP445A' ./* > list.txt
Copy files with a matching pattern to a new directory
cp `egrep [options] PATTERN [FILE...]` <New Directory>
cp `egrep -Rl "IP445A" ./*` ../matching/
The cp command will execute egrep first and copy all results to a new directory.
Note that egrep is wrapped with backticks and not single quotes.
Replace matching pattern with another string
grep -l '' <directory> | xargs sed -i "" 's/<regex>/<replacement>' -i extension Edit files in-place, saving backups with the specified extension. If a zero-length extension is given, no backup will be saved. It is not recommended to give a zero-length extension when in-place editing files, as you risk corruption or par-tial content in situations where disk space is exhausted, etc.
grep -l '' ./* | xargs sed -i "" 's/IP4445A/IP445A/'
Please refer to the man pages (in the sources section below) for detailed explanation of the parameters.
Read a list of files from a text file and copy each one to a new directory
#OSX use -I / for other Linux distros use -J cat list.txt | xargs -I % cp % <absolute path>
cat list.txt | xargs -I % cp % /Users/bassemd/newFolder
I recommend you go over the man pages for each command to have a deeper understanding of it and its diverse options