![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Combining two files | hemangjani | Shell Programming and Scripting | 7 | 06-13-2007 10:32 PM |
| Combining Two Files | stevefox | Shell Programming and Scripting | 4 | 02-20-2006 05:09 AM |
| Combining Two Files | bat711 | Shell Programming and Scripting | 3 | 10-05-2005 01:26 PM |
| Combining files | Enda Martin | UNIX for Dummies Questions & Answers | 2 | 07-20-2001 10:31 AM |
| combining files | apalex | UNIX for Dummies Questions & Answers | 3 | 06-19-2001 09:49 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
I'm trying to combine it using awk but still can't figure it out. Here is the file.
cat file1.txt Mr Smith Mr John Ms Linda cat file2.txt No 4 Jln Empat Kuala Lumpur No 213 Tmn Bunga Kedah No 1 Kampung Bukit Malaysia I want to combine this file1 and file2 so the output will be: Mr Smith No 4 Jln Empat Kuala Lumpur Mr John No 213 Tmn Bunga Kedah Ms Linda No 1 Kampung Bukit Malaysia Anybody please help me... ![]() |
|
||||
|
I try this script, but it says "awk: fatal: can't open source file `BEGIN {"
script: #!/bin/sh DATA1=file1.txt DATA2=file2.txt awk -f 'BEGIN { while ((getline < "'$DATA2'") > 0) f2array[$2] = $1 OFS=","} {if (f2array[$1]) print f2array[$1],$2,$3,$4,$5 }' $DATA1 Thanks for your time to read my thread Franklin ![]() |
|
||||
|
if you have Python
Code:
#!/usr/bin/env python
file1=open("file1").read().split("\n")
file1=[i for i in file1 if i.strip() !="" ]
file2=open("file2").read().split("\n\n")
for item in zip(file1,file2):
print item[0],"\n",''.join(item[1]),"\n"
Code:
# ./test.py Mr Smith No 4 Jln Empat Kuala Lumpur Mr John No 213 Tmn Bunga Kedah Ms Linda No 1 Kampung Bukit Malaysia |
|
||||
|
Assuming the lines are not separated by a blanc line in your files:
Code:
awk '{
print
getline s < "file2.txt"; print s
getline s < "file2.txt"; print s
print ""
}' file1.txt
Last edited by Franklin52; 05-27-2009 at 08:21 AM.. Reason: adjust code |
|
||||
|
Quote:
![]() |
| Sponsored Links | ||
|
|