merging info from 2 files into 1


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting merging info from 2 files into 1
# 1  
Old 03-29-2009
merging info from 2 files into 1

Hi,

I have 2 files that I need to combine. I'm not sure if there is an easy way to use grep or something to combine them how i need.

Basically I have 2 csv files. Each file shares an ID for a line of information, but has a bunch of other so I can't easily combine them.

file1: <ID>,<info>,<info>,<info>,<info>, etc
file2: <ID>, <other info>,<other info>, etc

File2 has a few hundred lines, which I need to pull and merge the info from file1 which has a couple thousand lines.

I need a way to search and match the exact "<ID>," and then put that line from file1 onto the line in file2 that matches.

I hope that makes sense how i explained it. Any help would be greatly appreciated.

Thank you
# 2  
Old 03-30-2009
Maybe the "join" command is what you are looking for.
# 3  
Old 03-30-2009
Tools thinking awk and a couple of arrays is the way to go

Something like the following would allow you to create arrays as you read thru each of your files. The following STARTS the coding for awk to read and set some variables to arrays. This still needs logic on what to write out to a work (output) file, and also ENDING logic. [Don't want to do everything for you.]
If you think this might be a way to go, you could start with this coding and research on how to do the remaining awk commands necessary.

Code:
awk -v wrk1=$workfile1ns -v wrk2x=$workfile2x '
      BEGIN {
         FSf1=","
         FSf2=","
      }
      FNR == 1 {
          if (FILENAME==wrk2x) FS=FSf2
          if (FILENAME==wrk1) FS=FSf1
          $0=$0
      }
      FILENAME==wrk1 { 
         medx[$1]=$2
         }

      FILENAME==wrk2x{ 
         ua1_pl[$1]=$2
         }

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merging two files

Hi All , I have a scenario where we need to combine two files . Below are the sample files and expected output , File 1: 1|ab 1|ac 1|ae 2|ad 2|ac File 2: 1|xy 1|fc 2|gh 2|ku Output file : 1|ab|xy (3 Replies)
Discussion started by: saj
3 Replies

2. Shell Programming and Scripting

Merging two files

Guys, I am having little problem with getting a daily report! The daily process that I do is as follows 1. Unload Header for the report from the systables to one unl file, say Header.unl 2. Unload the data from the required table/tables to another unl file, say Data.unl 3. Send a... (2 Replies)
Discussion started by: PikK45
2 Replies

3. Shell Programming and Scripting

merging two files

file1.txt 1 2 10 11 56 57 7 8 43 44 and let's suppose that there is a file called file2.txt with 100 columns I want to produce a file3.txt with columns specified in file1.txt in that order (1,2,10,11,56,57,7,8,43,44) Thanks! (2 Replies)
Discussion started by: johnkim0806
2 Replies

4. Shell Programming and Scripting

Merging files

I have two files file 1 containing x rows and 1 column file 2 containing x rows and 1 column I want to merge both the files and add a comma between the two eg plz guide (1 Reply)
Discussion started by: test_user
1 Replies

5. UNIX for Dummies Questions & Answers

Merging two files

Hi, I have two files a.txt and b.txt. a.txt 1 2 3 4 b.txt a b c d e I want to generate a file c.txt by merging these two file and the resultant file would contain c.txt 1 (4 Replies)
Discussion started by: siba.s.nayak
4 Replies

6. Shell Programming and Scripting

Help with merging files

i would like to merge two files that have the same format but have different data. i would like to create one output file that contains information from both the original files.:rolleyes: (2 Replies)
Discussion started by: joe black
2 Replies

7. Shell Programming and Scripting

merging two files

Friends, os: redhat enterprise linux/SCO UNIX5.0 I have two files and I would like to merge on given key value. Now I have tried with join commd but it does not supporte multiple delimiters. and if records length is not fixed. join -a1 5 -a2 1 -t -o file1 file2 > outname Can any... (7 Replies)
Discussion started by: vakharia Mahesh
7 Replies

8. Shell Programming and Scripting

merging files

Thanks in advance I have 2 files having key field in each.I would like to join both on common key.I have used join but not sucessful. The files are attached here . what i Want in the output is on the key field SLS OFFR . I have used join commd but not successful. File one ======= SNO ... (6 Replies)
Discussion started by: vakharia Mahesh
6 Replies

9. UNIX for Dummies Questions & Answers

Merging two files

Hi I have a requirement like this. I have two files This is how data1.txt looks: EI3171280 38640658501 NENN2005-12-129999-12-312005-12-12HALL NANCY 344 CHENEY HIGHWAY ... (4 Replies)
Discussion started by: venommaker
4 Replies

10. Shell Programming and Scripting

Merging info

Hi, pls advise how could we merge contents of lines from 2 different files into 1 file but shares common label continously as in the following example: Thanks in advance...rgds. File1: // line: 0 abc def // line: 1 ghi jkl File2: // line: 0 mno pqr // line: 1 stu vwx End... (3 Replies)
Discussion started by: Manan
3 Replies
Login or Register to Ask a Question