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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Take variable from file 1 and put at certain positions in file2

Status
Not open for further replies.

hmehta

IS-IT--Management
Jun 5, 2002
27
US
I need to do the following. I have two files file1 and file2.

File1 has some variables with !! characters at the begining and the end of the variables like the following
!!EXTERNAL_WEB_ADDRESS!!

File2 has variables with same name but without !! charcaters like

EXTERNAL_WEB_ADDRESS

Now I need to wrtie a script which looks at all bang bang(!!) variables in file 1 (!!EXTERNAL_WEB_ADDRESS!!)and then goes to file 2 and put
!! in front and end of all the similar named variables in file2.
These variable may occur at different positions in file 1 and file 2.
Can anybody suggest a script??


 
You could try this::no guarantees.

awk -v fn="file1" ' BEGIN {
while ((getline arr[a++] < fn) > 0) {
m++
}

print m, &quot;records read from&quot;, fn

for (x in arr) {
if (arr[x] !~ /!!.*!!/) {
delete arr[x]
}
}
}
{
for (all in arr) {
gsub(/!!/,&quot;&quot;,arr[all])
if ($0 == arr[all]) {
$0 = &quot;!!&quot; $0 &quot;!!&quot;
}
}
print $0 > tempfile
} file2


 
This doesn't work I chaged it to:
awk -v fn=web.xml.sv BEGIN'{
while ((getline arr[a++] < fn) > 0) {
m++
}

print m, &quot;records read from&quot;, fn

for (x in arr) {
if (arr[x] !~ /!!.*!!/) {
delete arr[x]
}
}
}
{
for (all in arr) {
gsub(/!!/,&quot;&quot;,arr[all])
if ($0 == arr[all]) {
$0 = &quot;!!&quot; $0 &quot;!!&quot;
}
}
print $0 > tempfile
}'web.xml
 

file test.txt:
--------------------
!!thisnthat!!
wasthenthere
whenthewre
!!thisreddog!!
jingobingo
!!redbottomrose!!
------------------------

file test2.txt
-----------------------
thisnthat
wasthenthere
whenthewre
thisreddog
jingobingo
redbottomrose
-----------------------

No sleep in 24 hours, believe it.

program rev 0.01
awk -v fn=&quot;/home/mars/test.txt&quot; ' BEGIN {
while ((getline array[a++] < fn) > 0) {
m++
}
print m, &quot;records read.&quot;

for (xx in array) {
if (array[xx] !~ /^!!.*!!$/) {
delete array[xx]
}
}
}
{
for (all in array) {
gsub(/!!/,&quot;&quot;,array[all])
if (array[all] == $1) {
$1 = &quot;!!&quot; $1 &quot;!!&quot;
}
}
print $1
}' test2.txt

program output:

6 records read.
!!thisnthat!!
wasthenthere
whenthewre
!!thisreddog!!
jingobingo
!!redbottomrose!!

Let the gods of awk(Cakiwi,Vgersh, et-al),help you if it still doesn't work.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top