Eikonal Blog

2010.10.05

sed tricks

Filed under: scripting, transformers — Tags: , , — sandokan65 @ 15:58

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:

References


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/

5 Comments »

  1. Thank you for sharing these one liners. There are very useful.

    Like

    Comment by Santhosh — 2010.10.06 @ 12:18

  2. […] 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 – […]

    Like

    Pingback by Scripting languages « Eikonal Blog — 2011.09.30 @ 12:43

  3. […] | 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 – […]

    Like

    Pingback by awk « Eikonal Blog — 2011.09.30 @ 12:45

  4. […] | 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 – […]

    Like

    Pingback by Unix tricks « Eikonal Blog — 2012.03.14 @ 13:47


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.