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
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.
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
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
$ 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
paste -d\\ file1 file2 > file3
Regards