Skip to content

Instantly share code, notes, and snippets.

@auycro
Last active April 25, 2022 09:29
Show Gist options
  • Save auycro/f38ff50ab52c1c7e06b0afb30dd6ded6 to your computer and use it in GitHub Desktop.
Save auycro/f38ff50ab52c1c7e06b0afb30dd6ded6 to your computer and use it in GitHub Desktop.
bash memo
#!/bin/bash
## TEXT MANIPULATION ##
### CUT ###
# print the fields from second fields to last.
echo 'a\tb\tc' | cut -d$'\t' -f 2-
# b c
# print the fields from second fields to last.
echo 'a b c d e f g h i' | cut -d ' ' -f 2-5
# b c d e
# print 2nd and 4th character
echo 'Hello' | cut -c '2,4'
# el
### TR ###
# replace all sequences of multiple spaces with just one space.
echo 'Worl d' | tr -s ' ' ' '
# Wor d
# delete except only character
echo 'abacaba' | tr -dc 'a'
# aaaa%
# exclude a character
echo 'abacaba' | tr -d 'a'
# bcb
### SORT ###
# Output the text file with the lines reordered in reverse lexicographical order
sort -r
# Sort the data in ascending order of the second column as number
sort -t $'\t' -k 2 -nsort -t $'\t' -k 2 -n
### UNIQ ###
# count the number of times each line repeats itself
uniq -c tmp.txt
# 2 00
# 2 01
# 2 00
# count the number of times each line repeats itself with CASE INSENSITIVE
uniq -c -i | sed 's/^\s*//g'
# only print unique lines
# A00
# a00
# 01
# 01
# 00
# 00
# 02
# 02
# 03
# aa
# aa
# aa
uniq -u tmp.txt
# A00
# a00
# 03
### PASTE ###
# Join all lines
# 00
# 00
# 01
paste -s tmp.txt
# 00 00 01
# rows are folded into one line with ';' separated
paste -s -d ';' tmp.txt
# 00;00;01%
# three consecutive rows are folded into one line
paste -d ',' - - - < capital.txt
# merge two file with \t separated
paste -d '\t' state.txt capital.txt
## ARRAY ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment