These one-liners are collected from various sites and articles on web – see the list of Sources at the bottom of this posting.
- Deleting all empty lines from the input file:
sed ‘/^$/d’
- In-place replacement:
sed –i ‘/^$/d’ INPUTFILE
- In-place replacement with backup of original file:
sed –ibak ‘/^$/d’ INPUTFILE
- In-place deletion of all occurences of a string in a file:
sed –i ‘/WORDTOBEDELETED/d’
- How to replace the first occurrence only (of a string match) in a file, using sed
sed '0,/THISSTRING/s//TOTHATSTRING/' INPUTFILE
- Append environment variable PATH with sed:
sed -e '/^PATH/s/"$/:\/usr\/lib\/myprog\/bin"/g' -i /etc/environment
- Remove all whitespace from beinning of lines:
sed 's/^[ \t]*//g' foo
- Deleting the / from all html files contained in current folder:
sed -i ‘s/src=”\//src=”/g’ *.html
- Greedy matching:
% echo "foobar" | sed 's///g' bar
- Non greedy matching:
% echo "foobar" | sed 's/]*>//g' foobar
Sources:
- How do you delete empty lines using ‘sed’? – http://ksearch.wordpress.com/2010/09/25/delete-empty-lines-using-sed/
- How to replace the first occurrence only (of a string match) in a file, using sed – http://techteam.wordpress.com/2010/09/14/how-to-replace-the-first-occurrence-only-of-a-string-match-in-a-file-using-sed/
- Append environment variable PATH with sed (2010.09.09) – http://kdguntu.wordpress.com/2010/09/09/append-path/
- Unix tip #3: Introduction to Find, Grep, Sed – http://developmentality.wordpress.com/2010/09/07/unix-tip-3-introduction-to-find-grep-sed/
- util: sed for text parsing – http://zosim26.wordpress.com/2010/09/01/util-sed-for-text-parsing/
- To delete the / after the src From HTML Files – http://somepalli.wordpress.com/2010/08/05/to-delete-the-word-after-the-src-from-html-files/
- “sed – non greedy matching” by Christoph Sieghart (2008.07.08) – http://0x2a.at/b/sed–non-greedy-matching
References
- UNIX man pages : sed – http://unixhelp.ed.ac.uk/CGI/man-cgi?sed
- sed – http://www.opengroup.org/onlinepubs/007908799/xcu/sed.html
- “Sed – An Introduction and Tutorial” by Bruce Barnett – http://www.grymoire.com/Unix/Sed.html
- man sed (1posix) – stream editor – http://pwet.fr/man/linux/commandes/posix/sed
- Get better at awk/sed/grep – http://kristianrumberg.wordpress.com/2010/09/01/get-better-at-awksedgrep/
Related here: Command line based text replace – https://eikonal.wordpress.com/2010/07/13/command-line-based-text-replace/.
Related here: Scripting languages – https://eikonal.wordpress.com/2010/06/15/awk-sed/ | Unix tricks – https://eikonal.wordpress.com/2011/02/15/unix-tricks/ | SED tricks – https://eikonal.wordpress.com/2010/10/05/sed-tricks/ | Memory of things disappearing > nmap stuff > getports.awk – https://eikonal.wordpress.com/2010/06/23/memory-of-things-disappearing-nmap-stuff-getports-awk/ | AWK – https://eikonal.wordpress.com/2011/09/30/awk/
Thank you for sharing these one liners. There are very useful.
LikeLike
Comment by Santhosh — 2010.10.06 @ 12:18
[…] sed tricks – https://eikonal.wordpress.com/2010/10/05/sed-tricks/ […]
LikeLike
Pingback by Unix pages (at this blog) « Eikonal Blog — 2011.04.04 @ 15:46
[…] here: Unix tricks – https://eikonal.wordpress.com/2011/02/15/unix-tricks/ | SED tricks – https://eikonal.wordpress.com/2010/10/05/sed-tricks/ | Memory of things disappearing > nmap stuff > getports.awk – […]
LikeLike
Pingback by Scripting languages « Eikonal Blog — 2011.09.30 @ 12:43
[…] | Unix tricks – https://eikonal.wordpress.com/2011/02/15/unix-tricks/ | SED tricks – https://eikonal.wordpress.com/2010/10/05/sed-tricks/ | Memory of things disappearing > nmap stuff > getports.awk – […]
LikeLike
Pingback by awk « Eikonal Blog — 2011.09.30 @ 12:45
[…] | Unix tricks – https://eikonal.wordpress.com/2011/02/15/unix-tricks/ | SED tricks – https://eikonal.wordpress.com/2010/10/05/sed-tricks/ | Memory of things disappearing > nmap stuff > getports.awk – […]
LikeLike
Pingback by Unix tricks « Eikonal Blog — 2012.03.14 @ 13:47