The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 05-27-2009
pisang pisang is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Arrow Help with Combining Content of Two files

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...
  #2 (permalink)  
Old 05-27-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
Quote:
Originally Posted by pisang View Post
I'm trying to combine it using awk but still can't figure it out.
Can you show us what you have tried with awk?

Regards
  #3 (permalink)  
Old 05-27-2009
pisang pisang is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Quote:
Originally Posted by Franklin52 View Post
Can you show us what you have tried with awk?

Regards
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
  #4 (permalink)  
Old 05-27-2009
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,422
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"
output
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
  #5 (permalink)  
Old 05-27-2009
Franklin52 Franklin52 is offline Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,212
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
Regards

Last edited by Franklin52; 05-27-2009 at 08:21 AM.. Reason: adjust code
  #6 (permalink)  
Old 05-27-2009
pisang pisang is offline
Registered User
  
 

Join Date: May 2009
Posts: 3
Quote:
Originally Posted by ghostdog74 View Post
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"
output
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
Thanks Ghostdog!
Sponsored Links
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 09:28 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0