Re: 1st line w/regex only

From: Walter Briscoe (wbriscoe_at_ponle.demon.co.uk)
Date: 08/07/03


Date: Thu, 7 Aug 2003 12:10:09 +0100


 In message <bgss32$kaq$1$830fa7a5@news.demon.co.uk> of Thu, 7 Aug 2003
07:36:56 in comp.unix.shell, Peter S Tillier
<peter_tillier@despammed.com> writes
>Alan Connor wrote:
>[...]
>> Worked like a CHARM, Peter! I use sed to replace "regex" with the
>> contents of a shell variable when the conditions of an "if" loop are
>> met, run the script, then switch it back to "regex" with sed...
>>
>> Here's a little tweak I had to do to get it work, and I know that
>> this is so obvious to you that it didn't seem worth mentioning, but it
>> might help my fellow sed newbies out:
>>
>> $ sed -n -f AlanC01.sed AlanC01.dat > tmpfile ; mv tmpfile
>> AlanC01.dat
>>
>
>Yes, sorry, should have mentioned that.
I am inclined to think the point need not have been made.
I think it is wrong in that it uses "-n" which was not in the original
posting and destroys the functionality. I find "-n" is rarely needed in
simple scripts.
C:\WINNT\Temp\1stscrap) sed -f AlanC01.sed AlanC01.dat
1
2
regex 2
3
4
regex 4
5
6

C:\WINNT\Temp\1stscrap) sed -n -f AlanC01.sed AlanC01.dat

C:\WINNT\Temp\1stscrap)

The data is put at slightly less risk with:
cp AlanC01.dat tmpfile && sed -f AlanC01.sed tmpfile > AlanC01.dat && rm tmpfile
I am not sure how "sed -f foo < bar" compares with "sed -f foo bar" in
terms of setting an exit status. YMMV. In Microsoft's command.com, the
former would not set an error status and the latter might!

>
>
>> 1 {
>> x
>> s/.*//
>> x
>> }
[snip]
Many will prefer Peter's tutorial form to my own; I am not going to
change; I reckon effort ought to be required to acquire understanding.

He solved the class of problem in a beautiful fashion. I would not have
posted my solutions to the particular problem if I had seen that work.

I am surprised by Peter's assumption that the hold space might not be
empty when sed starts. The point is not explicitly covered in
http://www.opengroup.org/onlinepubs/007904975/utilities/sed.html
I infer it can only be empty. Can someone point at an implementation
where Peter's assumption is necessary? Can anyone justify it?

I look forward to Alan's next question when he has the "sed and awk"
book ;-) I think sed is to awk as assembler is to C!

-- 
Walter Briscoe


Relevant Pages

  • Re: Print Section of file
    ... Specifically, FS *can* be a regex (if it's not a single character, ... then awk assumes it's a regex). ... regex (in fact it never is, unless the context requires a regex). ... a relational expression or a boolean combination of patterns... ...
    (comp.lang.awk)
  • Re: Using a regexp as field separator does not work!
    ... "field separator" if the regex '| *' is used as FS. ... alternation is used? ... just like an FS of a single blank character is a special case. ... could be optimized and awk doesn't try to analyze and warn you about any of them ...
    (comp.lang.awk)
  • Re: Using a regexp as field separator does not work!
    ... "field separator" if the regex '| *' is used as FS. ... alternation is used? ... just like an FS of a single blank character is a special case. ... could be optimized and awk doesn't try to analyze and warn you about any of them ...
    (comp.lang.awk)
  • Re: Using a regexp as field separator does not work!
    ... "field separator" if the regex '| *' is used as FS. ... alternation is used? ... just like an FS of a single blank character is a special case. ... could be optimized and awk doesn't try to analyze and warn you about any of them ...
    (comp.lang.awk)
  • Re: sed/regexp: remove empty lines
    ... without removing the empty lines the script runs fine. ... sed only supports BREs but if you switch to awk you can take advantage of EREs to combine the first 2 sed commands to this: ...
    (comp.unix.shell)