[Ilugc] sed - explanation required

  • From: akumar@xxxxxxxxxxxxx (Kumar Appaiah)
  • Date: Thu Aug 17 13:33:20 2006

On Wed, Aug 16, 2006 at 08:49:11PM -0700, Ashok Antony wrote:

hi all,

 # insert a blank line above every line which matches "regex"
 sed '/regex/{x;p;x;}'

what is the use of 'x' and 'p' there. could any one explain me better ?

From http://www.gnu.org/software/sed/manual/html_mono/sed.html#SEC8

`x'
    Exchange the contents of the hold and pattern spaces.
`p'
    Print out the pattern space (to the standard output). This command
    is usually only used in conjunction with the -n command-line option.
    [snip]

So, what this does is, moves a line matching the regex to the hold
space, and puts the contents of the hold space (which is blank) into
the pattern space. Then, it prints out the contents of the current
pattern space, which is the blank line. After this, it again exchanges
the pattern and hold spaces, which causes the line to return to the
pattern space. Finally, since the `-n' option was not specified, the
contents of the pattern space is printed.

I hope this explains it sufficiently. But to read the sed info. It is
an excellent document which will tell you about some awesome stuff
which can be done with sed very simply.

http://www.gnu.org/software/sed/manual/html_mono/sed.html

Kumar
-- 
Kumar Appaiah,
462, Jamuna Hostel,
Indian Institute of Technology Madras,
Chennai - 600 036
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : 
http://www.ae.iitm.ac.in/pipermail/ilugc/attachments/20060817/76c4407d/attachment.bin

Other related posts: