Computing.Net > Forums > Unix > I will need You help.

I will need You help.

Reply to Message Icon

Original Message
Name: johan
Date: January 18, 2003 at 03:54:02 Pacific
Subject: I will need You help.
OS: sun 8
CPU/Ram: 1/1
Comment:

Hi !
I am sorry because My english is not best possible, but I hope that You will understand.
In last time My question was in duplicate here, I hope not now.

I need You help.
I have two files, for example like these below: ( in real life files contain over 10000 lines and 11 field )

File 1 has some old values in field 2 and 4 (delimited : ) and values should be updated from the file 2. New line ”pignet” should move too from file2 to file 1.

value:11:and:22:for:the:cat
value:8888:and:9999:for:the:horse
value:3:and:4:for:the:dog
value:444:and:5::the:fish
value:5555:and:666:for:the:rat

File 2 is updating file and it has new values for the some animals and some values are same as before, and it has one ( or more ) new line too . Order of the lines can be anything. ( First can be rat or anything )

New:value:5555:666:rat
New:value:2222:333:horse
New:value:3:4:dog
New:value:11:22:for:cat
New:value:6666:777:piglet
New:value:444:5:fish

I can use command sort, diff, mv, cp, awk ( little ) e.t.c but My script not want pan out well.

Thanks for You all.


Report Offensive Message For Removal


Response Number 1
Name: johan
Date: January 18, 2003 at 06:59:57 Pacific
Reply: (edit)

I can see that I have error in My question, file 1 has one ":" mark too much in "fish line" and file 2 has word "for" too much in line "cat". So, I try to move new values from the updating file 2 to field 2 and 4 in file 1

please, try to understand

BR johan


Report Offensive Follow Up For Removal

Response Number 2
Name: James Boothe
Date: January 18, 2003 at 09:21:28 Pacific
Reply: (edit)

Following script will read file1 and output file1NEW, having applied changes from file2 which awk pulls into an array at startup. The size of file1 does not matter, but since file2 is being stored in any array, there will be a limit to how much it can store.

The END processing will output any file2 entries that were not matched during the processing of file1.

I have preserved the indentation by changing leading spaces to underscores. And left angle brackets cause text loss on this web page, so those have been changed to LB. After you copy/paste this script, you can change these back with:

sed -e "s/_/ /g" -e "s/LB/ scriptgood

#!/bin/sh

awk -F":" 'BEGIN {\
rc=1
while (rc==1)
__if (rc=getline LB "file2")
______{w2[$5]=$3
_______w4[$5]=$4}
OFS=":"
}

{
if ($7 in w2)
___{print $1,w2[$7],$3,w4[$7],$5,$6,$7
____matched[$7]=1}
else
___print
}
END {
for (i in w2)
____if (matched[i]!=1)
_______print "value",w2[i],$3,w4[i],"for","the",i
}' file1 > fileNEW

exit 0

(Leaving now, return Jan 23)


Report Offensive Follow Up For Removal

Response Number 3
Name: johan
Date: January 20, 2003 at 05:41:48 Pacific
Reply: (edit)

Hi James and Thank

Script what You did is very good and it work well, and I can learn many thing by it.

Thank You very much for You kindness

B.R.
johan


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: I will need You help.

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge