Useful commands#

Stream editor for filtering and transforming text#

Replace only the first Hello instance per line

$ echo "Hello World. Hello World." | sed 's/Hello/Bye/'
Bye World. Hello World.

Replace all Hello instances per line

$ echo "Hello World. Hello World." | sed 's/Hello/Bye/g'
Bye World. Bye World.

Replace only the second Hello instance per line

$ echo "Hello World. Hello World." | sed 's/Hello/Bye/2'
Bye World. Bye World.

Replace only the first Hello instance per line if the line contains the word World

$ echo "Hello World. Hello World." | sed '/World/s/Hello/Bye/'
Bye World. Hello World.

Show line 3 to 5 from file

$ sed -n 3,5p file.txt

Show all lines except 3 to 5 from file

$ sed -n 3,5d file.txt

Execute sed commands from script.sed

$ sed -f script.sed file.txt

Remove all trailing spaces from file with -i

$ sed -i 's/\s+$//' file.txt

Delete lines matching pattern

$ sed '/^#/d' /etc/services

Delete lines except matching pattern

$ sed '/^#/!d' /etc/services

remove sections from each line of files#

Displaying the 1st field of each line, using tab as the field separator

$ cut -f1 file.txt

Displaying the 2th field using : as the field separator

$ echo x:y:z | cut -d: -f2
y

Displaying the 1st and 3th field using ` ` as the field separator

$ echo x y z | cut -d" " -f1,3
x z

Displaying fields in a certain range using ` ` as the field separator

$ echo x y z a b c | cut -d" " -f1-3,5
x y z b

Display fields selected by the -f option aren’t reordered when printing it

$ echo x y z | cut -d" " -f3,2,1
x y z

Display fields from the 2th field and beyond

$ echo x y z | cut -d" " -f2-
y z

When no separators exist the command cut treats every character as a field

$ echo xyz | cut -f2,3
xyz

pattern scanning and processing language#

$ echo a  b | awk '{print $2}'
b
$ echo a  b | awk '{print $2 $1}'
ba

translate or delete characters#

$ echo "Hello World" | tr [a-z] [A-Z]
HELLO WORLD
$ echo "Hello World" | tr [:lower:] [:upper:]
HELLO WORLD
$ echo "Hello World" | tr -d ' '
HelloWorld
$ echo "Hello  World" | tr -d ' '
HelloWorld
$ echo "Hello  World" | tr -s ' '
Hello World
$ echo "Hello $UID"
Hello 1000
$ echo "Hello $UID" | tr " " "\n"
Hello
1000
$ echo "Hello $UID" | tr -cd "[:digit:]\n"
1000
$ echo "Hello $UID" | tr -d "a-zA-Z"
 1000
$ echo "Hello\n$UID" | tr "\n" " "
Hello 1000

sort lines of text files#

$ sort

Numerical value sort

$ sort -n

Reverse sort

$ sort -r

Random sort

$ sort -R

Sort by month

$ sort -M

Sort by specific column

$ sort -k 2

Sort and remove duplicates

$ sort -u

Ingore case while sorting

$ sort -f

Sort by human numeric value

$ sort -h

output the first part of files#

$ head

output the last part of files#

$ tail