# save stdin into a file# type in stdin and press Ctrl+Dcat > file.txt# show non-printable characterscat -A file.txt# show numbered linescat -n file.txt
sort
flags
f: ignore case
r: reverse order
n: numeric sort
k: for tabular data, based on given column
# sort stdin and save into a filesort > file.txt# sort multiple files and mergesort file1.txt file2.txt# sort multiple files and merge to save in a new filesort file1.txt file2.txt > merged.txt# sort by largest filedu -s /usr/share/* | sort -nr
uniq
must be used with sorted data
flags
i: ignore case
c: output duplicates with count
sort foo.txt | uniq
cut
extract section of text from line
flags
c: line ranges
f: for tabular data, based on given column
d: specify field delimiter (default is tab)
# print third column of tabular datacut -f 3 distros.txt# print lines 7 to 10cut -c 7-10 distros.txt
paste
add one or more columns of text files
# The final output will contain dates column first then columns from distro-versionspaste distro-dates.txt distro-versions.txt
join
performs join based on key on multiple tabular data
uses whitespace as delimiter
# both text files have first column as key which will be used to perform joinjoin distro-key-names.txt distro-key-vernums.txt
tac
reverse of cat
concatenates files in reverse order
# reverse chronological order of logs (latest first)tail /var/log/backup.log | tac
comm
compare files
comm file1.txt file2.txt
diff
compare files
more complex than comm
flags
c: context format
u: unified format
diff file1.txt file2.txt
patch
apply changes from diff
diff -Naur file1.txt file2.txt > patchfile.txt# applies patch to old file (file1.txt)# file1.txt will now match with file2.txtpatch < patchfile.txt
tr
Transliterate characters
letter to letter mapping
# convert lowercase to uppercase> echo "lowercase letters" | tr a-z A-ZLOWERCASE LETTERS# convert all chars to A> echo "lowercase letters" | tr [:lower:] AAAAAAAAAA AAAAAAA# remove repeated a or b> echo "aaabbbccc" | tr -s ababccc# perform ROT13 encoding> echo "secret text" | tr a-zA-Z n-za-mN-ZA-Mfrperg grkg
sed
Stream editor
started with commands
s/regexp/replacement/: substitution
y/set1/set2: transliteration
a: append
d: delete
i: insert
p: print
> echo "front" | sed 's/front/back/'back
awk
created by Bell Labs scientists
A – Alfred Aho (co-author of the classic compilers “Dragon Book”)
W – Peter Weinberger (known for his early work on UNIX databases)
K – Brian Kernighan (co-author of the first book on the C language, “K&R C”)
awk is a Turing complete language
Pattern scanning and processing language
# print specific columns> echo "John Doe 25" | awk '{print $1, $3}'John 25
aspell
interactive spelling checker
aspell check foo.txt
Text Formatting
nl
adds numbering to lines
nl distros.txt
fold
performs word wrap
# each line with width 12 chars> echo "The quick brown fox jumps over the lazy dog." | fold -w 12The quick brown fox jumps over the lazy dog.# don't break words> echo "The quick brown fox jumps over the lazy dog." | fold -w 12 -sThe quickbrown foxjumps overthe lazydog.
> printf "I formatted the string: %s\n" fooI formatted the string: foo# save result in variable> printf -v result "I formatted the string: %s\n" foo> echo "$result"I formatted the string: foo