- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Combine two text files
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2002 01:45 PM
тАО07-16-2002 01:45 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2002 01:46 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2002 01:46 PM
тАО07-16-2002 01:46 PM
Re: Combine two text files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2002 01:57 PM
тАО07-16-2002 01:57 PM
Re: Combine two text files
Use paste startdates enddates > test to get the result .
Manoj Srivastava
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2002 02:36 PM
тАО07-16-2002 02:36 PM
Re: Combine two text files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-16-2002 02:41 PM
тАО07-16-2002 02:41 PM
Re: Combine two text files
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2002 10:14 AM
тАО07-17-2002 10:14 AM
Re: Combine two text files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2002 10:20 AM
тАО07-17-2002 10:20 AM
Re: Combine two text files
#!/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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2002 10:33 AM
тАО07-17-2002 10:33 AM
Re: Combine two text files
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО07-17-2002 10:39 AM
тАО07-17-2002 10:39 AM
Re: Combine two text files
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.