LinuxCommandLibrary
GitHubF-DroidGoogle Play Store

Text Processing

Substitute Text

sed applies editing commands to each line. The substitute command s/old/new/ replaces the first match per line; g replaces all matches, I ignores case. -i edits the file in place instead of printing the result.
$ sed 's/old/new/g' [file]
copy
$ sed -i 's/old/new/g' [file]
copy
$ sed -i 's/old/new/gI' [file]
copy
sd does the same job with simpler, regex-by-default syntax.
$ sd "old" "new" [file]
copy
Any character can delimit the s command: s|/usr/bin|/usr/local/bin| avoids escaping slashes in paths.

Delete or Print Specific Lines

d deletes matching lines; -n with p prints only selected lines. Addresses can be patterns, line numbers, or ranges.
$ sed '/pattern/d' [file]
copy
$ sed -i '/^$/d' [file]
copy
$ sed -n '5,10p' [file]
copy
$ sed -n '/pattern/p' [file]
copy

Extract Fields

awk splits every line into fields: $1 is the first field, $0 the whole line, NR the line number. -F changes the field separator from whitespace to anything else.
$ awk '{print $1}' [file]
copy
$ awk -F: '{print $1, $3}' /etc/passwd
copy
$ awk '{print NR, $0}' [file]
copy
cut is the lightweight alternative for simple column extraction, by delimiter (-d, -f) or character position (-c).
$ cut -d: -f1 [file]
copy
$ cut -d',' -f1,3 [file]
copy
$ cut -c1-10 [file]
copy

Filter with Conditions

An awk program is condition { action }: lines matching the condition run the action (default: print the line).
$ awk '$3 > 100' [file]
copy
$ awk '/pattern/ {print $2}' [file]
copy
$ awk 'NR>=5 && NR<=10' [file]
copy
Aggregate across lines with variables and an END block.
$ awk '{sum += $1} END {print sum}' [file]
copy
$ awk '{sum += $1} END {print sum/NR}' [file]
copy

Sort Lines

Alphabetical by default; -n sorts numerically, -r reverses, -u drops duplicates, -t and -k sort by a specific field. -h understands human-readable sizes like 2K and 1G.
$ sort [file]
copy
$ sort -n [file]
copy
$ sort -r [file]
copy
$ sort -t: -k3 -n [file]
copy
$ sort -u [file]
copy

Find Duplicate Lines

uniq only compares neighboring lines, so sort first. -c counts occurrences, -d shows only duplicated lines, -u only unique ones.
$ sort [file] | uniq
copy
$ sort [file] | uniq -c | sort -rn
copy
$ sort [file] | uniq -d
copy
The sort | uniq -c | sort -rn pipeline is the classic frequency counter: it ranks every distinct line by how often it occurs.

Translate or Delete Characters

tr maps characters from one set to another, -d deletes them, -s squeezes repeats into one.
$ tr 'a-z' 'A-Z' < [file]
copy
$ tr -d '[:digit:]' < [file]
copy
$ tr -s ' ' < [file]
copy
$ tr '\n' ' ' < [file]
copy

Compare Files

diff -u is the standard patch-style comparison; -y shows files side by side; cmp compares bytes and is ideal for binary files.
$ diff -u [file1] [file2]
copy
$ diff -y [file1] [file2]
copy
$ cmp [file1] [file2]
copy
comm shows lines unique to each sorted file and lines they share, in three columns. Suppress columns by number.
$ comm [file1] [file2]
copy
$ comm -12 [file1] [file2]
copy
Both comm and join require their input files to be sorted.

Combine Files

paste glues files together line by line; -s joins all lines of one file into a single line. join matches lines from two files on a common field, like a database join.
$ paste [file1] [file2]
copy
$ paste -d',' [file1] [file2]
copy
$ paste -s [file]
copy
$ join [file1] [file2]
copy
$ join -t: -1 1 -2 3 [file1] [file2]
copy

Format Text

Align fields into a table, or wrap long lines. fmt rewraps paragraphs intelligently, fold cuts hard at the width (-s breaks at spaces).
$ column -t [file]
copy
$ column -t -s',' [file]
copy
$ fmt -w 80 [file]
copy
$ fold -s -w 80 [file]
copy

Number Lines

nl numbers only non-empty lines by default; -ba numbers all lines, like cat -n.
$ nl [file]
copy
$ nl -ba [file]
copy
$ cat -n [file]
copy

Count

Lines, words, and bytes; -m counts characters in multi-byte encodings.
$ wc [file]
copy
$ wc -l [file]
copy
$ wc -w [file]
copy
$ wc -c [file]
copy

Reverse and Shuffle

tac prints a file last line first, rev reverses each line's characters, shuf randomizes line order.
$ tac [file]
copy
$ rev [file]
copy
$ shuf [file]
copy
$ shuf -n 1 [file]
copy
Copied to clipboard
Kai