Grep one liners

From richud.com
Jump to: navigation, search


insert tab in grep search field

Do cntrl-v then cntrl-i to insert a tab

filter characters that break grep

If you are searching files that may contain 'odd' characters in them (e.g. Windows .inf driver files) these break grep. Basically it wont error or show anything is wrong but just wont return a match in the file. To get around this filter through tr first.

cat "*.inf" | tr -cd '\11\12\15\40-\176' | grep -Eir '[V][EI][ND]_[A-Z0-9]{4}&|=.*\*[A-Z0-9]{7,8}'

Replace old style PHP tags with newer syntax

find ./ -name "*.php" -print |  xargs -I {} sed -i 's/<?/<?PHP/g' {}
grep -lirh "<?PHPphp" * | xargs -I {} sed -i 's/<?PHPphp/<?PHP/g' {}


extract contents of pst files using libpst-0.6.52

/libpst-0.6.52/src/readpst -r backup.pst -o /media/mailpst
grep 10 lines each way of match (A and B are before and after separately)
grep -C10 -irHE "code" /media/mailpst/04_2002\ to\ 31_12_2008/Inbox/*


get device/vendor codes of hardware

sudo lspci -n | grep -Eio '[0-9A-Z]{4}:[0-9A-Z]{4}' > /tmp/pci
sudo hwinfo | grep "MODALIAS=acpi" | grep -vE "dev|PNP|LNX|INT"

Replace only LINE X matching

These are for third line matching only, /3 controls match line

all into pattern buffer

sed ':a N;$!ba; s/url =>[^\n]*/url => http:\/\/www.test.com/3' /tmp/automatic.conf

convert to string and back

cat /tmp/automatic.conf | tr "\n" "#" | sed "s/url =>[^#]*/url => http:\/\/www.test.com/3" | tr "#" "\n"