grep
- Extract lines with phrase "Silver rocket"
grep "Silver rocket" filename
- List names of files that contain the word "hummingbird"
grep -l hummingbird *.csv > filelist.txt &
- Use grep as a filter
ls -l | grep string
- Grep for word "unix", irrespective of case
grep -i unix *.html
- Select all lines containing xyz followed by any 2 numbers
grep 'xyz[0-9][0-9]' filename
- Retrieve all lines that begin with a space
grep '^ ' filename
- Retrieve all lines that do not begin with a space
grep '^[^ ]' filename
- Search directories recursively
grep -r string /directory/name
- Search directories recursively, looking only in *PD files
find /directory/name -name '*PD' -print | xargs grep string /dev/null
- Retrieve all lines that begin with a number
grep '^[1-9]' filename
- Retrieve lines with string and the lines above and below string
grep -C 2 'string' filename
- Retrieve lines containing string with a leading dash (-)
grep -e '-string' filename
- Retrieve lines containing both string1 and string2
/usr/xpg4/bin/grep -E 'string1|string2' tmp.out
- Have grep return a status code (0 or 1) for testing
if grep -s $1 $2 then echo "Found it" else echo "Did not find it" fi
- Test if file exists and is readable:
if test -r filename then grep $1 filename else echo "ERROR! Database file does not exist or is not readable". fi