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?
| Tool | Thinks in | Use it to |
|---|---|---|
| sed | lines | replace text, delete or insert lines, edit config files in place |
| awk | columns (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/:
| What | Command |
|---|---|
| Replace the first hit per line | sed 's/old/new/' file |
| Replace all hits | sed 's/old/new/g' file |
| Change the file in place | sed -i 's/old/new/g' file |
In place, with a backup file.bak | sed -i.bak 's/old/new/g' file |
| Ignore case | sed 's/old/new/gI' file |
| Paths: use another separator | sed '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 lines | sed '/^#/d;/^$/d' file |
| Delete line 5 / lines 5 to 10 | sed '5d' file / sed '5,10d' file |
| Print only lines 10 to 20 | sed -n '10,20p' file |
| Insert a line before line 1 | sed -i '1i # managed by ansible' file |
| Append a line after a match | sed -i '/^\[Service\]/a Restart=always' file |
| Remove trailing spaces | sed -i 's/[[:space:]]*$//' file |
| Remove Windows line endings | sed -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
-ifirst. 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.
| What | Command |
|---|---|
| Print column 1 | awk '{print $1}' file |
| Several columns | awk '{print $1, $3}' file |
| Last column | awk '{print $NF}' file |
Other separator (:) | awk -F: '{print $1}' /etc/passwd |
| Lines where column 3 > 1000 | awk -F: '$3 >= 1000 {print $1}' /etc/passwd |
| Lines matching a pattern | awk '/error/ {print $1, $2}' file |
| Skip the header line | awk 'NR > 1 {print $1}' file |
| Sum a column | awk '{sum += $5} END {print sum}' file |
| Count lines per value | awk '{c[$1]++} END {for (k in c) print c[k], k}' file |
| Format output | awk '{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.