UNIX basic1
Regex basics
*: zero or more+: one or more?: zero or one.: any single character[^ ]: except^: start$: end{n}:nrepetitions{n,m}:ntomrepetitions{,m}:mor fewer repetitions
grep
grep -i: ignore upper/lower casegrep -v: invert / not matchgrep -c: count of matching linesgrep -w: whole word only
wc
wc: word counterwc -c: characterswc -w: wordswc -l: lines- Default output includes lines, words, and characters
tr
tr sourceChars destCharstr 'A-Z' 'a-z'tr -d '0-9': delete0-9tr aeiou 12333: mapsaeiouto12333
head / tail
head/tail: default 10 lineshead -n {number}/tail -n {number}- Combine them:
head -n 100 | tail -n 20
Character set example
[a]=a
cut
cut -f: tab-separated fieldscut -c: characterscut -d: custom delimiter
Examples:
cut -f1,4
cut -d'|' -f1-3
cut -c1-5
sort
sort -r: descending ordersort -n: numeric sortsort -d: dictionary ordersort -t: delimitersort -k: column/key
Example:
sort -nr -k3
Meaning: sort by the 3rd column in descending numeric order.
uniq
uniq -c: count duplicated linesuniq -d: print one copy of duplicated linesuniq -u: print lines that occur only once
Regex examples
[ab]+ -> a, b, aa, abb
(ab)+ -> ab, abab, ababab
To update later
- Add small input/output examples for each command
- Add notes on when to combine
sort | uniq