Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find first pattern and replace with a string read from another file

Status
Not open for further replies.

loclong

Technical User
Aug 3, 2001
7
CA
Hello

I need help with replacing string in a text file with another string read from another file.

I have the following

file1

first_line
10.0.0.0 end_of_line mystring
10.0.0.0 end_of_line mystring
10.0.0.0 end_of_line mystring
the_rest

file2
10.1.1.1 next_string
10.1.1.2 next_string
10.1.1.3 next_string

I would like to have the output file3 to look like this

first_line
10.1.1.1 end_of_line next_string
10.1.1.2 end_of_line next_string
10.1.1.3 end_of_line next_string
the_rest

I wrote a small script but the sed didn't seem to work within a bourne shell script

#!/bin/sh
while read entry
do
string1=`echo $entry | awk '{print $1}'`
string2=`echo $entry | awk '{print $2}'`

cat file1 | sed '1,/10.0.0.0/s//'"$string1"'/' | sed '1,/my_string/s//'"$string2"/' > file3

done < file2

The output always came out like this for me
first_line
10.1.1.3 end_of_line next_string
10.1.1.3 end_of_line next_string
10.1.1.3 end_of_line next_string
the_rest

Thanks in advance for your help
LT
 
Sorry the output of my script should look like this instead

first_line
10.1.1.3 end_of_line next_string
10.0.0.0 end_of_line my_string
10.0.0.0 end_of_line my_string
the_rest
 
Maybe ...

awk '
BEGIN{"tail -1 file2"|getline;x=$1;y=$2}
NR==2{$1=x;$3=y}
{print}
' file1

Tested...

first_line
10.1.1.3 end_of_line next_string
10.0.0.0 end_of_line mystring
10.0.0.0 end_of_line mystring
the_rest
 
Variation on a theme

BEGIN {
fn = "file2"
while ((getline < fn) > 0) {
a1 = $1
a2 = $2
}
}
NR == 2 { print a1,$2,a2;next}
{print}

CaKiwi
 
Sorry I wasn't clear on the question

There are other text strings in the file and I only want to find a certain string pattern and replace it. The string pattern could be anywhere in the file like below...

fafaf
afadfadkskfj
string_to_replaced
adfadfja
fafadsaf
dafdsa
string_to_replaced

With the sample files I described and with the script I wrote it always came out like below

first_line
10.1.1.3 end_of_line my_string
10.0.0.0 end_of_line next_string
10.0.0.0 end_of_line next_string
the_rest

but I would like to see the output like this
first_line
10.1.1.1 end_of_line my_string
10.1.1.2 end_of_line my_string
10.1.1.3 end_of_line my_string
the_rest

Many thanks
LT
 
I don't understand what you want. For a start, is this

fafaf
afadfadkskfj
string_to_replaced
adfadfja
fafadsaf
dafdsa
string_to_replaced

in file1 or file2?

CaKiwi
 
Something like this ?
Code:
awk '
BEGIN{while((getline<"file2")>0){
  a[++i]=$1;b[i]=$2
} }
$1=="10.0.0.0"{print a[++j],$2,b[j];next}
1' file1 >file3

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
CaKiwi

Here is the portion of the content of the actual text file. I would like to find any instance of cvr_public_ip="0.0.0.0"
and tunnel_ip="0.0.0.0" and replace the 0.0.0.0 with the new ip address read in from a file. #.#.#.# will be a different number for every new instance found

ie:

first cvr_public_ip="0.0.0.0" would be replaced by
cvr_public_ip="10.1.1.1" and the first tunnel_ip="0.0.0.0" would be replaced by "tunnel_ip="20.1.1.1"

next one would be 10.1.1.2 , 20.1.1.2 so on and so forth...

The second input file would contain

10.1.1.1 20.1.1.1
10.1.1.2 20.1.1.2
10.11.0.1 21.1.1.2

etc....


Thanks
LT

</routetable>
<bgp_manager is_enabled="no" ipv6_enable="no" update_msg_interval="0" max_attributes="10000" origin="0">
<bgp_node ip_version="4" ip_addr="10.1.1.2" link_local_addr_enable="no" routerid_override="no" as_number="65001" confed_enable
="no" confed_as="1" is_enabled="yes" route_advertise_mode="auto" vcg_enable="yes" vpi="0" vci="32">
<routetable name="" description="">
<route enable="yes" type="ipv4_mbgprd" prefix="24" network_address="0.0.0.0" host_address="0.0.0.1" step="1" count="1">
<mbgprd cvr_count="1" cvr_public_ip="0.0.0.0" cvr_public_ip_step="0.0.0.1" tunnel_ip="0.0.0.0" tunnel_ip_step="0.0.0.1" ip_subnet_incr
ement="0.0.0.0" vpn_id="00-00-00-00-00-00-01" vpn_id_step="00-00-00-00-00-00-01"/>
<bgp_attributes origin="0" next_hop_override="no" next_hop="0.0.0.0" enable_exit_discriminator="no" exit_discriminator="0" local_prefe
rence="0" atomic_aggregate="no" enable_aggregator="no" aggregator_AS="0" aggregator_IP="0.0.0.0" enable_originator_id="no" originator_
id="0.0.0.0">
<as_path>
</as_path>
</bgp_attributes>
<ospf_attributes lsa_type="as_ext" cost_type="type1_ext_lsa" cost="1" mc="no" local_address_enable="no" no_unicast_enable="propagate="
no" route_tag_enable="no" route_tag="0" adv_router_override="no" advertising_router="0.0.0.0" forward_addr_enable" forward_addr_ip_ver
sion="4" forward_addr_ip="0.0.0.0" ref_ls_type="0" ref_ls_id="0.0.0.0"/>
<isis_attributes lsp_type="internal" metric_type="internal" default_metric="20" delay_enable="yes" delay_metric="20" expensele="yes" e
xpense_metric="20" error_enable="yes" error_metric="20" updown_bit="no" metric="20"/>
<rip_attributes metric="1" next_hop="10.1.1.51" route_tag="0"/>
<flap enable_="yes" partial_enable="no" interval_step_mode="disabled" interval_min="0" interval_max="60" interval_step="1"rval_initial
="60" duration_step_mode="disabled" duration_min="0" duration_max="60" duration_step="1" duration_initial="60"ial_step_mode="disabled"
partial_min="1" partial_max="1" partial_step="1" partial_initial="1"/>
</route>
</routetable>
<bgp_peer ip_version="4" ip_addr="10.1.1.1" as_number="65001" flap="no" connect_retry_period="120" hold_time="90" link_flap_
interval="1" link_flap_duration="1" max_update_msg_size="1024" max_route_per_msg="1000" suppress_notify_tx_enable="no" update_msg_log_
enable="no" advertise_node_route="yes" confed_member="no" ipv4_mpls_enable="no" ipv4_mpls_vpn_enable="no" ipv4_unicast_enable="no" ipv
4_multicast_enable="no" ipv4_mbgprd_enable="yes" ipv4_vpn_vr_enable="yes" ipv4_mpls_vpn_vr_enable="no" ipv6_mpls_enable="no" ipv6_mpls
_vpn_enable="no" ipv6_unicast_enable="no" ipv6_multicast_enable="no" route_refresh="yes" aspath_proc_enable="yes"/>
</bgp_node>
<bgp_node ip_version="4" ip_addr="10.1.1.3" link_local_addr_enable="no" routerid_override="no" as_number="65001" confed_enable
="no" confed_as="1" is_enabled="yes" route_advertise_mode="auto" vcg_enable="yes" vpi="0" vci="32">
<routetable name="" description="">
<route enable="yes" type="ipv4_mbgprd" prefix="24" network_address="0.0.0.0" host_address="0.0.0.1" step="1" count="1">
<mbgprd cvr_count="1" cvr_public_ip="0.0.0.0" cvr_public_ip_step="0.0.0.1" tunnel_ip="0.0.0.0" tunnel_ip_step="0.0.0.1" ip_subnet_incr
ement="0.0.0.0" vpn_id="00-00-00-00-00-00-01" vpn_id_step="00-00-00-00-00-00-01"/>
<bgp_attributes origin="0" next_hop_override="no" next_hop="0.0.0.0" enable_exit_discriminator="no" exit_discriminator="0" local_prefe
rence="0" atomic_aggregate="no" enable_aggregator="no" aggregator_AS="0" aggregator_IP="0.0.0.0" enable_originator_id="no" originator_
id="0.0.0.0">
<as_path>
</as_path>
</bgp_attributes>
<ospf_attributes lsa_type="as_ext" cost_type="type1_ext_lsa" cost="1" mc="no" local_address_enable="no" no_unicast_enable="propagate="
no" route_tag_enable="no" route_tag="0" adv_router_override="no" advertising_router="0.0.0.0" forward_addr_enable" forward_addr_ip_ver
sion="4" forward_addr_ip="0.0.0.0" ref_ls_type="0" ref_ls_id="0.0.0.0"/>
<isis_attributes lsp_type="internal" metric_type="internal" default_metric="20" delay_enable="yes" delay_metric="20" expensele="yes" e
xpense_metric="20" error_enable="yes" error_metric="20" updown_bit="no" metric="20"/>
 
Something like this ?
Code:
awk '
BEGIN{while((getline<"file2")>0){
  a[++i]=$1;b[i]=$2
} }
/cvr_public_ip/{
 sub(/cvr_public_ip="0.0.0.0"/,"cvr_public_ip=\""a[++j]"\"",$0)
}
/tunnel_ip/{
 sub(/tunnel_ip="0.0.0.0"/,"tunnel_ip=\""b[++k]"\"",$0)
}
1' file1 >file3

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
My attempt is similar

BEGIN {
if (!fn) fn = "file2"
while ((getline < fn) > 0) {
a1[++n] = $1
a2[n] = $2
}
}
/cvr_public_ip="0\.0\.0\.0"/{sub(/0\.0\.0\.0/,a1[++j])}
/tunnel_ip="0\.0\.0\.0"/ {sub(/0\.0\.0\.0/,a2[++k])}
{print}

CaKiwi
 
cakiwi, what happens with your script when tunnel_ip is before[/b] cvr_public_ip ? I guess the 0.0.0.0 pattern is replaced by the wrong value.
 
PHV,

True. But then neither of our scripts checks to see if there are more substitutions than there are records in file2.

CaKiwi
 
CaKiwi, you're absolutely right, loclong have some work to do by himself...
 
CaKiwi and PHV

Thank you both for your tremendous help !!!
I used your scripts and they work great.
I put in the check in the bourne shell for other things...

Cheers
Loc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top