//Tips tagged grep
Take the '| sh' off the end before running to check the commands that will be run.
Latest tips by RSS
Click here to subscribe
Follow Shell-Fu on Twitter
Click here to follow
Follow Shell-Fu on identi.ca
Click here to follow
Place a filename at the beginning of the line to make it easier to edit the search at the end of the command.
$ </var/log/messages grep foo
$ </var/log/messages grep bar
$ </var/log/messages grep user1
Print a random shell-fu tip:
;-)
links -dump "http://www.shell-fu.org/lister.php?random" | grep -A 100 -- ----
;-)
Grepping for a process will return the grep command, this can be avoided by adding '| grep -v grep' to a command or easier in some cases altering the regular expression by adding brackets around a character.
The regular expression 'ss[h]' matches the literal string 'ssh' when it appears in the process list, but does not accidentally match the string 'ss[h]' when it appears in the process list as 'grep ss[h]'.
ps | grep 'ss[h]'
The regular expression 'ss[h]' matches the literal string 'ssh' when it appears in the process list, but does not accidentally match the string 'ss[h]' when it appears in the process list as 'grep ss[h]'.
Want to check the amount of used, free and total memory and swap from the command line? This script displays memory and swap information. Fully posix compliant and should work with all 2.[2-6].* kernels .
#fetch and process memory information
[ -f /proc/meminfo ] && {
Buffers=`grep -we 'Buffers' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
Cached=`grep -we 'Cached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemFree=`grep -ie 'MemFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
MemTotal=`grep -ie 'MemTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapCached=`grep -ie 'SwapCached' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapFree=`grep -ie 'SwapFree' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
SwapTotal=`grep -ie 'SwapTotal' /proc/meminfo | cut -d' ' -f2- | tr -d "[A-Z][a-z] "`
}
MEMUSED="$(( ( ( ( $MemTotal - $MemFree ) - $Cached ) - $Buffers ) / 1024 ))"
MEMTOTAL="$(( $MemTotal / 1024))"
MEMFREE="$(( $MEMTOTAL - $MEMUSED ))"
MEMPER="$(( ( $MEMUSED * 100 ) / $MEMTOTAL ))"
[ "$SwapTotal" -gt "1" ] && {
SWAPUSED="$(( ( ( $SwapTotal - $SwapFree ) - $SwapCached ) / 1024 ))"
SWAPTOTAL="$(( $SwapTotal / 1024))"
SWAPFREE="$(( $SWAPTOTAL - $SWAPUSED ))"
SWAPPER="$(( ( $SWAPUSED * 100 ) / $SWAPTOTAL ))"
} || {
SWAPUSED="0"
SWAPTOTAL="0"
SWAPPER="0"
}
# display the information
/bin/echo
/bin/echo "Memory"
/bin/echo "Used: $MEMUSED"
/bin/echo "Free: $MEMFREE"
/bin/echo "Total: $MEMTOTAL"
/bin/echo
/bin/echo "Swap"
/bin/echo "Used: $SWAPUSED"
/bin/echo "Free: $SWAPFREE"
/bin/echo "Total: $SWAPTOTAL"
/bin/echo
Find all files with given name (you can use Bash expansion if you'd like), and Grep for a phrase:
To display the filename that contained a match, use -print:
Or, use Grep options to print the filename and line number for each match:
The string `{}` is replaced by the current filename being processed everywhere it occurs in the arguments to the command. See the `find` man page for more information.
find . -name-exec grep "phrase" {} \;
To display the filename that contained a match, use -print:
find . -name-exec grep "phrase" {} \; -print
Or, use Grep options to print the filename and line number for each match:
find . -name-exec grep -Hn "phrase" {} \;
The string `{}` is replaced by the current filename being processed everywhere it occurs in the arguments to the command. See the `find` man page for more information.
It's easy to remove (or copy, move etc.) all files that match a given criteria, but harder to move all but ones excluded by a criteria.
To do this we can combine grep's -v option with Unix command substitution:
To do this we can combine grep's -v option with Unix command substitution:
$ ls 1.txt 2.txt 3.txt 4.txt $ rm `ls | grep -v 4\.txt` $ ls 4.txt
ps | grep processName | grep -v grep | awk '{print "kill -9 " $2}' | sh
Take the '| sh' off the end before running to check the commands that will be run.
lynx -dump http://www.spantz.org | grep -A999 "^References$" | tail -n +3 | awk '{print $2 }'
A simple shell script to get the latest stable version of the linux kernel:
#!/bin/bash
kernelV=`finger finger@kernel.org | grep 'stable version' | awk '{print $NF}'`
wget -c http://www.kernel.org/pub/linux/kernel/v2.6/linux-$kernelV.tar.bz2
If your version of grep supports coloring matches, you can use the following to give a calendar with the current date colored:
cal | grep --color=auto -E \(\ \|^\)$(date +%e)\(\ \|\$\)\|\$
Generate a random 8 character password containing a-z, A-Z and 0-9:
egrep -ioam1 '[a-z0-9]{8}' /dev/urandomuse `grep -o` to select only the pattern matched rather than the whole line:
grep -o 'GET [^ ]\+ ' /var/log/httpd_access.log | sort | uniq -c | sort -n | head -n 100 > top100
Grep file(s) and highlight the grep matches using less...
grep query *.php | less +/query
#!/bin/bash
# by dj.r4iden
echo "Your ip Address is" `lynx --source http://www.formyip.com/ |grep The | awk {'print $5'}`
# by dj.r4iden
echo "Your ip Address is" `lynx --source http://www.formyip.com/ |grep The | awk {'print $5'}`
Go to [http://www.bbc.co.uk/cgi-perl/weather/search/new_search.pl] and search for your location. Copy the link to the 3 day forecast feed, for example [http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0105.xml]. Then just put that link into the following command to get a quick command line weather forecast.
wget -q -O - http://feeds.bbc.co.uk/weather/.../XXXXX.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"
Example:
$ wget -q -O - http://feeds.bbc.co.uk/weather/feeds/rss/5day/world/0105.xml | grep title | sed -e "s/<[^>]*>//g" -e "s/°//g" | egrep "^[A-Z]"
BBC - Weather Centre - Forecast for San Francisco, United States of America Tuesday: sunny, Max Temp: 22C (72F), Min Temp: 13C (55F) Wednesday: sunny, Max Temp: 22C (72F), Min Temp: 15C (59F) Thursday: sunny, Max Temp: 25C (77F), Min Temp: 14C (57F)
I've often seen people using the find command on a directory to get all the files, then using these with grep to find files containing a particular word. This can however, be done better just using grep:
greps for occurances of 'search_for' in the specified directory and below, or the current directory if none specified.
Options:
-i case insensitive
-n shows line number
-H shows file name
-R recursive
grep --color=auto -inHR "search_for" /in/dir
greps for occurances of 'search_for' in the specified directory and below, or the current directory if none specified.
Options:
-i case insensitive
-n shows line number
-H shows file name
-R recursive
Displays a random xkcd comic. Requires ImageMagick.
wget http://dynamic.xkcd.com/comic/random/ -O -| grep <img src="http://imgs.xkcd.com/comics | sed s/<img src="// | sed s/"[a-z]*.*// | wget -i - -O -| display
Mail somebody about space running low in some path (ksh, bash):
PATHS="/export/home /home"
AWK=/usr/bin/awk
DU="/usr/bin/du -ks"
GREP=/usr/bin/grep
DF="/usr/bin/df -k"
TR=/usr/bin/tr
SED=/usr/bin/sed
CAT=/usr/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mailx
mailto="all@company.com"
for path in $PATHS
do
DISK_AVAIL=`$DF $path | $GREP -v "Filesystem" | $AWK '{print $5}'|$SED 's/%//g'`
if [ $DISK_AVAIL -gt 90 ];then
echo "Please clean up your stuff\n\n" > $MAILFILE
$CAT $MAILFILE | $MAILER -s "Clean up stuff" $mailto
fi
done
I use the following to list non-system users. It should be portable though won't work on systems without the getent command.
alias lsusers='getent passwd | tr ":" " " | awk "\$3 >= $(grep UID_MIN /etc/login.defs | cut -d " " -f 2) { print \$1 }" | sort'example, look for all the TODO and HACK strings I left in large java project and show a bit of context before and after using the -C switch of grep.
find path_to_start | grep \\.java | xargs egrep -C3 "TODO|HACK" | less
A slight improvement to #64 -- drops one grep and adds line numbers
find _path_to_start_ -name '*.php' | xargs egrep -nC3 'TODO|HACK' | less
My little "iso2cd" alias. Not clean, but handy. The Burning device will be auto detected.
example call:
iso2cd debian_lenny_final.iso
alias iso2cd="cdrecord -s dev=`cdrecord --devices 2>&1 | grep "\(rw\|dev=\)" | awk {'print $2'} | cut -f'2' -d'=' | head -n1` gracetime=1 driveropts=burnfree -dao -overburn -v"
example call:
iso2cd debian_lenny_final.iso
alias iso2cd="cdrecord -s dev=`cdrecord --devices 2>&1 | grep "\(rw\|dev=\)" | awk {'print $2'} | cut -f'2' -d'=' | head -n1` gracetime=1 driveropts=burnfree -dao -overburn -v"

