Sed: Difference between revisions

From Hackepedia
Jump to navigationJump to search
mNo edit summary
mNo edit summary
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
== Introduction ==
== Introduction ==


Sed stands for stream editor and it does just that, it edits a stream.  There is many uses for it for example in the well known substitution mode.
Sed stands for stream editor and it does just that, it edits a stream of text.  There is many uses for it, for example in the well known substitution mode.  Sometimes [[tr]] does a job better than sed.


 
== substitution ==
== sed and substitution ==


Using the s allows the results to be substituted like so:
Using the s allows the results to be substituted like so:
Line 14: Line 13:
  bleep two three four bleep six seven eight nine
  bleep two three four bleep six seven eight nine


In a directory where files were backed up as "file-save" the renaming from the backup to the original file went like this:


== sed and deleting ==
ls *save| while read i ; do NEW=`echo $i | sed -e 's/-save//g'`; mv -f $i $NEW; done


If I wanted to delete lines 1 through 46 the syntax would be so:
== deleting ==
 
If I wanted to delete lines 1 through 46:


  francisco$ sed -e '1,46d' /etc/passwd   
  francisco$ sed -e '1,46d' /etc/passwd   
Line 23: Line 25:
  testuser:*:1001:1001:test:/usr/home/testuser:/bin/ksh
  testuser:*:1001:1001:test:/usr/home/testuser:/bin/ksh


You see the last two entries of a 48 line file.
You see the last two entries of a 48 line file.  This can also be achieved with [[head]].


== sed and printing certain rows ==
== displaying certain rows ==


Pretend you need to work on the first entry in the /etc/passwd file:
To see the first entry in the /etc/passwd file:


  francisco$ sed -n 1p /etc/passwd
  francisco$ sed -n 1p /etc/passwd
  root:*:0:0:Charlie &:/root:/bin/ksh
  root:*:0:0:Charlie &:/root:/bin/ksh
And this is how you display rows 2 through 5:
francisco$ sed -n 2,5p /etc/passwd
daemon:*:1:1:The devil himself:/root:/sbin/nologin
operator:*:2:5:System &:/operator:/sbin/nologin
bin:*:3:7:Binaries Commands and Source,,,:/:/sbin/nologin
smmsp:*:25:25:Sendmail Message Submission Program:/nonexistent:/sbin/nologin
== delimiters ==
pretend you see this:
sed -e 's=rat=/raven/=g' file
it looks difficult because you may not know that sed can use any delimiter between a substitute.
In the example given every instance of rat is changed to /raven/ with the slashes.  Here is
another one
sed -e s,/bin,/newbin,g file
Here the delimiter is a comma (,) and / is part of the substitute text.  This is totally ok.

Latest revision as of 21:48, 13 August 2008

Introduction

Sed stands for stream editor and it does just that, it edits a stream of text. There is many uses for it, for example in the well known substitution mode. Sometimes tr does a job better than sed.

substitution

Using the s allows the results to be substituted like so:

echo one two three four five six seven eight nine | sed -e 's/one/bleep/g' -e 's/five/bleep/g'

would result in:

bleep two three four bleep six seven eight nine

In a directory where files were backed up as "file-save" the renaming from the backup to the original file went like this:

ls *save| while read i ; do NEW=`echo $i | sed -e 's/-save//g'`; mv -f $i $NEW; done

deleting

If I wanted to delete lines 1 through 46:

francisco$ sed -e '1,46d' /etc/passwd   
_postfix:*:507:507:Postfix Daemon:/var/empty:/sbin/nologin
testuser:*:1001:1001:test:/usr/home/testuser:/bin/ksh

You see the last two entries of a 48 line file. This can also be achieved with head.

displaying certain rows

To see the first entry in the /etc/passwd file:

francisco$ sed -n 1p /etc/passwd
root:*:0:0:Charlie &:/root:/bin/ksh

And this is how you display rows 2 through 5:

francisco$ sed -n 2,5p /etc/passwd
daemon:*:1:1:The devil himself:/root:/sbin/nologin
operator:*:2:5:System &:/operator:/sbin/nologin
bin:*:3:7:Binaries Commands and Source,,,:/:/sbin/nologin
smmsp:*:25:25:Sendmail Message Submission Program:/nonexistent:/sbin/nologin

delimiters

pretend you see this:

sed -e 's=rat=/raven/=g' file

it looks difficult because you may not know that sed can use any delimiter between a substitute.

In the example given every instance of rat is changed to /raven/ with the slashes. Here is another one

sed -e s,/bin,/newbin,g file

Here the delimiter is a comma (,) and / is part of the substitute text. This is totally ok.