Concatenating records from 2 files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Concatenating records from 2 files
# 1  
Old 04-02-2008
Data Concatenating records from 2 files

I'm trying to concatenate records from 2 files and output it to a third file. The problem I'm running into is that it seems like the "While" command is limited to processing one file at a time. It seems like you could read a record from file1 into a variable. Then do the same for the for file2. Then print $rec1 $rec2 >> file3. I can do this successfully with the first record without a looping command. However, when I try to put it in a while loop I get all the records from file 1 concatenated with the first record of file2. The code I'm trying looks like this => while read rec1
do
read rec3 <$dir/$temp_fil3
print $rec1 $rec3 >>$dir/$temp_fil4
# 2  
Old 04-02-2008
By concatenation, do you mean you're trying to take row 1 from file A and file B and make them into one long line, then repeat with row 2 from files A and B, and so on?

So if file A and file B have 300 rows each, you'll have a single file of 300 rows, where the contents of each row is a concatenation of the rows?

If so, what if the files have a different number of rows?

Please provide sample input and sample expected output.
# 3  
Old 04-02-2008
You are correct in your assumption of the concatenation. The files will always have exactly the same number of records because file 3 was built using file 1 as input. Sample input for file1 looks like this =>
400,a_200/1200/3200/amzq4m5.wid Sample input for file3 looks like this =>
,Dec092007 Sample output for file4 should like this =>
400,a_200/1200/3200/amzq4m5.wid,Dec092007
# 4  
Old 04-02-2008
This will work, but it's quick and dirty and has no error handling.

You have to run the script passing the two files as the only two arguments, in order.

ShawnMilo

Code:
 $ cat temp1.txt 
400,a_200/1200/3200/amzq4m5.wid
300,b_200/1200/3200/wxyq4m5.wid

 $ cat temp2.txt 
,Dec092007
,Nov092008

 $ cat powcmptr.py 
#!/usr/bin/env python

import sys

file1 = open(sys.argv[1], 'r')
file2 = open(sys.argv[2], 'r')

for line1 in file1:

    
    line1 = line1.rstrip("\n")
    line2 = file2.readline().rstrip("\n")

    print line1 + line2



 $ ./powcmptr.py temp1.txt temp2.txt 
400,a_200/1200/3200/amzq4m5.wid,Dec092007
300,b_200/1200/3200/wxyq4m5.wid,Nov092008

# 5  
Old 04-02-2008
Code:
paste -d\\ file1 file2 > file3

Regards
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Concatenating two files in required format

Firstly one of my mysql queries will yeild following output +-------+---------------------+-------------------+----------------------------------------------------------------------------+ | ID | PLATFORM | SORT_NAME | DESCRIPTION ... (2 Replies)
Discussion started by: vivek d r
2 Replies

2. Shell Programming and Scripting

Concatenating 3 files into a single file

I have 3 files File1 C1 C2 c3 File 2 C1 c2 c3 File 3 C1 c2 c3 Now i want to have File1 as C1 c2 c3 I File2 as C1 c2 c3 O File3 as c1 c2 c3 D and these 3 files should be concatenated into a single file how can it be done in unix script? (3 Replies)
Discussion started by: Codesearcher
3 Replies

3. Shell Programming and Scripting

Compare two files with different number of records and output only the Extra records from file1

Hi Freinds , I have 2 files . File 1 |nag|HYd|1|Che |esw|Gun|2|hyd |pra|bhe|3|hyd |omu|hei|4|bnsj |uer|oeri|5|uery File 2 |nag|HYd|1|Che |esw|Gun|2|hyd |uer|oi|3|uery output : (9 Replies)
Discussion started by: i150371485
9 Replies

4. UNIX for Dummies Questions & Answers

Concatenating Text Files

Hi, I have 30 text files on UNIX that I need to concatenate and create one big file. Could anyone provide me with a solution (if one exist)? I need the answer asap (today). Thanks a lot. Denis (5 Replies)
Discussion started by: 222001459
5 Replies

5. Shell Programming and Scripting

Concatenating two files

HI I need to concatenate two files which are having headers. the result file should contain only the header from first file only and the header in second file have to be skipped. file1: name age sriram 23 file2 name age prabu 25 result file should be name age sriram 23 prabu ... (6 Replies)
Discussion started by: Sriramprabu
6 Replies

6. Shell Programming and Scripting

negatively concatenating files

That subject might sound weird. What I have is two files. One looks like: rf17 pts/59 Jul 10 08:43 (10.72.11.22) 27718 pts/59 0:00 b rf17 pts/3 Jul 10 10:03 (10.72.11.22) 32278 pts/3 1:43 b rf58 pts/29 Jul 10 10:09 (10.72.11.51) 44220 pts/29 0:06 b rf58 pts/61 Jul 10 08:45 (10.72.11.51)... (2 Replies)
Discussion started by: raidzero
2 Replies

7. Shell Programming and Scripting

concatenating static string to records in data file

I just need to add a static ID to each output record so the users will be able to tell which group records in combined flatfiles come from I have the static ID in a bourne variable. I tried awk '{print "${GroupID}" $0}' infile > outfile But I ended up with the string ${GroupID} instead of... (5 Replies)
Discussion started by: gillbates
5 Replies

8. UNIX for Dummies Questions & Answers

concatenating x files into a one...

... i have 4 files to concatenate but in a certain order and i wanted to do it in a shorter one line command , if possible ! 4 files : file , file0 , file1 and file2 file1 into file2 file0 into the result file into the result thanks in advance Christian (1 Reply)
Discussion started by: Nicol
1 Replies
Login or Register to Ask a Question