← Knowledge Base
BashReference note1 min read · updated 2026-11-27

Text processing with sed, awk, cut, sort and uniq

Small commands that cover most log and CSV wrangling.

sedawkcuttext
sed 's/foo/bar/g' file                  # replace on every line
sed -i 's/^#Port 22/Port 2222/' conf    # edit the file in place (use -i.bak to keep a copy)
sed -n '10,20p' file                    # print lines 10 to 20
awk '{print $1, $NF}' file              # first and last column
awk -F: '{print $1}' /etc/passwd        # custom separator
awk '$3 > 100' data.txt                 # filter on a column
cut -d, -f2,4 data.csv                  # columns 2 and 4 of a CSV (simple CSV only)
sort file | uniq -c | sort -rn | head   # most frequent lines
tr 'a-z' 'A-Z' < file                   # translate characters
wc -l file                              # count lines

A typical log question: awk '{print $1}' access.log | sort | uniq -c | sort -rn | head lists the busiest client addresses.