Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

appending files using unix shell.

Status
Not open for further replies.

james777

Programmer
Jul 9, 2000
41
0
0
US
HI,<br>Can any of you tell me how to write a unix shell script (i'm learning now)<br>using AWK or general scripting<br>to append 3 files horizontally into a single file with two fields common in each file and the common fields doesn't repeat in the final file.<br><br>Example data::<br>file1<br><br>1234,abc,a12,zx,10<br>1235,acd,b35,dc,10<br>1456,cds,c25,fr,10<br>1290,cds,r45,fx,50<br><br><br>file2<br><br>1456,678,rrr,4679,10<br>1234,234,qqq,3456,10<br>1235,478,ddd,7890,10<br>1290,456,ttt,5867,50<br><br>file3<br><br>1290,ddd,345,50<br>1235,aaa,110,10<br>1234,ttt,100,10<br>1456,fff,234,10<br><br><br>The first and last fields for the three files are same but the order<br>they appear in each file might be different.<br>But it should grab the same first and last fields and write to final file<br>like below.<br><br>the final file should be like this.<br><br><br>1234,abc,a12,zx,234,qqq,3456,ttt,100,10<br>1235,acd,b35,dc,478,ddd,7890,aaa,110,10<br>1456,cds,c25,fr,678,rrr,4679,fff,234,10<br>1290,cds,r45,fx,456,ttt,5867,ddd,345,50<br><br>Any suggestions thanks<br>james.
 
There is more than one approach using shell scripting. Assuming all files contain lines represented in the other files and that the first record is the key then here is one method: -<br><br>sort file1 ¦ cut -d, -f1-4 &gt; file4<br>sort file2 ¦ cut -d, -f2-4 &gt; file5<br>sort file3 ¦ cut -d, -f2-4 &gt; file6<br><br>paste file4 file5 file6 &gt; file7<br><br> <p>Ged Jones<br><a href=mailto:gedejones@hotmail.com>gedejones@hotmail.com</a><br><a href= > </a><br>
 
Hi Jones,<br><br>Is doesn't worked out for me .<br>I need to pass the files as variables to awk.<br>and send the out put to final file.<br>I need to compare the first record and last record of <br>each file and appened the records horizontally<br>into one record for each , where the first and last record matches.<br>Please help urgent.<br><br>Thanks<br>james
 
Try reading <b>man join</b>, thath command is made for excatly you want to do.<br><br>I hope it works...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top