$ Linux Commands

Text Processing — Searching and manipulating text

awk Text Processing

Pattern scanning and processing language

awk 'PROGRAM' [FILE...]
awk '{print $1}' file.txt
awk -F: '{print $1}' /etc/passwd
awk '{sum += $1} END {print sum}' nums.txt
-F set field separator

$1, $2... refer to fields. $0 is the whole line. NR is the line number.

cut Text Processing

Remove sections from each line of input

cut [OPTIONS] [FILE...]
cut -d: -f1 /etc/passwd
cut -c1-10 file.txt
-d delimiter
-f field numbers
-c character positions
diff Text Processing

Compare files line by line

diff [OPTIONS] FILE1 FILE2
diff file1.txt file2.txt
diff -u old.py new.py
-u unified format (easier to read)
-r compare directories recursively
-i ignore case
grep Text Processing

Search text using patterns

grep [OPTIONS] PATTERN [FILE...]
grep "error" /var/log/syslog
grep -r "TODO" ./src
grep -i "warning" app.log
grep -n "def " app.py
-r recursive search
-i case-insensitive
-n show line numbers
-l only print filenames
-v invert match
-c count matches
-E extended regex
sed Text Processing

Stream editor for filtering and transforming text

sed [OPTIONS] SCRIPT [FILE...]
sed 's/foo/bar/g' file.txt
sed -i 's/old/new/g' file.txt
sed -n '5,10p' file.txt
-i edit file in-place
-n suppress default output
s/old/new/g substitute all occurrences

The -i flag modifies the file directly — always back up first or test without -i.

sort Text Processing

Sort lines of text

sort [OPTIONS] [FILE...]
sort file.txt
sort -r file.txt
sort -n numbers.txt
sort -u file.txt
-r reverse order
-n numeric sort
-u remove duplicates
-k N sort by field N
-t set field delimiter
wc Text Processing

Count lines, words, and characters

wc [OPTIONS] [FILE...]
wc -l file.txt
wc -w file.txt
grep "error" log.txt | wc -l
-l count lines
-w count words
-c count bytes
-m count characters