18 July 2015

grep

Line numbering

grep -nr SEARCHTERM file1 file2 ...

last matching line number

grep -n pattern file.txt | cut -d : -f 1 | tail -1

Lines not containing a pattern

grep -v pattern file.txt

sed

sed q

Replace string1 by string2 in file

cp file file.bak
sed 's/string/string2/' file.bak > file
rm file.bak

tr

Translate characters into other characters

$ tr [options] set1 [set2]

Convert lower case to upper case

$ tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ  

Translate braces into parenthesis

$ tr '{}' '()' < inputfile > outputfile   

Translate white-space to tabs

$ echo "This is for testing" | tr [:space:] '\t'  

Squeeze repetition of characters using -s $ echo “This is for testing” | tr -s [:space:]

Delete specified characters using -d option

$ echo "the geek stuff" | tr -d 't'   

Complement the sets using -c option

$ echo "my username is 432234" | tr -cd [:digit:]   

Remove all non-printable character from a file

$ tr -cd [:print:] < file.txt   

Join all the lines in a file into a single line

$ tr -s '\n' ' ' < file.txt