Table of Contents
1. Sed
Here is an example test.txt
file:
> cat test.txt
hello world
hello world
hello world
hello world
hello world
hello world
1.1. Insert / Append
# Insert before the second row
> sed '2i\haha' test.txt
hello world
haha
hello world
hello world
hello world
hello world
hello world
# Insert after the third row
> sed '3a\hahaha' test.txt
hello world
hello world
hello world
hahaha
hello world
hello world
hello world
# Insert before the last row
> sed '$i\hahahah' test.txt
hello world
hello world
hello world
hello world
hello world
hahahah
hello world
1.2. Delete
# Delete the context at second row
> sed '2d' test.txt
hello world
hello world
hello world
hello world
hello world
1.3. Replace
# Replace the first (by default) 'l' by 'a' for all rows (by default)
> sed 's/l/a/' test.txt
healo world
healo world
healo world
healo world
healo world
healo world
# Replace the second 'l' by 'x' for all rows
> sed 's/l/x/2' test.txt
helxo world
helxo world
helxo world
helxo world
helxo world
helxo world
# Replace all the matches of 'l' by 'x' for all rows
> sed 's/l/x/g' test.txt
hexxo worxd
hexxo worxd
hexxo worxd
hexxo worxd
hexxo worxd
hexxo worxd
# Repace the third 'l' by 'x' only for second row.
> sed '2s/l/x/3' test.txt
hello world
hello worxd
hello world
hello world
hello world
hello world
# Replace and modify the source files, all the 'l' by 'x' for the third row.
> sed -i '3s/l/x/g' test.txt
> cat test.txt
hello world
hello world
hexxo worxd
hello world
hello world
hello world
2. Awk
awk
is a powerful text analysis tool. The command is:
> awk [POSIX or GNU style options] [--] 'program' file ...
Let’s use an example file test.txt
:
> cat test.txt
hello world
hello world
hexxo worxd
hello world
heaao worad
hello world
2.1. Read different columns
The results of different columns are stored in $1
, $2
, $3
and etc. $0
stores the whole information.
> awk '{print $1}' test.txt
hello
hello
hexxo
hello
heaao
hello
> awk '{print $2}' test.txt
world
world
worxd
world
worad
world
> awk '{print $0}' test.txt
hello world
hello world
hexxo worxd
hello world
heaao worad
hello world
2.2. Use customized separator
# Use ":" to separate columns
> awk -F: '{print $1}' /etc/passwd
root
bin
daemon
adm
lp
sync
shutdown
halt
mail
operator
games
ftp
nobody
systemd-network
dbus
polkitd
sshd
postfix
chrony
nscd
tcpdump
2.3. Filter results by regular expressions
# Only show rows that contain "world"
> awk '/world/ {print $0}' test.txt
hello world
hello world
hello world
hello world
# Only shows rows that matches "world" for a specific column
# No results to match the first column
> awk '($1 ~ /world/) {print $0}' test.txt
> awk '($2 ~ /world/) {print $0}' test.txt
hello world
hello world
hello world
hello world
> awk '($2 ~ /wor[a-z]d/) {print $0}' test.txt
hello world
hello world
hexxo worxd
hello world
heaao worad
hello world