Useful commands#
print lines that match patterns#
Case-sensitive search
$ grep keyword file.txt
Case-insensitive search
$ grep -i keyword file.txt
Case-insensitive word search
$ grep -iw keyword file.txt
Inverse match
$ grep -v keyword file.txt
Search in all files recursively
$ grep -r keyword *
Display N lines after match
$ grep -A 2 keyword file.txt
Display N lines before match
$ grep -B 2 keyword file.txt
Display N lines around match
$ grep -C 2 keyword file.txt
Count the number of matches
$ grep -c keyword file.txt
Display only the file names containing matches
$ grep -l keyword *
Show only the matched string
$ grep -o keyword file.txt
Show the position of the matched string
$ grep -o -b keyword file.txt
Show the line number while showing the matching string
$ grep -n keyword file.txt
$ egrep
$ fgrep
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
print a sequence of numbers#
Generate a sequence of numbers from 1 to 5
$ seq 5
1
2
3
4
5
Generate a sequence of numbers from 3 to 5
$ seq 3 5
3
4
5
Generate a sequence of numbers from 3 to 5 with a customer integer value increment
$ seq 3 2 5
3
5
Generate a sequence of numbers from 3 to 5 with a customer decimal value increment
$ seq 3 0.5 5
3.0
3.5
4.0
4.5
5.0
The default increment is 1
$ seq 5 3
To generate a sequence of numbers from 5 to 3 the custom increment must be negative
$ seq 5 -1 3
5
4
3
Add leading zeros to make numbers the same size
$ seq -w 9 11
09
10
11
Specify the format of the output
$ seq -f '##%g##' 3 5
##3##
##4##
##5##
Set a string as separator
$ seq -s ':' 3 5
3:4:5