awk vs. sed question 
Author Message
 awk vs. sed question

When I execute 'awk -f sample.awk input' where sample.awk looks like:

/cow|pig/       {
                        print $0 "(found)"
                        next
                }
/.*/            {
                        print
                }

and input looks like:
dog
cat
pig
cow

my output (as expected) looks like:
dog
cat
pig(found)
cow(found)

Now, here is my question. Can someone explain how this works using sed.
I have tried 'sed -f sample.sed input' where sample.sed is:

/cow|pig/s/$/ (found)/

I have been able to get sed to work with the following:

/[cp][oi][wg]/s/$/(found)/

but 'pow', 'pog' and a few others would test positive as well. Any ideas
would be greatly appreciated.

--
Don Caldwell



Mon, 08 Feb 1999 03:00:00 GMT  
 awk vs. sed question

Quote:

> When I execute 'awk -f sample.awk input' where sample.awk looks like:

> /cow|pig/       {
>                         print $0 "(found)"
>                         next
>                 }
> /.*/            {
>                         print
>                 }

> and input looks like:
> dog
> cat
> pig
> cow

> my output (as expected) looks like:
> dog
> cat
> pig(found)
> cow(found)

> Now, here is my question. Can someone explain how this works using sed.
> I have tried 'sed -f sample.sed input' where sample.sed is:

> /cow|pig/s/$/ (found)/

> I have been able to get sed to work with the following:

> /[cp][oi][wg]/s/$/(found)/

> but 'pow', 'pog' and a few others would test positive as well. Any ideas
> would be greatly appreciated.

You need to employ some of the programmatic features of "sed" instead of
just the search-and-replace basics with which most shell programmers are
acquainted. A simplistic way would be to have a "substitute-and-return"
clause in addition to a plain substitute clause, i.e.

sed '/cow/{
        s/$/ (found)/
        b
     }
     /pig/s/$/ (found)/'

This is a little sub-optimal, however, because the substitute command is
repeated in the script. Slightly more cryptic, but more elegant and
efficient, is to have a single substitute command, and use the
"branch-to-label" feature:

sed '/cow/b found
     /pig/!b
     : found
     s/$/ (found)/'

                                                - Kevin



Mon, 08 Feb 1999 03:00:00 GMT  
 awk vs. sed question



Quote:

>When I execute 'awk -f sample.awk input' where sample.awk looks like:

>/cow|pig/      {

> ...

>/cow|pig/s/$/ (found)/

>I have been able to get sed to work with the following:

>/[cp][oi][wg]/s/$/(found)/

>but 'pow', 'pog' and a few others would test positive as well. Any ideas
>would be greatly appreciated.

>--
>Don Caldwell

Try:

sed -e "s/cow/cow(found)/" -e "s/pig/pig(found)/"

Sed can't combine regular expressions in the same way as awk (or egrep).
--
Adam Lovick



Tue, 09 Feb 1999 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie awk (sed??) question, regular expressions

2. Awk/Sed Filehandler question

3. A very simple question on SED or AWK for a GURU, and an enjoyable problem

4. awk or sed: basic? question

5. 2 questions from book sed and awk programming

6. Question: How to remove END OF LINE using AWK or SED

7. Sed/awk question

8. Sed/awk question Answered!

9. sed, awk, perl

10. SED to AWK...???

11. Extracting hyphenated words using sed/awk

12. Need help with sed or awk !!

 

 
Powered by phpBB® Forum Software