LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   join 2 text files based on first number present in every line of the 2 text files (https://www.linuxquestions.org/questions/linux-software-2/join-2-text-files-based-on-first-number-present-in-every-line-of-the-2-text-files-784007/)

markraem 01-22-2010 07:17 AM

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.

indiajoe 01-22-2010 09:05 AM

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

pixellany 01-22-2010 09:31 AM

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


markraem 01-25-2010 06:11 AM

@pixellany

Your solution is indeed sufficient for my needs.

Thanks a lot !

jschiwal 01-25-2010 06:26 AM

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

Quote:

DESCRIPTION
Write sorted concatenation of all FILE(s) to standard output.


All times are GMT -5. The time now is 05:06 PM.