ubuntu, updated

sed and awk

The sed and awk one-liners admins actually use: replace text in files, delete lines, and pick columns out of command output and logs.

Which one for what?

ToolThinks inUse it to
sedlinesreplace text, delete or insert lines, edit config files in place
awkcolumns (fields)pick columns, filter by a column’s value, sum and count

Both read a file (or a pipe), line by line, and print the result. They don’t change the file unless you use sed -i.

sed: replace and edit

The core command is s/OLD/NEW/:

WhatCommand
Replace the first hit per linesed 's/old/new/' file
Replace all hitssed 's/old/new/g' file
Change the file in placesed -i 's/old/new/g' file
In place, with a backup file.baksed -i.bak 's/old/new/g' file
Ignore casesed 's/old/new/gI' file
Paths: use another separatorsed 's#/var/www#/srv/www#g' file
Only in lines containing “server”sed '/server/s/80/8080/' file
Delete lines containing “debug”sed '/debug/d' file
Delete comments and empty linessed '/^#/d;/^$/d' file
Delete line 5 / lines 5 to 10sed '5d' file / sed '5,10d' file
Print only lines 10 to 20sed -n '10,20p' file
Insert a line before line 1sed -i '1i # managed by ansible' file
Append a line after a matchsed -i '/^\[Service\]/a Restart=always' file
Remove trailing spacessed -i 's/[[:space:]]*$//' file
Remove Windows line endingssed -i 's/\r$//' file

Change a setting in a config file:

sudo sed -i 's/^#\?PasswordAuthentication .*/PasswordAuthentication no/' /etc/ssh/sshd_config

^#\? matches the line whether it is commented out or not.

Tip: Run without -i first. sed prints the result to the screen and you see whether it does what you want.

awk: columns

awk splits every line into fields: $1 is the first column, $2 the second, $NF the last, $0 the whole line. By default it splits at spaces and tabs; -F sets another separator.

WhatCommand
Print column 1awk '{print $1}' file
Several columnsawk '{print $1, $3}' file
Last columnawk '{print $NF}' file
Other separator (:)awk -F: '{print $1}' /etc/passwd
Lines where column 3 > 1000awk -F: '$3 >= 1000 {print $1}' /etc/passwd
Lines matching a patternawk '/error/ {print $1, $2}' file
Skip the header lineawk 'NR > 1 {print $1}' file
Sum a columnawk '{sum += $5} END {print sum}' file
Count lines per valueawk '{c[$1]++} END {for (k in c) print c[k], k}' file
Format outputawk '{printf "%-20s %s\n", $1, $2}' file

Real-world one-liners

# Human users (UID >= 1000)
awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd

# Disk usage above 80 %
df -h | awk 'NR > 1 && $5+0 > 80 {print $6, $5}'

# Top 10 client IPs in an nginx access log
awk '{print $1}' /var/log/nginx/access.log | sort | uniq -c | sort -rn | head

# HTTP status codes and how often they occur
awk '{print $9}' /var/log/nginx/access.log | sort | uniq -c | sort -rn

# Total memory used by all nginx processes (in MB)
ps -C nginx -o rss= | awk '{s += $1} END {print s/1024 " MB"}'

# Config file without comments and empty lines
sed '/^[[:space:]]*#/d;/^[[:space:]]*$/d' /etc/ssh/sshd_config

# Replace a hostname in all configs of a folder
grep -rl "old-db" /etc/myapp/ | xargs sudo sed -i 's/old-db/new-db/g'

Need more than a one-liner? That’s the point to switch to a Bash script or Python.

§ home
~/knubertus/home/
1,1 Top