Table of contents
Text Manipulation
Translate or delete characters
replace all occurrences of a colon with a newline character (split path)
echo $PATH | tr ':' '\n'
to upper case
tr "[a-f]" "[A-F]"
delete all occurrences of a single character
ls / | tr -d "t"
delete a range of characters
ls / | tr -d "[aeoui]"
ls / | tr -d "[a-d]"
extracting characters by index
echo "earth" | cut -c 2-4
echo "questionable" | cut -c 9-
deleting characters by index
echo "earth" | cut -c 2-4 --complement
reverse the word to take characters from the end
echo "earth" | rev | cut -c 1
add line numbers
echo -e "a\nb" | nl
h2 contains
contains() {
for word in $(echo "$1")
do
if [ "$word" = "$2" ]; then
echo "found $2!"
return 0
fi
done
echo "not found $2!"
return 1
}
regex
echo "abc" | grep -Eo '[a-c]+$'
expr "abc" : '[a-c]\+$'
numbers
zero pad numbers
printf "%05d\n" $i
sequence of zero padded numbers
seq -w [n]
Sorting
sort file contents
sort [file]
reverse sort
sort -r [file]
remove duplicate lines
sort -u [file]
sort on 6th column using field separator '/'
sort -t '/' -k '6'
Counting lines / words
count words in file (newlines, words, characters)
wc [file]
count newlines only
wc -l [file]
count characters only
wc -c [file]
determining the length of a string
len=$(expr length "$1")
len=$(echo -n "$str" | wc -c)
Column Layout
select whitespace separated column [n]
awk '{print $[n]}'
select column [n] from output with delimiter [d]
cut -d [d] -f [n]
align column based output
mount | column -t
date / time
unix timestamp
date +%s
date yyyy-mm-dd hh:mm:ss
date "+%Y-%m-%d %H:%M:%S"
date UTC yyyy-mm-dd hh:mm:ss
date -u "+%Y-%m-%d %H:%M:%S"
date --utc "+%Y-%m-%d %H:%M:%S"
date day of week short
date +%a
specifying another point in time
date -d tomorrow
date -d 'last week'