LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-06-2010, 05:22 AM   #1
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Rep: Reputation: 30
Question Can I combine contents of 2 files into a third file ?


Hi there,
Suppose I have 2 files:

one.dat

Code:
common      one
1           20
2           30
30          40
...         ...
n            m


two.dat
Code:
common      two
1           10
2           15
30          20
...         ...
n            p
Can I use awk or something els like awk to get a third file (say one-cum-two.dat) with contents like this:


one-cum-two.dat
Code:
common      one     two
1           20      10
2           30      15
30          40      20
...         ...     ...
n            m       p
Kindly guide me. Best Regards
 
Old 02-06-2010, 05:40 AM   #2
sohail0399
Member
 
Registered: Oct 2008
Location: Pakistan, Islamabad
Distribution: CentOS, Fedora, Solaris
Posts: 154

Rep: Reputation: 23
may be this can help you

cat one.dat && tail -n "`wc -l two.dat | awk '{ print $1-1}'`" two.dat
 
Old 02-06-2010, 08:34 AM   #3
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks sohail0399
When I did as you said, this is what happened:
Code:
[mq15@localhost try]$ cat one.dat && tail -n "`wc -l two.dat | awk '{ print $1-1}'`" two.dat
common      one
1           20
2           30
30          40
...         ...
n            m
1           10
2           15
30          20
...         ...
n            p
[mq15@localhost try]$
I want the output to be the same as one.dat but with a third column (i.e two) from two.dat file.

Last edited by mq15; 02-06-2010 at 12:55 PM.
 
Old 02-06-2010, 09:39 AM   #4
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Uncle Google found the "paste" command:
Code:
[mherring@Ath play]$ more one
common      one
1           20
2           30
30          40

[mherring@Ath play]$ more two
common      two
1           10
2           15
30          20

[mherring@Ath play]$ paste one two|awk '{printf("%s\t%s\t%s\n", $1,$2,$4)}'
common  one     two
1       20      10
2       30      15
30      40      20
 
1 members found this post helpful.
Old 02-06-2010, 10:21 AM   #5
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
There is also the join command:
Code:
cat one                      
1       20                                               
2       30
3       4
30      40
35      45
40      50
n       m
> cat two
1       23
2       33
3
30      43
33
40      53
n       o
> join -j1 one two -e# -t'    '
1       20      23
2       30      33
3       4       #
30      40      43
40      50      53
n       m       o
The character after the -t option is a tab. The input files need to be sorted on the join fields.
 
1 members found this post helpful.
Old 02-06-2010, 11:25 AM   #6
mq15
Member
 
Registered: Apr 2009
Location: Pakistan
Distribution: Fedora 9
Posts: 192

Original Poster
Rep: Reputation: 30
Thanks to everyone here..
 
Old 02-06-2010, 01:07 PM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
Quote:
Originally Posted by jschiwal View Post
There is also the join command:
Code:
cat one                      
1       20                                               
2       30
3       4
30      40
35      45
40      50
n       m
> cat two
1       23
2       33
3
30      43
33
40      53
n       o
> join -j1 one two -e# -t'    '
1       20      23
2       30      33
3       4       #
30      40      43
40      50      53
n       m       o
The character after the -t option is a tab. The input files need to be sorted on the join fields.
In this method, the lines beginning with 33 and 35 are missing.

My method also fails when some lines are missing fields.
 
Old 02-06-2010, 03:48 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738Reputation: 738
I have put this back to "unsolved" status-----because 2 solutions (including mine) have some issues when fields are empty.

A fix that's in my head is to first preprocess the files and put a word (eg "none") in place of the empty fields. While my head tries to do this, I fully expect an AWK wizard to jump in with something more elegant....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
combine multiple files into one file? baddah Linux - Newbie 10 09-06-2011 12:22 PM
Comparing two files to get matched contents in another file using shell script pooppp Linux - Networking 3 08-05-2008 01:11 AM
Combine output of multiple files in one CSV file say_hi_ravi Programming 4 07-17-2008 04:04 AM
Combine multiple wmv files to one large file drkstr Linux - Software 4 04-26-2006 12:50 PM
Command to combine several files as a single file, etc. satimis *BSD 3 06-10-2004 04:59 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration