Need a Help with sort a text file with some fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need a Help with sort a text file with some fields
# 1  
Old 02-16-2007
Tools 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. 466.032 CF0163.DAT
01/09/2006 12:14 p.m. 596.848 CF0162.DAT
01/09/2006 12:14 p.m. 784.896 CF0161.DAT
01/09/2006 12:14 p.m. 948.416 CF0160.DAT
01/09/2006 12:14 p.m. 1.046.528 CF0159.DAT
02/08/2006 08:09 a.m. 40.880 CF2673.DAT
02/08/2006 08:09 a.m. 40.880 CF2675.DAT
02/08/2006 08:09 a.m. 49.056 CF2671.DAT
02/08/2006 08:09 a.m. 57.232 CF2672.DAT
02/08/2006 08:09 a.m. 57.232 CF2674.DAT
07/08/2006 08:09 a.m. 57.232 CF2676.DAT
02/08/2006 08:09 a.m. 57.232 CF2678.DAT
02/08/2006 08:09 a.m. 65.408 CF2677.DAT
08/08/2006 08:09 a.m. 73.584 CF2670.DAT
02/08/2006 08:09 a.m. 73.584 CF2679.DAT
02/08/2006 08:09 a.m. 89.936 CF2668.DAT
12/08/2006 08:09 a.m. 89.936 CF2669.DAT
02/08/2006 08:09 a.m. 106.288 CF2667.DAT
.....
(it has more rows up to the last day of the month.

The FS (Field Separator is a TAB)

I need to sort this file the first field first by month and second by day thirb by year. The first field is dd/mm/yyyy. ive tried different says without success.
The output must be:
02/08/2006 08:09 a.m. 73.584 CF2670.DAT
02/08/2006 08:09 a.m. 73.584 CF2679.DAT
02/08/2006 08:09 a.m. 89.936 CF2668.DAT
02/08/2006 08:09 a.m. 89.936 CF2669.DAT
02/08/2006 08:09 a.m. 106.288 CF2667.DAT
02/08/2006 08:09 a.m. 73.584 CF2670.DAT
03/08/2006 08:09 a.m. 73.584 CF2679.DAT
03/08/2006 08:09 a.m. 89.936 CF2668.DAT
04/08/2006 08:09 a.m. 89.936 CF2658.DAT
04/08/2006 08:09 a.m. 106.288 CF2677.DAT
..
01/09/2006 13:40 p.m 89.936 CF2668.DAT


i found a way to sort this file but i had to change the structure: the comand line is
awk 'BEGIN {OFS="/"} {print $1,$2,$3,$4,$5}' listacdrs|sort -t/ +1 -3 > lista1:
The output of the comamnd is :
cat lista1
02/08/2006/08:31/a.m./1.046.528/CF2910.DAT
02/08/2006/08:31/a.m./1.046.528/CF2911.DAT
02/08/2006/08:31/a.m./1.046.528/CF2912.DAT
02/08/2006/08:31/a.m./1.046.528/CF2913.DAT
02/08/2006/08:31/a.m./1.046.528/CF2914.DAT
02/08/2006/08:31/a.m./1.046.528/CF2915.DAT
02/08/2006/08:31/a.m./425.152/CF2919.DAT
02/08/2006/08:31/a.m./523.264/CF2918.DAT
02/08/2006/08:31/a.m./662.256/CF2917.DAT
02/08/2006/08:31/a.m./883.008/CF2916.DAT
03/08/2006/07:41/a.m./1.046.528/CF2947.DAT
03/08/2006/07:41/a.m./1.046.528/CF2948.DAT
03/08/2006/07:41/a.m./1.046.528/CF2949.DAT
03/08/2006/07:41/a.m./114.464/CF2926.DAT
03/08/2006/07:41/a.m./114.464/CF2927.DAT
03/08/2006/07:41/a.m./122.640/CF2925.DAT
03/08/2006/07:41/a.m./138.992/CF2924.DAT
03/08/2006/07:41/a.m./147.168/CF2939.DAT
03/08/2006/07:41/a.m./171.696/CF2923.DAT
03/08/2006/07:41/a.m./179.872/CF2940.DAT
it sorts this file in this way.

But is there any easier way to sort this file without changing ths structure?
Thansk for your help.
# 2  
Old 02-16-2007
Code:
sort -t"/" -k3.1,3.4 -k2n file

# 3  
Old 02-18-2007
Tools Your sort command is work ok. thanks. but would you explain me exactly how it works?

Thanks anbu123 for your help. but i dint understand exactly the parameters you use. i see -k3 is the key field number 3 (year) isnt it?. -k2 mean sort by monh in a numeric way, isnt it?. what does the colon (,) and the dot (.)mean?.
I would be grateful to you once again.
# 4  
Old 02-19-2007
Quote:
i see -k3 is the key field number 3 (year) isnt it?.
You are right.
Quote:
k2 mean sort by monh in a numeric way, isnt it?.
You are right.

k3.1,3.4 Sort from first to fourth char of third field
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rearrange fields of delimited text file

I want to rearrange the fields of delimited text file after sorting first line (only): input file: a_13;a_2;a_1;a_10 13;2;1;10 the result should be: a_1;a_2;a_10;a_13 1;2;10;13 any help would be appreciated andy (20 Replies)
Discussion started by: andy2000
20 Replies

2. Shell Programming and Scripting

How to Modify a file content in UNIX and sort for only required fields ?

I have the below contents in a file after making the below curl call curl ... | grep -E "state|Rno" | paste -sd',\n' | grep "Disconnected" > test "state" : "Disconnected",, "Rno" : "5554f1d2" "state" : "Disconnected",, "Rno" : "10587563" "state" : "Disconnected",, "Rno" :... (2 Replies)
Discussion started by: Vaibhav H
2 Replies

3. Shell Programming and Scripting

Parse file for fields and specific text

I have a file of ~500,000 entries in the following: file.txt chr1 11868 12227 ENSG00000223972.5 . + HAVANA exon . gene_id "ENSG00000223972.5"; transcript_id "ENST00000456328.2"; gene_type "transcribed_unprocessed_pseudogene"; gene_status "KNOWN"; gene_name "DDX11L1"; transcript_type... (17 Replies)
Discussion started by: cmccabe
17 Replies

4. Shell Programming and Scripting

awk sort based on difference of fields and print all fields

Hi I have a file as below <field1> <field2> <field3> ... <field_num1> <field_num2> Trying to sort based on difference of <field_num1> and <field_num2> in desceding order and print all fields. I tried this and it doesn't sort on the difference field .. Appreciate your help. cat... (9 Replies)
Discussion started by: newstart
9 Replies

5. Shell Programming and Scripting

number of fields in a text file as a variable - perl

I am looking for perl code to get following o/p. If a line has more than 7 fields then value in field 7 onwards is BHA_GRP1, BHA_GRP2, BHA_GRP3, BHA_GRP4 etc. Here is example of what I am trying to achieve. INPUT File: VAH NIC_TYPE CONFIG SIZE_GB PILO KOM BHA_GRP1 BHA_GRP2 BHA_GRP3...... 2... (1 Reply)
Discussion started by: dynamax
1 Replies

6. 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

7. Shell Programming and Scripting

Getting required fields from a text file in UNIX

My data is something like as shown below. Out of this i want the details of alarms (ex: 1947147711,1947147081......) and the fields( ex :sw=tacmwafabb9:shelf=1:slot=5-2:pport=2) Once i have these details separated, i want the count of these excluding the duplicates. What is the best possible way... (7 Replies)
Discussion started by: rdhanek
7 Replies

8. Shell Programming and Scripting

Perl function to sort a file based on key fields

Hi, I am new to PERL.I want to sort all the lines in a file based on 1,2 and 4th filelds. Can U suggest me a command/function in perl for this operation.. (5 Replies)
Discussion started by: karthikd214
5 Replies

9. UNIX for Dummies Questions & Answers

Sort the fields in a comma delimited file

Hi, I have a comma delimited file. I want to sort the fields alphabetically and again store them in a comma delimited file. For example, My file looks like this. abc,aaa,xyz,xxx,def pqr,ggg,eee,iii,qqq zyx,lmo,pqr,abc,fff and I want my output to look like this, all fields sorted... (3 Replies)
Discussion started by: swethapatil
3 Replies

10. UNIX for Dummies Questions & Answers

extract fields from text file using delimiter!!

Hi All, I am new to unix scripting, please help me in solving this assignment.. I have a scenario, as follows: 1. i have a text file(read1.txt) with the following data sairam,123 kamal,122 etc.. 2. I have to write a unix... (6 Replies)
Discussion started by: G.K.K
6 Replies
Login or Register to Ask a Question