How to search and replace

Paul Howarth paul at city-fan.org
Thu Feb 2 16:38:57 UTC 2006


Dan Track wrote:
> On 2/2/06, Paul Howarth <paul at city-fan.org> wrote:
> 
>>Dan Track wrote:
>>
>>>On 2/2/06, Paul Howarth <paul at city-fan.org> wrote:
>>>
>>>
>>>>Dan Track wrote:
>>>>
>>>>
>>>>>On 2/2/06, Paul Howarth <paul at city-fan.org> wrote:
>>>>>
>>>>>
>>>>>
>>>>>>Dan Track wrote:
>>>>>>
>>>>>>
>>>>>>
>>>>>>>Hi
>>>>>>>
>>>>>>>I thought I'd ask this here as you guys helped me setup mrtg :)
>>>>>>>
>>>>>>>I've got the following pattern in a text file in linux.
>>>>>>>Target[description1].1.3.4.6.5.3.:string at 10.11.8.10
>>>>>>>Directory[description1]: server1
>>>>>>>
>>>>>>>Target[description2].1.3.4.6.4.9.:string at 10.17.5.24
>>>>>>>Directory[description2]: server2
>>>>>>>
>>>>>>>What I would like to do is replace the ip of each with the following
>>>>>>>value of "Directory". So for example I would like
>>>>>>>Target[description1].1.3.4.6.5.3.:string at 10.11.8.10 to become
>>>>>>>Target[description1].1.3.4.6.5.3.:string at server1.example.com
>>>>>>>
>>>>>>>How can I accomplish this using either C,perl,sed or awk
>>>>>>
>>>>>>sed -e '$b
>>>>>> /^Target\[[^]]*\][.0-9]*:.*@[0-9.]*$/ {
>>>>>>   $!N
>>>>>>   s/^\(Target\[[^]]*\][.0-9]*:.*@\)[0-9.]*\nDirectory\[[^]]*\]:
>>>>>>\(.*\)$/\1\2.example.com/
>>>>>>   P;D
>>>>>> }' textfile > output
>>>>>>
>>>>>
>>>>>
>>>>>Thanks for the reply. When I run it I get the following error:
>>>>
>>>>The long line starting with "s/" and ending with "example.com/" is a
>>>>single long line; make sure your mail client didn't add a newline.
>>>>
>>>
>>>Ok I tried that, it seem to have fixed the "s" problem. But now I get
>>>the following:
>>>
>>>sed -e '$b /^Target\[[^]]*\][.0-9]*:.*@[0-9.]*$/ {
>>>$!N
>>>s/^\(Target\[[^]]*\][.0-9]*:.*@\)[0-9.]*\nDirectory\[[^]]*\]:\(.*\)$/\1\2.example.com/
>>>P;D
>>>}' text_files/mrtg.cfg > text_files/mrtg.cfg.changed
>>>sed: can't find label for jump to `/^Target\[[^]]*\][.0-9]*:.*@[0-9.]*$/'
>>
>>You joined the line at the start with the "$b" to the following line.
>>There should have been a newline (or a semicolon) there.
>>
> 
> 
> Excellent. Thanks very much for your help. Just two points if you don't mind.
> 
> 1) I read the info page on sed, and found "b" means
> "`b LABEL' Unconditionally branch to LABEL."
> 
> What does branch mean, is it merely saying jump to the first instance
> of the next match?

Yes, branch means jump. If LABEL is omitted, the branch is to the end of 
the script.

> 2) The new changed config file now looks great but for this one point:
> The output now looks like:
> Target[description1].1.3.4.6.5.3.:string@ server1.example.com
> 
> There is a space between the "@" sign and the hostname. Any ideas on
> how that got in the sed statement and how to delete it. I can re-run
> the sed statement if you want.

You dropped a space from the script:

I had:    Directory\[[^]]*\]: \(.*\)$
You have: Directory\[[^]]*\]:\(.*\)$

I guess your mail client changed the space to a newline.

Paul.




More information about the fedora-list mailing list