Difference between revisions of "Sed"

From Linuxintro
imported>ThorstenStaerk
(New page: sed: Replace strings sed -e "s/cgi?\([0-9][0-9]*\)/cgi@\1.html/g" myfile.html > index.html)
 
imported>ThorstenStaerk
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
sed: Replace strings
+
[[sed]] is a [[command]] to edit a text stream in [http://en.wikipedia.org/wiki/Batch_mode batch mode].
  sed -e "s/cgi?\([0-9][0-9]*\)/cgi@\1.html/g" myfile.html > index.html
+
 
 +
For example,
 +
  sed "s/a/o/"
 +
Will read your input (stream) from the keyboard and '''s'''ubstitute every '''a''' by an '''o'''.
 +
 
 +
= Usecases =
 +
* Remove leading white space
 +
sed 's/^[ \t]*//'
 +
* Replace strings
 +
# echo "hello world" | sed "<abbr title="substitute">s</abbr>/world/moon/"
 +
hello moon
 +
* Insert a line a beginning of file
 +
sed -i '1i <This is now at the first line>' <filename>
 +
* Replace several newlines by one
 +
sed 's/\ \{1,\}/\ /g'
 +
* change the protocol for a given port in /etc/services:
 +
sed -ri "s/.{16}3200/sapdp00 3200/" /etc/services
 +
 
 +
= Limitations =
 +
You cannot use sed to delete linebreaks, for this you must use tr.
 +
 
 +
= See also =
 +
* [[Bash Skripting Tutorial]]
 +
* [[piping]]
 +
* [[grep]]
 +
* [[regex]]

Latest revision as of 19:31, 7 February 2013

sed is a command to edit a text stream in batch mode.

For example,

sed "s/a/o/"

Will read your input (stream) from the keyboard and substitute every a by an o.

Usecases

  • Remove leading white space
sed 's/^[ \t]*//'
  • Replace strings
# echo "hello world" | sed "s/world/moon/"
hello moon
  • Insert a line a beginning of file
sed -i '1i <This is now at the first line>' <filename>
  • Replace several newlines by one
sed 's/\ \{1,\}/\ /g'
  • change the protocol for a given port in /etc/services:
sed -ri "s/.{16}3200/sapdp00 3200/" /etc/services

Limitations

You cannot use sed to delete linebreaks, for this you must use tr.

See also