Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
README.org
cheatsheet-sed-A4.pdf
my-file

README.org

1 Sed CheatSheet

linkedin
github
slack


PRs Welcome

File me Issues or star this repo.

1.1 Advanced sed

NameCommand
Update with auto backupsed -i.bak 's/localhost/myhost/g' my-file, ls my-file*, Link: my-file
Use variables in sednewip=’127.0.1.1’; sed -i "s/127.0.0.1/$newip/g" my-file Link: unix.com
Only replace the first matchsed -i '0,/localhost/{s/localhost/newstring/}' my-file Link: stackoverflow
Replace multiple linessed -i ':a;N;$!ba;s/Host.*localhost//g' my-file Link: stackexchange
Add a new line to 3th linesed -i "3i mynew string" my-file
Insert a line of text before a linesed -i '/KUBELET_NETWORK_ARGS/i newline' my-file

1.2 GNUS sed - delete

NameCommand
Replace stringsed -i 's/127.0.0.1/127.0.1.1/g' my-file Link: my-file
Use a different seperatorsed -i 's#127.0.0.1#127.0.1.1#g' my-file
Use a different seperatorsed -i 's#https://www.test.com/test#http://www.try.com#g' my-file
Delete patternsed '/KUBELET_NETWORK_ARGS.*/d' my-file
Delete matched linessed -i 's/.*KUBELET_NETWORK_ARGS.*//g' my-file
Delete whitespacesed -i 's/ //g' my-file
Delete empty linessed -i '/^$/d' my-file
Delete 2nd to 4th linessed -i '2,4d' my-file
Delete leading whitespacesed -i 's/^[ \t]*//' my-file
Delete trailing whitespacesed -i 's/[ \t]*$//' my-file

1.3 GNUS sed - insertion

NameCommand
Insert string to the begining of linessed -i 's/^/head /g' my-file
Insert string to the end of linessed -i 's/$/ tail/g' my-file
Add content after nth linesed -n -i 'p;3a "new string"' my-file, cat -n my-file Link: unix.com
Add a new line to 3th linesed -i "3i mynew string" my-file
Insert a line of text before a linesed -i '/KUBELET_NETWORK_ARGS/i newline' my-file

1.4 GNUS sed - review

NameCommand
Print lines from 2nd to 6thsed -n '2,6p' my-file

1.5 Freebsd sed on Mac

NameCommand
In-place sedsed -i "" 's/127.0.0.1/127.0.1.1/g' my-file, link:stackoverflow

1.6 More Resources

License: Code is licensed under MIT License.

http://sed.sourceforge.net/sed1line.txt

https://thornelabs.net/2016/02/01/sed-commands-cheat-sheet.html

http://www.grymoire.com/Unix/Sed.html