Sort Text


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Sort Text
# 1  
Old 09-09-2008
Sort Text

Hello,

I have a text file that I need to sort the lines by date

record=5,French 9,2008-09-02T08:55:00,2008-09-02T10:00:00,2
record=79,Entrepreneurship 30,2008-09-17T11:00:00,2008-09-17T12:00:00,2
record=6,Computer Science 20,2008-09-02T09:55:00,2008-09-02T10:50:00,1
record=7,Entrepreneurship 30,2008-09-02T11:00:00,2008-09-02T12:00:00,2


Essentially I need to sort these lines by the start date & time which is bolded here. Is it possible to sort these lines by the date highlighted?

Thank you for your help
Dallas
# 2  
Old 09-09-2008
Try this:

Code:
awk -F, '{t=$3;gsub("[-T:]*","",t);print t,$0}' inputfile | sort -n -k 1,1 | cut -d " " -f 2- > outputfile

Essentially it creates a temporary field containing the date stamp minus all punctuation, sorts it by that field numerically, and removes that field again.
# 3  
Old 09-09-2008
I have to be honest with you. I have absolutely no idea how this works but it works very well. Thank you so much for your help.

Thank you Annihilannic
Dallas
# 4  
Old 09-09-2008
Hello,

Based in this same text format is there a way to extract all lines between two dates.

record=5,French 9,2008-09-05T08:55:00,2008-09-02T10:00:00,2
record=79,Entrepreneurship 30,2008-09-04T11:00:00,2008-09-17T12:00:00,2
record=6,Computer Science 20,2008-09-03T09:55:00,2008-09-02T10:50:00,1
record=7,Entrepreneurship 30,2008-09-02T11:00:00,2008-09-02T12:00:00,2

Essentially can I pull say the all items between September 3 at midnight and september 4th at midnight.

Thank you
Dallas
# 5  
Old 09-09-2008
Try this:

Code:
awk -F, '{t=$3;gsub("[-T:]*","",t); if (t >= 20080903000000 && t < 20080904000000) print t,$0}' inputfile | sort -n -k 1,1 | cut -d " " -f 2- > outputfile

I'll take the opportunity to explain how it works:

awk -F, - run awk, setting the default input field separator to a comma.

The next part between the single quotes is the awk script:
{t=$3; - assign the third field to variable "t"
gsub("[-T:]*","",t); - substitute any occurrences of dash, capital T or a colon in the variable "t" with nothing
if (t >= 20080903000000 && t< 20080904000000) print t,$0} - print t followed by the original input line only if the timestamp is in the specified range

inputfile - the input data

| sort -n -k 1,1 - sort numerically by the first field

| cut -d " " -f 2 - remove the temporary first field which we used for sorting

> outputfile - and store the output in "outputfile"
# 6  
Old 09-10-2008
Thanks again, I really appreciate your help. I have been working on a custom scheduling software for an in house project at my work and your help with processing this data has really sped up my progress. Thank you
Dallas
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Best way to sort file with groups of text of 4-5 lines by the first one

Hi, I have some data I have taken from the internet in the following scheme: name direction webpage phone number open hours menu url book url name ... Of course the only line that is mandatory is the name wich is the one I want to sort by. I have the following sed & awk script that... (3 Replies)
Discussion started by: devmsv
3 Replies

2. Shell Programming and Scripting

Sort data in text file in particular format

I have to sort below output in text file in unix bash 20170308 DA,I,113 20170308 PM,I,123 20170308 DA,U,22 20170308 PM,U,123 20170309 DA,I,11 20170309 PM,I,23 20170309 DA,U,123 20170309 PM,U,233 (8 Replies)
Discussion started by: Adfire
8 Replies

3. Shell Programming and Scripting

Sort file by field 1 that has text as well as a number

I am using the below awk that results in the below output: awk '{k=$1 OFS $2; s+=$4; c++} END{for(i in s) print i, s/c}' input.txt > output.txt output.txt chr20:43625799-43625957 STK4:exon.6;STK4:exon.7 310.703 chr20:36770455-36770611 TGM2:exon.6;TGM2:exon.7 614.756... (5 Replies)
Discussion started by: cmccabe
5 Replies

4. Shell Programming and Scripting

How to sort a text file if certain columns are blank?

Dear all, I am trying to sort a text file based on column 3, 10, 11 and 12. But certain column are blank for some lines. Column 3 has to be in ascending order after sorting. Part of my input file is as follows: CN727990 1 A01 4703 5083 73.28 - A_scaffold000011 4365605 4365985 73.28 +... (10 Replies)
Discussion started by: huiyee1
10 Replies

5. Shell Programming and Scripting

Need to sort text keeping first line always first

I have a file is created from standard output. I have put a leading space to force the first line to collate low vis a vis the rest of the lines. If I pass the entire file to the Linux sort, it ignores the leading space and the first line appears in somewhere in the list. If I add lots of... (15 Replies)
Discussion started by: lsatenstein
15 Replies

6. Shell Programming and Scripting

sort each column of text file alone

Hello , i have a text file like this 1 a1 ,AB ,AC ;AD ,EE 2 a2 ,WE ;TR ,YT ,WW 3 a3 ;AS ,UY ;RF ,YT i want to sort this text file based on each row , and excluding 2nd column from the sorting and not taking the comma or ; into consideration in the sorting, so it will become like this... (12 Replies)
Discussion started by: shelladdict
12 Replies

7. Shell Programming and Scripting

sort text file

HI all i have a text file file1 like this 004002004545454000001 041002004545222000002 006003008751525000003 007003008751352000004 006003008751142000005 004001005745745000006 i want to sort the file according to position 1-5 and secondary sort by the last position of file 16-21... (4 Replies)
Discussion started by: naamas03
4 Replies

8. Shell Programming and Scripting

Need Help to sort text lines

I need to sort input file as below to display as below: input.txt User: my_id File: oracle/scripts/ssc/ssc_db_info User: your_id File: pkg_files/BWSwsrms/request User: your_id File: pkg_files/BWSwsco/checkConfig.sh OUTPUT: User: my_id File: ... (3 Replies)
Discussion started by: tqlam
3 Replies

9. Shell Programming and Scripting

Perl Sort on Text File

Hi, I have a file of names and I want perl to do a sort on this file. How can I sort this list of names using perl? I'm thinking of a command like: @sorted = sort { lc($a) cmp lc($b) } @not_sorted # alphabetical sort The only thing I'm sort of unsure of is, how would I get the name in my... (6 Replies)
Discussion started by: eltinator
6 Replies

10. Shell Programming and Scripting

Need a Help with sort a text file with some fields

Ive got a file called listacdrs with this structure: 01/09/2006 12:13 p.m. 1.046.528 CF0155.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0156.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0157.DAT 01/09/2006 12:13 p.m. 1.046.528 CF0158.DAT 01/09/2006 12:14 p.m. ... (3 Replies)
Discussion started by: alexcol
3 Replies
Login or Register to Ask a Question