LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 01-22-2010, 07:17 AM   #1
markraem
Member
 
Registered: Nov 2003
Posts: 82

Rep: Reputation: 15
join 2 text files based on first number present in every line of the 2 text files


All,

I have 2 text files : file1.txt and file2.txt
cat file1.txt
15 this is a sentence containing various words and spaces
34 this is a another sentence containing various words and spaces

cat file2.txt
2 this is sentence1file2
6 this is sentence2file2
54 this is sentence3file2


I would like to join these 2 files. The result should look as follows :
cat joinedfile.txt

2 this is sentence1file2
6 this is sentence2file2
15 this is a sentence containing various words and spaces
34 this is a another sentence containing various words and spaces
54 this is sentence3file2

==> so the joined file must be sorted on the first number. Any ideas how this can be achieved ?

thanks in advance.
 
Old 01-22-2010, 09:05 AM   #2
indiajoe
Member
 
Registered: Jan 2009
Location: India
Distribution: Porteus atma
Posts: 84

Rep: Reputation: 21
Hi,
Use a shell script to take out line by line of first text file.
And call an gawk script which checks whether $1 is greater that the first word of the given line (it got from shell command as input) and if so append in previous line, and exit the gawk script.
Hope I am clear. Anyway gawk is the best thing to do such tasks...
-All the best.
indiajoe
 
Old 01-22-2010, 09:31 AM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
All you need is "cat" + "sort"
Example---I generated 2 files--join1 and join 2:
Code:
[mherring@Ath play]$ cat join1
1 aaasdg
5 sadfooin
5 asdb
8 asdoihn
[mherring@Ath play]$ cat join2
31 oipiou
1 oibswetasd
9 the horse
2 the pig
[mherring@Ath play]$ cat join1 join2|sort -n
1 aaasdg
1 oibswetasd
2 the pig
5 asdb
5 sadfooin
8 asdoihn
9 the horse
31 oipiou
 
1 members found this post helpful.
Old 01-25-2010, 06:11 AM   #4
markraem
Member
 
Registered: Nov 2003
Posts: 82

Original Poster
Rep: Reputation: 15
@pixellany

Your solution is indeed sufficient for my needs.

Thanks a lot !
 
Old 01-25-2010, 06:26 AM   #5
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
You don't even need cat:
sort -n file1 file2

Quote:
DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.
 
1 members found this post helpful.
  


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
Convert DOS text files to UNIX text files ta0kira Linux - Software 7 03-15-2011 11:42 AM
[SOLVED] can add a text line to text files? windstory Linux - Newbie 4 08-25-2009 06:40 AM
Command to join text files with headers chips11 Linux - Newbie 3 11-19-2008 03:43 PM
How to convert Line endings of text files mikesjays Linux - Newbie 5 04-07-2008 10:43 PM
renaming text files based upon a pattern in their content Spacepup Linux - General 1 07-28-2005 01:43 PM


All times are GMT -5. The time now is 10:33 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration