The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Advanced & Expert Users
Google UNIX.COM


UNIX for Advanced & Expert Users Advanced UNIX and Linux questions go here. Expert-to-Expert.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem joining 2 files rochitsharma UNIX for Advanced & Expert Users 4 04-03-2008 03:12 AM
Help with joining two files rjlohman Shell Programming and Scripting 3 09-27-2006 08:55 AM
Joining lines from two files - please help chandra004 Shell Programming and Scripting 25 07-26-2006 11:39 PM
joining files Manu UNIX for Dummies Questions & Answers 2 04-25-2005 09:24 AM
joining 2 files webtekie UNIX for Dummies Questions & Answers 1 10-21-2003 07:51 AM

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

Join Date: May 2008
Posts: 12
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Joining 2 CSV files together

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.

The logic behind 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.
Forum Sponsor
  #2 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 120
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi,

#!/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


Output:
001X ,,300
002X ,,300
003X ,,500
004X ,,300
005X ,,600
006X ,,100


Penchal
  #3 (permalink)  
Old 05-06-2008
Registered User
 

Join Date: Apr 2008
Location: Bangalore
Posts: 120
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Hi,

You can add this line as 2nd line of the above script for complete output

echo "HEADER \nHEADER \nheader \n, ,HEADER " >> final_out

Output:
HEADER
HEADER
header
, ,HEADER
001X ,,300
002X ,,300
003X ,,500
004X ,,300
005X ,,600
006X ,,100
  #4 (permalink)  
Old 05-06-2008
Moderator
 

Join Date: Feb 2007
Posts: 1,388
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit! Stumble this Post!Spurl this Post!
Double post questions is not allowed, please read the:

rules

Continue here:

Merging 2 .CSV files in Unix

Thread closed.
Google UNIX.COM
Closed Thread

Thread Tools
Display Modes


The 50 most popular UNIX and Linux searches.
Google Search Cloud for The UNIX and Linux Forums
421 service not available, remote server has closed connection ^m automate ftp autosys awk trim bash eval bash exec bash for loop command copy/move folder in unix couldn't set locale correctly curses.h cut command in unix find grep find mtime find null character in a unix file grep multiple lines grep or grep recursive hp-ux ifconfig inaddr_any inappropriate ioctl for device lynx javascript mailx attachment mget mtime ping port remove first character from string in k shell replace space by comma , perl script rsync ftp scp recursive segmentation fault(coredump) sftp script snoop unix solaris change ip address stale nfs file handle syn_sent tar exclude tar extract to folder test: argument expected unix unix .profile unix forum unix forums unix internals unix interview questions unix mtime unix simulator unix.com vi substitute while loop within while loop shell script


All times are GMT -7. The time now is 03:59 AM.


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

Content Relevant URLs by vBSEO 3.2.0

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101