kayak
October 9, 2013, 7:41pm
1
Hello, I have two files that have this format:
file 1
86.82 0.00 86.82 43.61
86.84 0.00 86.84 43.61
86.86 0.00 86.86 43.61
86.88 0.00 86.88 43.61
file 2
86.82 0.22
86.84 0.22
86.86 0.22
86.88 0.22
I would like to merge these two files such that the final file looks like this:
86.82 0.00 86.82 43.61 86.82 0.22
86.84 0.00 86.84 43.61 86.84 0.22
86.86 0.00 86.86 43.61 86.86 0.22
86.88 0.00 86.88 43.61 86.88 0.22
Can someone please help?
Assumption files are sorted and missing columns should get 0.00:
join -o 1.1,1.2,1.3,1.4,2.1,2.2 -e "0.00" file1 file2
An awk approach may try
$ cat f2
86.82 0.22
86.84 0.22
86.86 0.22
86.88 0.22
$ cat f1
86.82 0.00 86.82 43.61
86.84 0.00 86.84 43.61
86.86 0.00 86.86 43.61
86.88 0.00 86.88 43.61
$ awk 'FNR==NR{_[$1]=$0;next}{printf($1 in _)?$0 FS _[$1] RS:""}' f2 f1
Resulting
86.82 0.00 86.82 43.61 86.82 0.22
86.84 0.00 86.84 43.61 86.84 0.22
86.86 0.00 86.86 43.61 86.86 0.22
86.88 0.00 86.88 43.61 86.88 0.22
Jotne
October 10, 2013, 2:00am
4
@Akshay Hegde
Using _
as a variable name is not a good idea. It makes beginners wonder what is going on. Its valid, but use some thing else, like this:
awk 'FNR==NR{arr[$1]=$0;next}{printf($1 in arr)?$0 FS arr[$1] RS:""}' f2 f1
When you see some like this example, its not easy to see what is going on
awk '_ && _--;/test/{_=3}' file
Thank you Jotne.. what you said is true it will be difficult for beginners to understand
RudiC
October 10, 2013, 7:59am
6
sort file1 file2 | paste -d"\t\n" -s
86.82 0.00 86.82 43.61 86.82 0.22
86.84 0.00 86.84 43.61 86.84 0.22
86.86 0.00 86.86 43.61 86.86 0.22
86.88 0.00 86.88 43.61 86.88 0.22