Skip to content

Instantly share code, notes, and snippets.

@alexdyukov
Created October 9, 2021 22:58
Show Gist options
  • Save alexdyukov/bd1e108423bb6bfd0435016a76e81672 to your computer and use it in GitHub Desktop.
Save alexdyukov/bd1e108423bb6bfd0435016a76e81672 to your computer and use it in GitHub Desktop.
text manipulation bash shortcuts
#replace 'string1' with 'string2'
sed 's/string1/string2/g'
#modify 'anystring1' to 'anystring2'
sed 's/\(.*\)1/\12/g'
#remove comments and blank lines
sed '/^ *#/d; /^ *$/d'
#remove trailing spaces from lines
sed 's/[ \t]*$//'
#strip multiple space to one
sed 's/[ \t]\+/ /g'
#convert dos end of lines to unix
tr -d '\r'
#convert unix end of lines to dos
sed 's/$'"/`echo \\\r`/"
#print 1000th line
sed -n '1000p'
#print lines 10 to 20
sed -n '10,20p'
#print file except first 10 lines
sed '1,10d'
#remove last line
sed '$d'
#print the line immediately before a regexp, but not the line containing the regexp
sed -n '/regexp/{g;1!p;};h'
#print the line immediately after a regexp, but not the line containing the regexp
sed -n '/regexp/{n;p;}'
#print only lines of >= 80 chars
sed -n '/^.\{80\}/p'
#print only lines of < 80 chars
sed -n '/^.\{80\}/!p'
#print section of file from regexp till the end
sed -n '/regexp/,$p'
#print section of file between two regexp
sed '/regexp1/,/regexp2/p'
#print file except of the section between two regexp
sed '/regexp1/,/regexp2/d'
#insert blank line above every line which matches regexp
sed '/regexp/{x;p;x;}'
#insert blank line below every line which matches regexp
sed '/regexp/G'
#insert blank line above and below every line which matches regex
sed '/regex/{x;p;x;G;}'
#print sum of the fields on each line
awk '{s=0; for (i=1;i<=NF;i++) s=s+i; print s}'
#print sum of the fields in file
awk '{for (i=1;i<=NF;i++) s=s+i};END{print s}'
#print last field of each line
awk '{print $NF}'
#remove extra spaces between fields
awk '{$1=$1;print}'
#switch first 2 fields of every line
awk '{tmp=$1; $1=$2; $2=tmp}'
#print reverse order the fields of every line
awk '{for (i=NF;i>0;i--) printf("%s ",$i); printf("\n")}'
#print only printable chars
tr -dc '[:print:]'
#soft IPv4 addresses
sort -t. -k1,1n -k2,2n -k3,3n -k4,4n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment