The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
merging of 2 files AWK - part 2 pp56825 Shell Programming and Scripting 2 02-11-2008 06:14 PM
merging of 2 files AWK, SHELL or something else klut Shell Programming and Scripting 18 01-18-2008 04:46 AM
Merging two files venommaker UNIX for Dummies Questions & Answers 4 01-10-2008 04:15 AM
merging colums from different files ppass Shell Programming and Scripting 5 06-19-2006 04:26 AM
merging two files rameshonline Shell Programming and Scripting 13 06-29-2004 03:06 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-02-2008
Registered User
 

Join Date: May 2008
Posts: 12
Merging 2 .CSV files in Unix

I need a little help as I am a complete novice at scripting in unix. However, i am posed with an issue... i have two csv files in the following format@

FILE1.CSV:
HEADER
HEADER
Header
, , HEADER
001X ,,200
002X ,,300
003X ,,300
004X ,,300



FILE2.CSV:
HEADER
HEADER
Header
, , HEADER
001X ,,300
003X ,,500
005X ,,600

006X ,,100


I need the above to files to merged into on final file as below:

FINAL_MERGED.CSV:
HEADER
HEADER
Header
, , HEADER
001X ,,300
002X ,,300
003X ,,500
004X ,,300
005X ,,600
006X ,,100



I have tried using the join command but that only lets me to join the two files but as you can see from the above i need the ability to be able to overwrite values from file 1 whith those in file 2.

Any assistance would be greatly appreciated.
Thanks

Last edited by chachabronson; 05-06-2008 at 12:58 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 05-02-2008
Registered User
 

Join Date: May 2008
Posts: 12
anybody outhere with a clue???
Reply With Quote
  #3 (permalink)  
Old 05-04-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,549
Could you clarify the logic?
Why you want 005X to be 100 and not 600?
Reply With Quote
  #4 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: May 2008
Posts: 12
Quote:
Originally Posted by radoulov View Post
Could you clarify the logic?
Why you want 005X to be 100 and not 600?
Sorry my mistake that was due to a typo. I have edited and amended above as required.

The logic behing it is that the first csv (FILE1.CSV) file is produced but may have incorrect figures associated with it. Once the information is readily available the second csv (FILE2.CSV) is produced with the correct figures and any missing codes.

Once this has been done file 1 and file 2 need to be merged to have the correct figures from file 2 and also to include any missing codes that are on file 2 but not file 1.

I need to do this in Unix, my initial thoughts were to use the join command but not sure if this is possible but may be some use of lookup could be done... again i am a programming and unix newbie so would appreciate any assistance.

Thanks
Reply With Quote
  #5 (permalink)  
Old 05-06-2008
radoulov's Avatar
addict
 

Join Date: Jan 2007
Location: Milan, Italy/Varna, Bulgaria
Posts: 1,549
Use nawk or /usr/xpg4/bn/awk on Solaris.
You may need to modify the code, if the format of your input doesn't match exactly the samples you posted.

Code:
awk 'END {
for (i=5; i<=n; i++)
  if (f2[_[i]])
    print f2[_[i]]
}
NR == FNR {
f2[_[n = FNR] = $1] = $0
next
}
$1 in f2 { 
$0 = f2[$1] 
delete f2[$1]
}1' FS=, OFS=, file2.csv file1.csv

Last edited by radoulov; 05-06-2008 at 04:37 AM.
Reply With Quote
  #6 (permalink)  
Old 05-09-2008
Registered User
 

Join Date: May 2008
Posts: 12
Thanks for your help, however i am unable to get teh code above to work.
I have had some assistance with the above from anothe forumite who gave me the following code:

#!/bin/ksh
cat file1.csv file2.csv >> temp
grep X temp | cut -d"," -f1 | sort -u > final2
while read line
do
line1=`grep $line file2.csv`
if [ $? -eq 0 ]
then
echo $line1 >> final_out
else
line2=`grep $line file1.csv`
echo $line2 >> final_out
fi

done < final2


If anyone could explain in detail as to what is happening in the loop that would be great... i have tested the above and it works like a dream but i want to learn rather than just use code. I am a complet novice so would like a description in a basic level which will help me understand for future.

Thanks to everyone who has helped so far
Reply With Quote
  #7 (permalink)  
Old 08-05-2008
Registered User
 

Join Date: Aug 2008
Posts: 1
Join csv

Hi .. as written I tried to implement the logic.....I could successfully do the concatenation....but my file "final_2" is blank.....
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
solaris

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:48 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0