Mombu the Programming Forum sponsored links

Go Back   Mombu the Programming Forum > Programming > Modify file based on contents of another file in awk
User Name
Password
REGISTER NOW! Mark Forums Read

sponsored links


Reply
 
1 20th January 06:04
lbalbalba
External User
 
Posts: 1
Default Modify file based on contents of another file in awk


Dear sir,

Thank you so much for the solution, and for the example that I gave earlier
it seems to work perfectly. However, I still seem to have some minor
problems with the merging. This appears to have to do with the fact that
the formatting of my stanza files is just a little bit different from the
example I gave earlier, and your otherwise perfect solution seems not quite
able to handle that gracefully. My sincerest apologies for not providing the
right stanza formatting earlier.

My stanza format really is this:

stanza1:
attribute1 = value1
attribute2 = value2

Notice that there is a leading 'tab' before each attribute, and that there
is a 'space' right before and right after the '=' sign. I seem to be unable
to modify your solution so that it deals correctly with this. With this
formatting your previous solution just prints out every found stanza each
time it finds it, printing out the found stanza twice is if it is both in
orgfile and in modfile .

Would you be willing to assist me with this ?

Sincerely,

John Smith.
  Reply With Quote


  sponsored links


2 20th January 06:04
demas
External User
 
Posts: 1
Default Modify file based on contents of another file in awk


Read both files into memory using arrays to store them, then process
them and output in the END section.


Chuck Demas

--
Eat Healthy | _ _ | Nothing would be done at all,
Stay Fit | @ @ | If a man waited to do it so well,
Die Anyway | v | That no one could find fault with it.
demas@theworld.com | \___/ | http://world.std.com/~cpd
  Reply With Quote
3 20th January 06:04
ed morton
External User
 
Posts: 1
Default Modify file based on contents of another file in awk


<snip>

Try this:

gawk 'NF == 1 {
stanza = $0 }
NF > 1 {
attribute = $1
value = $3
valueArray[stanza,attribute] = value
}
END {
for (stanAtt in valueArray) {
split(stanAtt,stanAttArray,SUBSEP)
stanza = stanAttArray[1]
attribute = stanAttArray[2]
if (stanza != prevStanza) {
print "\n" stanza
}
print "\t" attribute " = " valueArray[stanAtt]
prevStanza = stanza
}
}' orgfile modfile


Regards,

Ed.
  Reply With Quote
4 20th January 06:04
lbalbalba
External User
 
Posts: 1
Default Modify file based on contents of another file in awk


Thank you very much. This modified script works like magic. Thank you
so much for helping me out here, you have been a real life saver.


Sincerely,

John Smith.
  Reply With Quote
Reply


Thread Tools
Display Modes




Copyright 2006 SmartyDevil.com - Dies Mies Jeschet Boenedoesef Douvema Enitemaus -
666