1753969 Members
7214 Online
108811 Solutions
New Discussion юеВ

Combine two text files

 
SOLVED
Go to solution
Derek Card
Advisor

Combine two text files

Hi,

I have two text files that have a starting date in one file and an ending date in the other file.

File "startdates"
1-Jan-1999
1-Jan-1999
2-Jan-1999

File "enddates"
12-Jun-1999
----------
31-Mar-2002

I would like to combine each line of these two files into 1 line
like this:
1-Jan-1999 12-Jun-1999
1-Jan-1999 ----------
2-Jan-1999 31-Mar-2002

The actual file has thousands of entries. I am fairly new to UNIX. Is there a command to easily do this?

TIA, Derek Card
13 REPLIES 13
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Combine two text files

Derek, this is about as easy as it gets.

paste startdates enddates > bothdates

Man paste for details.
If it ain't broke, I can fix that.
Dave Chamberlin
Trusted Contributor

Re: Combine two text files

There is - it is the paste command. Ex paste file1 file2. Do a man paste for options.
MANOJ SRIVASTAVA
Honored Contributor

Re: Combine two text files

Hi Derek


Use paste startdates enddates > test to get the result .


Manoj Srivastava
Ian Kidd_1
Trusted Contributor

Re: Combine two text files

Another option is pr -tm startdates enddates > bothdates.

The only advantage this has over paste is some formatting (although there's no advantage with your example.

But if the lengths of the lines in startdates varies, for example:

10-January-1999
1-March-1999
2-May-1999

then the output of paste would be:
No date has been specified 22-Jan-1999
10-January-1999 12-Jun-1999
1-March-1999 ----------
2-May-1999 31-Mar-2002

(I added 22-Jan-1999 to make the number of items/file equal)

with pr -tm
No date has been specified 22-Jan-1999
10-January-1999 12-Jun-1999
1-March-1999 ----------
2-May-1999 31-Mar-2002



If at first you don't succeed, go to the ITRC
Ian Kidd_1
Trusted Contributor

Re: Combine two text files

I was afraid that my extra white spaces would vanish in my reply above. The attachment I included should show the correct view (I hope)
If at first you don't succeed, go to the ITRC
Derek Card
Advisor

Re: Combine two text files

Hello again,

Thank you for all your replies. I'm afraid that I didn't quite get enough information and now I'm really stuck. These two date files have an additional entry called ID Number. I really need
to combine the Id Number, the start dates, and the end dates into 1 line. I don't even know where to begin.

File "startdates"
1-Jan-1999 1009
1-Jan-1999 1010
2-Jan-1999 1011
5-Jan-1999 1012

File "enddates"
12-Jun-1999 1009
---------- 1010
31-Mar-2002 1011

1009 1-Jan-1999 12-Jun-1999
1010 1-Jan-1999 ----------
1011 2-Jan-1999 31-Mar-2002

If there is no matching ID Number, no output is needed.
I've been looking at awk. Is that what I need to use?

Thanks, Derek Card
A. Clay Stephenson
Acclaimed Contributor

Re: Combine two text files

When I saw this post yesterday I thought this was a very hokey data model but now it makes a bit more sense. You could use awk (or Perl) for this but today I think a little sort and join will do the trick.

#!/usr/bin/sh

TDIR=${TMPDIR:-/var/tmp}
PID=${$}
T1=${TDIR}/X${PID}_1.tmp
T2=${TDIR}/X${PID}_2.tmp

sort -k2,2b startdates > ${T1}
sort -k2,2b enddates > ${T2}
join -j 2 ${T1} ${T2} | sort -k1,1n
STAT=${?}
rm -f ${T1} ${T2}
exit ${STAT}

That should be a complete solution. (If my expert typing is okay.)

Man sort, join for details.

Clay
If it ain't broke, I can fix that.
Steven Mertens
Trusted Contributor

Re: Combine two text files

hi ,

what about :
(you can put the output in a file)

#!/bin/sh

startDateFile=./startdates
endDateFile=./enddates

while read -e startDate idNr
do
while read -e endDate idNr2
do
if [ "${idNr}" = "${idNr2}" ]
then
echo "${idNr} ${startDate} ${endDate}"
fi
done < ${endDateFile}
done < ${startDateFile}

regards,

Steven

Leif Halvarsson_2
Honored Contributor

Re: Combine two text files

Hi
There is a (perhaps not so known) command in HP-UX called "join" which is a kind of "relational operator". It matches fields in two files. The files need to be sorted on the matching fields.

In your case the "basic" of your script should look somthing like.

join -j1 3 -j2 1 -o 1.1 2.2 2.1 startfile endfile

Which means it matches field 3 of startfile with field 1 of endfile and output field 1 of startfile and field 2 and 1 of endfile.

Have a look at man join.