Merge 2 text files to one text file side by side

Inquiring minds want to know.... I need to take two files that I have latitude and longitude values and then combine them into one file with the values side by side separated by a space.

the first file is temp113-lat.txt and the second is temp113-lon.txt. They each have values listed in the file like the example below

<temp113-lat.txt>
2644.989500
2645.791200
2647.358600
2647.358300
2647.338700
2647.307900
2647.312100
2647.240400
2647.000300
2646.618800
2646.472100
2646.358800

<temp113-lon.txt>
5104.146495
5103.428400
5103.305700
5103.446300
5103.442100
5103.538900
5103.875200
5104.080100
5105.286800
5105.493300
5105.901000
5106.284000

I want to take these and post them into a .txt file in the following format.

<113latlon.txt>
2644.989500 5104.146495
2645.791200 5103.428400
.... ....

I am trying to write a script to do this automatically but I am missing the line with the command string to do this function. I have tried to use the "paste" command and for some reason it will not output the correct format I want. anyone want to jump in on this one for me... I am at my limit.

paste -d' ' filea fileb > filec

-d specifies the delimiter.

That is what I thought. I am not really understanding what the problem is... I did that exact command and this is what I got

[77225 loc]$ paste -d' ' temp113-lat.txt temp113-lon.txt
5105.471900
5105.626300
5104.146495
5104.146495
5104.146495
5103.428400
5103.305700

This is only one of the files and it seperated at the beginning by a space. Why would it only be pulling the data from one file? I checked both files and they contain data. Could it be due to the fact there is a process to get these files? I am including the process in the script.

grep m_lat /113/.log > /loc/temp113lat.txt
grep m_lon /113/
.log > /loc/temp113lon.txt
grep m_lat /114/.log > /loc/temp114lat.txt
grep m_lon /114/
.log > /loc/temp114lon.txt
cd /loc
awk '{print $3}' temp113lat.txt > temp113-lat.txt
awk '{print $3}' temp113lon.txt > temp113-lon.txt
awk '{print $3}' temp114lat.txt > temp114-lat.txt
awk '{print $3}' temp114lon.txt > temp114-lon.txt
The next line would be the process to combine the files into one text file so the other program can read them. Any thoughts?

no. it should work. are you sure that temp113-lat.txt has data?

i'd put some diagnostics following the awk scripts...
just /bin/ls -l or wc -l *

I checked both of the files and they contain data.... I am really lost on this one...

Could it be the - in the file name?

[77225 loc]$ wc -l temp113-lon.txt
52 temp113-lon.txt
[77225 loc]$ wc -l temp113-lat.txt
52 temp113-lat.txt
[77225 loc]$

i might make the file names a little more different just so it's easier to tell what's what.
maybe therein is the problem.

maybe like this:

grep junk some_file > grep_some_file

awk junk grep_some_file > awk_some_file

paste -d' ' awk_some_file awk_some_other_file > paste_some_files

But copying everything you've mentioned -- from the original post -- and
trying it on my system - - - - paste -d' ' worked perfectly.

it looks like ~something~ is nuking that first file argument to the paste command,
or the file name is wrong maybe....

I have done some changing files around and it seems that the paste command is only pulling from the second input file. What would cause it to do this? I changed the files around and if the lat was fist and lon was second pulled data from the lon and vice versa.

don't know.

try this in the middle of the script.

create file a -- put some junk in there.
create file b -- put some junk in there.

run the command:

paste -d' ' a b > c

exactly like that right before or after your paste command in your script.

if c contains something -- then paste is working as advertised.
If THAT doesn't work.... we'll go to C. By coincidence, I rewrote the
paste command on my system because I wanted something similar
to the output of the Visual SourceSafe diff command.

OK ... so i made 1.txt file containing
1
2
3
4....
and I made 2.txt file containing
a
b
c
d...
and after using the paste command i got

1 a
2 b
3 c
4 d
5 e
6 f
7 g
8 h
9 i

So it works. For some reason it is killing the first file. Now I just found out that if i do not use the deliminator I get this

[77225 loc]$ paste temp113-lat.txt temp113-lon.txt
2645.0805105.471900
2644.5485105.626300
2643.7325104.146495
2643.7325104.146495
2643.7325104.146495
2644.9895103.428400
2645.7915103.305700

 lat                lon

2647.358 | 5103.446300

It has some digits missing from the lat file it should be XXXX.XXXXXX but all of the digits are in the lon file. I wonder what is causing this to happen? Why would it not want to read the first file with a deliminator? I think it is a conspiracy.

Hi, can I just jump in here :slight_smile:
as a noob i'd not heard of the paste command, and was quite interested in what it could do. I tried the command as suggested

#paste -d' 'filea fileb > filec and got the same results as the OP, in that only details from the second file were being picked up.

however with a quick blurt at the man page i tried again, with some spaces that were not originally apparent from the suggested post

dave@freya:~$ paste -d ' ' 113_lat.txt 113_lon.txt > latlon.txt
dave@freya:~$ cat latlon.txt
2644.989500 5104.146495
2645.791200 5103.428400
2647.358600 5103.305700
2647.358300 5103.446300
2647.338700 5103.442100
2647.307900 5103.538900
2647.312100 5103.875200
2647.240400 5104.080100
2647.000300 5105.286800
2646.618800 5105.493300
2646.472100 5105.901000
2646.358800 5106.284000

note the spaces between the -d the ' symbols and the first filename
(run on opensolaris and bash shell if that helps)

Anyhoo thanks for an interesting little tip :slight_smile:

OK... I am back to work... The last post talking about spaces... I tried but I am still getting the same result... Do spaces matter that much... I know whitespace makes a difference but there has to be a reason why it is not pulling from the second file using the delimiter.

paste -d" " file1 file2

Hi, which quotes are you using (single, double, back)?, what shell are you using? and what O/S are you using?

I'll include output below from a test run without spaces, and with spaces, running on Solaris 10 using a Bash shell

-bash-3.00$ ls -l
total 4
-rw-r--r-- 1 dhoward other 144 Feb 4 07:27 temp113-lat.txt
-rw-r--r-- 1 dhoward other 144 Feb 4 07:28 temp113-lon.txt
-bash-3.00$
-bash-3.00$ paste -d''temp113-lat.txt temp113-lon.txt > 113latlon.txt
-bash-3.00$ cat 113latlon.txt
5104.146495
5103.428400
5103.305700
5103.446300
5103.442100
5103.538900
5103.875200
5104.080100
5105.286800
5105.493300
5105.901000
5106.284000
-bash-3.00$ paste -d ' ' temp113-lat.txt temp113-lon.txt > 113latlon2.txt
-bash-3.00$ cat 113latlon2.txt
2644.989500 5104.146495
2645.791200 5103.428400
2647.358600 5103.305700
2647.358300 5103.446300
2647.338700 5103.442100
2647.307900 5103.538900
2647.312100 5103.875200
2647.240400 5104.080100
2647.000300 5105.286800
2646.618800 5105.493300
2646.472100 5105.901000
2646.358800 5106.284000
-bash-3.00$

As you can see the spaces do matter, as does the type of quotes, in the above examles i have used the single quotes('), in the following examples, I am using the backquotes (`) and the double quotes (").

-bash-3.00$ paste -d ` ` temp113-lat.txt temp113-lon.txt > 113latlon3.txt
-bash-3.00$ cat 113latlon3.txt
5104.146495
5103.428400
5103.305700
5103.446300
5103.442100
5103.538900
5103.875200
5104.080100
5105.286800
5105.493300
5105.901000
5106.284000

-bash-3.00$ paste -d " " temp113-lat.txt temp113-lon.txt > 113latlon4.txt
-bash-3.00$ cat 113latlon4.txt
2644.989500 5104.146495
2645.791200 5103.428400
2647.358600 5103.305700
2647.358300 5103.446300
2647.338700 5103.442100
2647.307900 5103.538900
2647.312100 5103.875200
2647.240400 5104.080100
2647.000300 5105.286800
2646.618800 5105.493300
2646.472100 5105.901000
2646.358800 5106.284000
-bash-3.00$

The backquotes return only one set of values. from the second file

RedHat

Linux 2.6.18-92.1.22.el5 #1 SMP Fri Dec 5 09:29:46 EST 2008 i686 i686 i386 GNU/Linux

Here is the O/S and I am using a bash shell.

I tried what you are speaking of and i still get the same result. It is ignoring the first file and only outputting the second file. example below\

[67807 loc]$ paste -d ' ' temp113-lat.txt temp113-lon.txt > 113latlon2.txt
[67807 loc]$ cat 113latlon2.txt
5105.471900
5105.626300
5104.146495
5104.146495
5104.146495
5103.428400
5103.305700
5103.446300

[67807 loc]$ paste -d " " temp113-lat.txt temp113-lon.txt > 113latlon4.txt
[67807 loc]$ cat 113latlon4.txt
5105.471900
5105.626300
5104.146495
5104.146495
5104.146495
5103.428400
5103.305700

I am still getting the leading space as the delimiter but it is not reading the first file. but if I do it this way....

[67807 loc]$ paste temp113-lat.txt temp113-lon.txt > 113latlon5
[67807 loc]$ cat 113latlon5
2645.0805105.471900
2644.5485105.626300
2643.7325104.146495
2643.7325104.146495
2643.7325104.146495
2644.9895103.428400
2645.7915103.305700
2647.3585103.446300

I get both files and some of the first has been cut off. So I know it can read the file it just does not want to do it how I want. I am about to let this go and look for a work around on this problem. Is there any other way of doing this?

Thanks also to everyone who has posted help for this issue I appreciate such hospitality from the Unix.com community.

ok.... totally different approach since paste is causing you so much trouble:

#!/bin/ksh

len_a=$( wc -l < a )
len_b=$( wc -l < b )

if [ $len_a -gt $len_b ]; then
  max=$len_b
else
  max=$len_a
fi

(
head -n $max a
head -n $max b
) | pr -l$max -2

maybe cj can help make this POSIX compliant. :slight_smile: