Sort two files by time


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort two files by time
# 1  
Old 01-26-2010
Sort two files by time

I had a question about sorting two files which contain data like the following:

File 1:
09:12:12 blah blah
0 blah blah
1 blah blah
2 blah blah

09:12:15 blah blah
0 blah blah
1 blah blah
2 blah blah

File 2:
09:12:13 blah2
0 blah2
1 blah2
2 blah2

Result:
09:12:12 blah blah
0 blah blah
1 blah blah
2 blah blah

09:12:13 blah2
0 blah2
1 blah2
2 blah2

09:12:15 blah blah
0 blah blah
1 blah blah
2 blah blah

Basically the result should contain the contents of both files sorted by time and the 3 lines under it.

Thanks
# 2  
Old 01-26-2010
Hi.

There is a utility msort that allows sorting in complex ways. For example:
Code:
#!/usr/bin/env bash

# @(#) s1	Demonstrate flexible sort utility, msort
# http://www.billposer.org/Software/msort.html

echo
set +o nounset
LC_ALL=C ; LANG=C ; export LC_ALL LANG
echo "Environment: LC_ALL = $LC_ALL, LANG = $LANG"
echo "(Versions displayed with local utility \"version\")"
version >/dev/null 2>&1 && version "=o" $(_eat $0 $1) msort
set -o nounset

FILE1=data1
FILE2=data2
echo "" > EMPTY

echo
echo " Data file $FILE1:"
cat $FILE1

echo
echo " Data file $FILE2:"
cat $FILE2

echo
echo " Results:"
cat $FILE2 EMPTY $FILE1 EMPTY |
msort -q -b -n1 -ct 

exit 0

producing on your data in files data2 and data1:
Code:
% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0 
GNU bash 3.2.39
msort - ( /usr/bin/msort Apr 24 2008 )

 Data file data1:
09:12:12 blah blah
0 blah blah
1 blah blah
2 blah blah

09:12:15 blah blah
0 blah blah
1 blah blah
2 blah blah

 Data file data2:
09:12:13 blah2
0 blah2
1 blah2
2 blah2

 Results:
09:12:12 blah blah
0 blah blah
1 blah blah
2 blah blah

09:12:13 blah2
0 blah2
1 blah2
2 blah2

09:12:15 blah blah
0 blah blah
1 blah blah
2 blah blah

This script catenates the data files, making sure that there is at least one empty line at the end of each file, and then sorts. The options are: quiet, block (2 or more newlines delimit a record), sort on field 1, use comparison for time notation.

The msort code was in the Debian repositories for me. See the URL in the script for more information and a download as necessary ... cheers, drl
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sort date time in ascending order

Hi, i had a data block (coming from pipe from other codes) as: H YF_CO.dat 77164 11/17/2013 04:00:02 731374590.96 1 1 731374590.96 76586 77164 578 2988 Y H YF_CO.dat 77164 11/17/2013 04:00:07 731374590.96 1 4 731374590.96 76586 77164 578 2988 Y H YF_CO.dat 77178 ... (5 Replies)
Discussion started by: pr5439
5 Replies

2. Shell Programming and Scripting

Help with sort word and general numeric sort at the same time

Input file: 100%ABC2 3.44E-12 USA A2M%H02579 0E0 UK 100%ABC2 5.34E-8 UK 100%ABC2 3.25E-12 USA A2M%H02579 5E-45 UK Output file: 100%ABC2 3.44E-12 USA 100%ABC2 3.25E-12 USA 100%ABC2 5.34E-8 UK A2M%H02579 0E0 UK A2M%H02579 5E-45 UK Code try: sort -k1,1 -g -k2 -r input.txt... (2 Replies)
Discussion started by: perl_beginner
2 Replies

3. Shell Programming and Scripting

How to Sort by Date and Time?

Hi, The input file is as follows, 22.06.2012 17:58:38 CPUser: xxxxxxx, billedAfterStatus: Active 13.07.2012 08:46:15 CPUser: xxxxxxx, billedAfterStatus: Active 20.07.2012 08:56:24 CPUser: xxxxxxx, billedAfterStatus: Active 20.03.2012 08:56:24 CPUser: xxxxxxx, billedAfterStatus: Active... (16 Replies)
Discussion started by: nanthagopal
16 Replies

4. Shell Programming and Scripting

Why is sort -k not working all the time?

I have a script that puts a list of files in two separate arrays: First, I get a file list from a ZIP file and fill `FIRST_Array()` with it. Second, I get a file list from a control file within a ZIP file and fill `SECOND_Array()` with it while read length date time filename ... (8 Replies)
Discussion started by: alan
8 Replies

5. Shell Programming and Scripting

Sort by name, time, and size

How do you combine these ls commands so that I can have the outputs by name, time stamp, and size? ls -al |grep name_of_file ls -al | sort +4nr ls -l -t Please advise. (4 Replies)
Discussion started by: Daniel Gate
4 Replies

6. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

7. Shell Programming and Scripting

shell script to sort entries in a file by date and time

Hello All, Need a shell script to sort entries in a file by date and time. Below are the entries in the file, i need to sort it first by the date and then time Note :- Date is in MM/DD/YY format and date comes as the 6th & time comes on 7th coloumns respectively. 150 pbnawldb001-b... (10 Replies)
Discussion started by: ajiwww
10 Replies

8. Shell Programming and Scripting

sort takes a long time

Dear experts I have a 200MG text file in this format: text \tab number I try to sort using options -fd and it takes very long! is that normal or I can speed it up in some ways? I dont want to split the file since this one is already splitted. I use this command: sort -fd file >... (12 Replies)
Discussion started by: voolek
12 Replies

9. Shell Programming and Scripting

Sort By Date and Time

Hi , I would like to list or sort by date and time (the files are named in day and time format) where the latest file will be placed at the bottom and the earliest file be placed at the top. Can anybody help me? My files are named in the following manner. EG: abc_071128_144121_data "... (21 Replies)
Discussion started by: Raynon
21 Replies

10. Shell Programming and Scripting

how to numeric sort on field time

i want to sort time field given by who command as a whole i have tried like this who|sort -n +4 -5 (1 Reply)
Discussion started by: rahulspatil_111
1 Replies
Login or Register to Ask a Question