← Cheat Sheets
Bash Text Processing
grep, sed, awk and friends for working with text.
# Bash Text Processing
grep, sed, awk and friends for working with text.
```bash
grep -rn "text" . # search recursively with line numbers
grep -i "text" file # ignore case
grep -v "text" file # lines NOT matching
grep -E "a|b" file # extended regex
sed 's/old/new/g' file # replace all on each line
sed -n '5,10p' file # print lines 5-10
awk '{print $1}' file # first column
awk -F, '{print $2}' f.csv # CSV second column
cut -d, -f1,3 file.csv # fields 1 and 3
sort -n file # numeric sort
sort file | uniq -c # count duplicates
wc -l file # count lines
tr 'a-z' 'A-Z' < file # to uppercase
diff a.txt b.txt # compare files
xargs -n1 cmd # run cmd for each input line
```
On macOS, in-place edits need an empty suffix: `sed -i '' 's/a/b/' file`.