Skip to content

Instantly share code, notes, and snippets.

@forestbaker
Last active November 24, 2015 19:29
Show Gist options
  • Save forestbaker/462a60e151def16d3e8e to your computer and use it in GitHub Desktop.
Save forestbaker/462a60e151def16d3e8e to your computer and use it in GitHub Desktop.
greptastic flavorites
# display a random number based on the number of lines in a file
echo $(($RANDOM%`grep -c '$' ~/.bash_history`))
# replace COMMAND to find all the libboost libraries it is using
lsof | grep 'COMMAND.*mem.*libboost'
# display contents of file without comments or empty lines, also removes "in-line" comments
# this is an example of a reason to NOT USE IN LINE COMMENTS
egrep -v '#|^$' /etc/mysql/my.cnf
# display file contents without leading comments or empty line, preserves "in line" comments
egrep -v '^#|^$' /etc/mysql/my.cnf
# display the content of multiple files, without empty lines, preserving "in line" comments
egrep -hv '^#|^$' /var/log/*.log
# output the total ram available
# Example: 8176 MB of RAM
echo "$(( $(egrep -o "MemTotal:[[:space:]]+[[:digit:]]+[[:space:]]" /proc/meminfo | awk '{print $2}') / 1000 )) MB of RAM"
# return total physical processors
grep "physical id" /proc/cpuinfo | sort -u | wc -l
# return cores per cpu
grep "cpu cores" /proc/cpuinfo |sort -u |cut -d":" -f2
# return total logical cores
grep -c "processor" /proc/cpuinfo
# Match valid IPv4 addresses
$ grep -E -o "(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" file.txt
# display all files in pwd and below that do not contain the pattern chewbacca
# output: ./dirname/filename
find . -type f \! -exec grep -q 'chewbacca' {} \; -print
# test command success after execution, instead of using $?
if $(grep -q 'chewbacca' file);
then
echo 'file does contain "chewbacca"'
else
echo 'file does not contain the "chewbacca"'
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment