Some useful sed command

Remove all empty line:
sed '/^$/d'
In vim :
:g/^$/d
:v/./d

Remove or replace space and tabs:
sed 's/^[ \t]*//g'

Remove or replace all line return:
sed ':a;N;$!ba;s/\n//g'

Append a line after every line matching the pattern:
sed '/Sysadmin/a \ text' file

Insert Lines:
sed 'ADDRESS i\ text' file

Append at the end of line matching
sed 's/match.*/& text/' abcd.txt

Insert at the beginning:
sed 's/^/text/'

Append at the end:
sed 's/$/text/'

Remove the Ending char:
sed 's/.$//'

Remove ^M in vim :
:%s/^M//g
:%s/\r//g
With tr :
tr "\r" "\n"
And with sed :
sed -i -e 's/\r//g'

remove space at the beginning :
sed 's/^ *//g'

The following command deletes any trailing whitespace at the end of each line in vim :
%s/\s\+$//e

Sed direcly in a file
sed -i 's/STRING_TO_REPLACE/STRING_TO_REPLACE_IT/g' index.html

2020-07-11 21:02:23

Comments

Add a Comment

Login or Register to post a Comment.

Homepage