home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / unix / question / 15868 < prev    next >
Encoding:
Internet Message Format  |  1993-01-23  |  4.9 KB

  1. Xref: sparky comp.unix.questions:15868 comp.unix.shell:5477
  2. Path: sparky!uunet!mcsun!sunic!ugle.unit.no!flipper.pvv.unit.no!alkymi.unit.no!arnej
  3. From: arnej@alkymi.unit.no (Arne Henrik Juul)
  4. Newsgroups: comp.unix.questions,comp.unix.shell
  5. Subject: Re: Need help understanding sed
  6. Date: 23 Jan 93 01:57:25
  7. Organization: Norwegian Institute of Technology
  8. Lines: 132
  9. Message-ID: <ARNEJ.93Jan23015725@ild.alkymi.unit.no>
  10. References: <1993Jan22.140947@is.morgan.com> <1993Jan22.210855.29212@infolog.se>
  11. NNTP-Posting-Host: ild.alkymi.unit.no
  12. In-reply-to: mich@lin.infolog.se's message of Fri, 22 Jan 1993 21:08:55 GMT
  13.  
  14. In article <1993Jan22.210855.29212@infolog.se>
  15.  mich@lin.infolog.se (Thomas Michanek) writes:
  16.  
  17.  > From: mich@lin.infolog.se (Thomas Michanek)
  18.  
  19.  > I've started to use 'sed' a lot, not only for simple 's/.../.../'
  20.  > substitutions, but I have trouble understanding the man page for sed
  21.  > when trying to using more complex commands. Could someone help me
  22.  > with the following issues:
  23.  
  24. I'll try to help you a bit, but you also need to read the man page
  25. more intensely. Some things are best explained by example, though.
  26.  
  27.  > 1. The man page sais:
  28.  
  29.     Some commands use a hold space to save all or  part  of  the
  30.     pattern space for subsequent retrieval.
  31.  
  32.  >    However, I can't find _which_ commands do that and how. I've tried
  33.  >    using the g, G, h, H, x commands, but sed produces no output.
  34.  
  35. well, let's look at 'g' and 'h': g copies hold space into pattern
  36. space, h is vice versa. So
  37.    $ sed '/a/h;/b/g'
  38. Will `remember' lines containing the letter "a", and replace lines
  39. containing "b" with the last such `remembered' line.
  40.  
  41. input            output        comment
  42. groo            groo        
  43. b line                    hold space initially empty
  44. foo            foo
  45. alf            alf        remember this
  46. blue            alf        and copy it in
  47. glue            glue
  48. blue too        alf        and copy it in again
  49. new a line        new a line    remember
  50. newer a line        newer a line    remember
  51. b line            newer a line    copy hold space in
  52. both a and b        both a and b    remember, then copy
  53.  
  54.  
  55.  > 2. "\n" in a context address is supposed to match a NEWLINE in the
  56.  >    pattern space. But doesn't sed read one line at a time into the
  57.  >    pattern space, each line separated by NEWLINE?
  58.  
  59. Yes, you can't use this without using N, which does:
  60.     (2)N        Append the next line of  input  to  the  pattern
  61.                 space  with  an  embedded newline.  (The current
  62.                 line number changes.)
  63.  
  64.  
  65.  >    I want to look for files containing the pattern "A" in one line
  66.  >    and the pattern "B" in the next line. The following doesn't work:
  67.  >
  68.  >    sed '/A\nB/p'
  69.  >
  70.  >    How can you do such a 'multi-line grep'?
  71.  
  72.    $ sed -n 'N;/A.*\n.*B/p'
  73. will take two and two lines, and check if they together contain the regexp.
  74. So with input
  75.  hey
  76.  you
  77.  blArg
  78.  foo-BAR
  79.  and
  80.  Alpha
  81.  Beta
  82.  too!
  83. it will 'grep' out just
  84.  blArg
  85.  foo-Bar
  86. not the 'Alpha Beta' two-liner because of odd-even considerations. However
  87.    $ sed -n 'N;/A.*\n.*B/p;D'
  88. will find both, because it doesn't discard *all* of pattern space, just
  89. the first of the two lines we have got. The normal cycle is:
  90.      In normal operation sed cyclically copies a  line  of  input
  91.      into a pattern space (unless there is something left after a
  92.      D command), sequentially applies all commands with addresses
  93.      matching  that  pattern  space until reaching the end of the
  94.      script, copies the pattern  space  to  the  standard  output
  95.      (except under -n), and finally, deletes the pattern space.
  96. It's all in the man page, but it isn't easy :-)
  97.  
  98.  > 3. What's the difference between the commands n and p? The man page sais 
  99.  >    n copies the pattern space to stdout, but it has never worked for me.
  100.  
  101. with the -n option, it will *not* copy the pattern space to stdout.
  102. I think this is just a bug in the man page. But consider
  103.    $ sed '/^ok:/n;s/^/ok:/'
  104. which adds "ok:" to all lines which does not already have it.
  105.  
  106.  > 4. What's the syntax for the { } command grouping? No matter how I try
  107.  >    I get "Extra text at end of command" or "Unrecognized command".
  108.  >
  109.  >    Isn't sed '/A/{=;p}' supposed to print the line number and the pattern
  110.  >    on stdout? I've tried quoting, backslashing, spaces, no semicolon etc.
  111.  
  112. It's a bit more baroque than that. '{' is actually a *command*, and you
  113. would write in a sed script
  114. /A/{
  115. =
  116. p
  117. }
  118.  
  119. or in bourne/korn/etc. shell
  120.    $ sed -n '/A/{
  121.      =
  122.      p
  123.      }'
  124.  
  125. But you can say
  126.    $ sed -n '/A/{;=;p;}'
  127. which probably does what you want.
  128.  
  129.  > BTW, I'm using C shell on SunOS4.1.1 and "sed0.c 1.15 89/03/28 SMI".
  130.  
  131. For some things, like commands with embedded newlines, bourne-derived
  132. shells are much better (for interactive use) than c shell. I personally
  133. use and recommend GNU's Bourne Again SHell, or at least you should get
  134. tcsh.
  135.  
  136. All of my examples above were tested with GNU sed 1.13, some also with
  137. Sun's /usr/bin/sed, but they should work on most sed variants.
  138.  
  139.  > Your aid is urgent and welcome!!!
  140.  
  141. Hope this post aided you (and maybe others). I take no responsibility
  142. for typos :-)
  143.  
  144. --
  145. Arne H. Juul  --  arnej@lise.unit.no  -- University of Trondheim, Norway
  146.