simon / sed

Last active 1 month ago

Revision 4cd1b2c7c6dd018fe37930e3751f457817735d60

sed.md Raw

sed

  • insert a line at the start of a file:

sed -i '1s/^/your text\n/' file

  • replace newline char:

sed ':a;N;$!ba;s/\n/NEWCHAR/g' filename

  • remove two adjacent lines (N; joins each line to the following line):

sed 'N;s/line 1\nline 2/replacement/g' filename

  • cut file between lines:

sed -n '3598627,3607135p' /var/log/syslog

  • insert a hash at the start of every line:

sed 's/^/#/' file.txt

  • and then remove the hash:

sed 's/^#//g' file.txt

  • insert a line after a match:

sed -i '/string_to_match.*/a string_to_insert' /file

  • match pattern and print including trailing three lines:

sed -n '/somestring/,+3 p' filename

  • match pattern and delete including trailing three lines:

sed '/somestring/,+3 d' filename