Sed: Replace text in one file with SPECIFIC text from another

Sed: Replace text in one file with SPECIFIC text from another

Post by Al.Vazq.. » Sat, 11 Feb 2006 00:33:47



Hi all,
  I'm trying to take a dhcpd.conf file and replace each IP address in
it with a host name. The address-to-hostname map is in hosts. This is
what I've got so far:

sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R hosts' dhcpd.conf

So far, this appends a single line from hosts to the matched line in
dhcpd.conf, here is sample output:

        fixed-address         192.168.1.81;
192.168.1.92    term012

The first line is from dhcpd.conf and the second is from hosts.
However, I have not been able to figure out how to append the
appropriate line; at the moment, it's going in sequence through hosts
and the lines being appended do not match the IP.

I've tried to get the output of grep piped into the read file, a la:

sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R `grep \1 hosts`'
dhcpd.conf

But it does nothing. I've tried several variants to no avail.

1) So the first task is to get the proper lines appended to the proper
places.

2) The second task is to modify the new data to have only the host
name.
To the best of my knowledge, reading does not affect the pattern space.
Is it possible to accomplish this in one pass if the pattern space is
not affected? Will I have to write the output to a new file and then
sed it again to get the results I want?

Any and all help is appreciated.

TIA,
Al

 
 
 

Sed: Replace text in one file with SPECIFIC text from another

Post by Icarus Sparr » Sat, 11 Feb 2006 01:26:43



> Hi all,
>   I'm trying to take a dhcpd.conf file and replace each IP address in
> it with a host name. The address-to-hostname map is in hosts. This is
> what I've got so far:

> sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R hosts' dhcpd.conf

> So far, this appends a single line from hosts to the matched line in
> dhcpd.conf, here is sample output:

>         fixed-address         192.168.1.81;
> 192.168.1.92    term012

> The first line is from dhcpd.conf and the second is from hosts.
> However, I have not been able to figure out how to append the
> appropriate line; at the moment, it's going in sequence through hosts
> and the lines being appended do not match the IP.

> I've tried to get the output of grep piped into the read file, a la:

> sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R `grep \1 hosts`'
> dhcpd.conf

> But it does nothing. I've tried several variants to no avail.

> 1) So the first task is to get the proper lines appended to the proper
> places.

> 2) The second task is to modify the new data to have only the host
> name.
> To the best of my knowledge, reading does not affect the pattern space.
> Is it possible to accomplish this in one pass if the pattern space is
> not affected? Will I have to write the output to a new file and then
> sed it again to get the results I want?

> Any and all help is appreciated.

I would do this in two steps
1) First convert the hosts file into a series of sed substitute statements.
2) Use these to edit dhcpd.conf

You can use sed for both of these. (untested)

sed -n 's/^\([0-9][0-9.]*\)[\t ]*\([^ \t]*\).*/s:\\<\1\\>:\2:p' hosts >t

sed -f t dhcpd.conf

(This assumes your sed knows about \< and \> for matching word boundaries)

Gnu sed has a '-i' option to do "in place" editing, but it would be more
normal to write the output of sed to a new file and then move that into
the original place.

 
 
 

Sed: Replace text in one file with SPECIFIC text from another

Post by Ed Morto » Sat, 11 Feb 2006 02:36:52


 > Hi all,
 >   I'm trying to take a dhcpd.conf file and replace each IP address in
 > it with a host name. The address-to-hostname map is in hosts. This is
 > what I've got so far:
 >
 > sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R hosts' dhcpd.conf
 >
 > So far, this appends a single line from hosts to the matched line in
 > dhcpd.conf, here is sample output:
 >
 >         fixed-address         192.168.1.81;
 > 192.168.1.92    term012
 >
 > The first line is from dhcpd.conf and the second is from hosts.
 > However, I have not been able to figure out how to append the
 > appropriate line; at the moment, it's going in sequence through hosts
 > and the lines being appended do not match the IP.
 >
 > I've tried to get the output of grep piped into the read file, a la:
 >
 > sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R `grep \1 hosts`'
 > dhcpd.conf
 >
 > But it does nothing. I've tried several variants to no avail.
 >
 > 1) So the first task is to get the proper lines appended to the proper
 > places.
 >
 > 2) The second task is to modify the new data to have only the host
 > name.
 > To the best of my knowledge, reading does not affect the pattern space.
 > Is it possible to accomplish this in one pass if the pattern space is
 > not affected? Will I have to write the output to a new file and then
 > sed it again to get the results I want?
 >
 > Any and all help is appreciated.

Can you use awk instead of sed? It's made for this kind of job. For
example, if dhcp.conf contains:

    fixed-address         192.168.1.81;

and "hosts" contains:

    192.168.1.81    term012

Then this:

    awk 'NR==FNR{map[$1";"]=$2";";next}{$2=map[$2]}1' hosts dhcpd.conf

will produce this:

    fixed-address term012;

Regards,

        Ed.

 
 
 

Sed: Replace text in one file with SPECIFIC text from another

Post by Xichen » Sat, 11 Feb 2006 14:11:12




>  > Hi all,
>  >   I'm trying to take a dhcpd.conf file and replace each IP address in
>  > it with a host name. The address-to-hostname map is in hosts. This is
>  > what I've got so far:

>  > sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R hosts' dhcpd.conf

>  > So far, this appends a single line from hosts to the matched line in
>  > dhcpd.conf, here is sample output:

>  >         fixed-address         192.168.1.81;
>  > 192.168.1.92    term012

>  > The first line is from dhcpd.conf and the second is from hosts.
>  > However, I have not been able to figure out how to append the
>  > appropriate line; at the moment, it's going in sequence through hosts
>  > and the lines being appended do not match the IP.

>  > I've tried to get the output of grep piped into the read file, a la:

>  > sed -e '/\([0-9]\{1,3\}[.]\)\{3\}[0-9]\{1,3\}/R `grep \1 hosts`'
>  > dhcpd.conf

>  > But it does nothing. I've tried several variants to no avail.

>  > 1) So the first task is to get the proper lines appended to the proper
>  > places.

>  > 2) The second task is to modify the new data to have only the host
>  > name.
>  > To the best of my knowledge, reading does not affect the pattern space.
>  > Is it possible to accomplish this in one pass if the pattern space is
>  > not affected? Will I have to write the output to a new file and then
>  > sed it again to get the results I want?

>  > Any and all help is appreciated.

> Can you use awk instead of sed? It's made for this kind of job. For
> example, if dhcp.conf contains:

>     fixed-address         192.168.1.81;

> and "hosts" contains:

>     192.168.1.81    term012

> Then this:

It looks this has been a typical problem in this group.. :-)

Quote:>     awk 'NR==FNR{map[$1";"]=$2";";next}{$2=map[$2]}1' hosts dhcpd.conf

this may have some problems if you dont have a corresponding ip between
two files, i.e. if $2 is not in map[$2], then something will be
discarded.. :(, so you might add some constraints like:

  awk 'NR==FNR{map[$1";"]=$2";";next}$2 in map{$2=map[$2]}1' hosts
dhcpd.conf

Best,
Xicheng

- Show quoted text -

Quote:> will produce this:

>     fixed-address term012;

> Regards,

>    Ed.