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. |
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 |
All you need is "cat" + "sort"
Example---I generated 2 files--join1 and join 2: Code:
[mherring@Ath play]$ cat join1 |
@pixellany
Your solution is indeed sufficient for my needs. Thanks a lot ! |
You don't even need cat:
sort -n file1 file2 Quote:
|
All times are GMT -5. The time now is 05:06 PM. |