Saturday 9 June 2012

SED command examples


PURPOSE
Learn SED command by examples

ENVIRONMENT
LINUX: shell/bash
WINDOWS:cygwin

EXAMPLES


  • we are going to work with the following file

$ cat file2
1. one
2. two
3. three
4. four
5. five
  • replacing "one" by "OOO"...instead of "OOO" if you put "" it deletes "one"

$ sed s/one/OOO/ file2
1. OOO
2. two
3. three
4. four
5. five

  • same as above multiple replace ( "one" by "OOONE" and "two" by "TWOOO")

$ sed -e s/one/OOONE/ -e s/two/TWOOO/ file2
1. OOONE
2. TWOOO
3. three
4. four
5. five
  • replace character by character(here "o" by "O" and "t" by "T")

$ sed 'y/ot/OT/' file2
1. One
2. TwO
3. Three
4. fOur
5. five
  • putting matches in a file and then running sed
$ cat matches
s/one/1111/
s/two/TTTT/


$ sed -f matches file2
1. 1111
2. TTTT
3. three
4. four
5. five








Finding pattern by line number or word
  • syntax: (in the following syntaxes X、Y are numbers and <word> is a word)


    • ①sed -n Xp filename  : prints line X of file
    • ②sed -n /<word>/p filename: prints line where <word>
    • ③sed -n 'X , Yp' filename : prints file from line N to line Y 
    • ④sed -n 'X ~ Y'p filename  : prints every Y number of line after line X
    • ⑤sed -n 'X , /<word>/p'  filename : prints file from line X to <word>
    • ⑥sed -n '/<word>/, Yp' filename : prints file from <word> to line Y
    • ⑦sed -n '/<word>/, +Yp' filename : prints from <word> and adds Y lines number
    • ⑧sed -n '/<wordA>/ , /<wordB>/p' filename : prints file from <wordA> to <wordB>
※ without the single quotes it mostly works,just try it yourself
  • ①  prints line 3
$ sed -n 3p file2
3. three


  • ② prints line containing the word "two"
$ sed -n /two/p file2
2. two
  • ③ prints line 3 to 4
$ sed -n 3,4p file2
3. three
4. four
  • ④ prints every 2 lines after the 1st line
$ sed -n '1~2'p file2
1. one
3. three
5. five

  • ⑤ prints line 3 to the line where "four" is found
$ sed -n '3,/four/p' file2
3. three
4. four


  • ⑥ prints the line where "two" is found up until the 4th line
$ sed -n '/two/,4p' file2
2. two
3. three
4. four

  • ⑦ prints 3lines after the matching the searched word "one"
$ sed -n '/one/,+3p' file2
1. one
2. two
3. three
4. four

  • ⑧ prints everything between line where "two" is found to line where "four" is found
$ sed -n /two/,/four/p file2
2. two
3. three
4. four


WE can use regex with SED too

  • erase "four" and delete line containing the word "six"
$ sed -e 's/four//;/six/d' file2 ( same as sed -e s/four// -e s/six// file2)
1. one
2. two
3. three
4.
5. five
  • find a word which start with "o" and ends with "e" and replace by 3 times the match
$ sed 's/o\(.*\)e/\1\1\1/g' file2
1. nnn
2. two
3. three
4. four
5. five
  • & represents the match of sed so lets replace "one" by "<one-one-one>"
$ sed 's/one/<&-&-&>/' file2
1. <one-one-one>
2. two
3. three
4. four
5. five
/div>
  • using other delimes than "/"
$ echo "http://www.google.com/" | sed @http://www.google.com/@http://www.youtube.com/@
http://www.youtube.comsomelinks/


  • writing changes directly to file(replacing "one" by "1111" directly in the target file)

$cp file2 file3    ( lets copy file2 into another file to not loose the original)
$sed -i s/one/1111/ file3
$ cat file3
1. 1111
2. two
3. three
4. four

5. five

  • replacing options "g" and "i"

  • the following replaces only the 1st match
$ echo "oneONEone" | sed s/one/111/
111ONEone

  • by adding the "g" option it replaces the match "Globally"
$ echo "oneONEone" | sed s/one/111/g
111ONE111

  • by adding the "i" option it replaces the match "Ignoring " the case
$ echo "oneONEone" | sed s/one/111/gi
111111111



No comments:

Post a Comment