Sorting group of records and loading last record


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting group of records and loading last record
# 1  
Old 01-20-2015
Sorting group of records and loading last record

Hi Everyone,
I have below record set. File is fixed widht file

Code:
101newjersyus 20150110
101nboston us 20150103
102boston   us 20140106
102boston   us 20140103

I need to group record based on first 3 letters in our case(101 and 102)
and sort last 8 digit in ascending order and print only last record
So output should be
Code:
101newjersyus 20150110
102boston   us 20140106


Last edited by Don Cragun; 01-20-2015 at 09:39 PM.. Reason: Add CODE tags again.
# 2  
Old 01-21-2015
How about this awk + sort solution

Code:
awk '
{ k=substr($0,1,3)
  v=substr($0,length-8)

  if(a[k]<v) {
     a[k]=v
     l[k]=$0
  }
}
END { for(k in a) print l[k] }' infile | sort

# 3  
Old 01-21-2015
Quote:
Originally Posted by patricjemmy6
Hi Everyone,
I have below record set. File is fixed widht file

Code:
101newjersyus 20150110
101nboston us 20150103
102boston   us 20140106
102boston   us 20140103

I need to group record based on first 3 letters in our case(101 and 102)
and sort last 8 digit in ascending order and print only last record
So output should be
Code:
101newjersyus 20150110
102boston   us 20140106

You said that the input file is fixed width, but as can clearly be seen above (now that CODE tags have been added), some lines are longer than others. If the lines were fixed length (or more importantly, if the date at the ends of these lines started in the same character position in all lines), Chubler_XL's awk | sort pipeline could be changed to a sort | awk pipeline with a slightly more complex sort command and a much simpler awk command. But, since your secondary sort key is not in a fixed field, and is not in a fixed position; that won't work.
# 4  
Old 01-21-2015
Were it a fixed width file, things would be much simpler. The following does work on your sample but will fail if positions of spaces and fields shift:
Code:
sort -t. -k1.1,1.3n -k1.14r file4 | sort -uk1.1,1.3
101newjersyus 20150110
102boston   us 20140106

# 5  
Old 01-21-2015
Quote:
Originally Posted by RudiC
Were it a fixed width file, things would be much simpler. The following does work on your sample but will fail if positions of spaces and fields shift:
Code:
sort -t. -k1.1,1.3n -k1.14r file4 | sort -uk1.1,1.3
101newjersyus 20150110
102boston   us 20140106

This will work on many systems. Unfortunately the standards are silent as to which line with identical keys will be printed by sort -u so there is no guarantee that it will always print the 1st line in each set of lines with identical strings in the 1st three characters of the line.

The following should work portably (as long as the 8 digit date string starts in column 13 or 14 and is preceded by a space if it starts in column 14):
Code:
sort -t. -k1.1,1.3n -k1.14r file|awk '(k=substr($0,1,3))!=K{print;K=k}'

But, of course, if you want to try this on a Solaris/SunOS system, you'd have to change awk to /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk.
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting a file of book records

I have a texinfo file containing book records and I want to sort them. An example is shown below. Records are separated by two blank lines. The sort pattern I want to sort is starting from the beginning of the year declaration and finishing at the beginning of the book title where I use the... (4 Replies)
Discussion started by: Danette
4 Replies

2. UNIX for Dummies Questions & Answers

keeping last record among group of records with common fields (awk)

input: ref.1;rack.1;1 #group1 ref.1;rack.1;2 #group1 ref.1;rack.2;1 #group2 ref.2;rack.3;1 #group3 ref.2;rack.3;2 #group3 ref.2;rack.3;3 #group3 Among records from same group (i.e. with same 1st and 2nd field - separated by ";"), I would need to keep the last record... (5 Replies)
Discussion started by: beca123456
5 Replies

3. Shell Programming and Scripting

Multiple Records from 1 Record

I need to make one record to multiple records based on occurence column in the record and change the date.For example below first record has 5 ,so need to create 5 records from one and change the date to 5 months.Occurence can be any number. I am unable to come with a script.Can some one help ... (5 Replies)
Discussion started by: traininfa
5 Replies

4. Shell Programming and Scripting

reformat one record from two records

I have not get much answer/solution for the posting. Here I break down the question and hope to get some help. 1. How can I use AWK to read in two records at the same time and keep loop to next two when the condition is meet? position 1-10 --> Unique to identity whether... (4 Replies)
Discussion started by: menglm
4 Replies

5. Shell Programming and Scripting

Sorting within a record using AWK

Hello, I have a file which has the following format: I have to do is sort individual records in the file based on the 4th field. Each record starts with "Module". Is there an easy way to do this using awk. I have tried piping output from awk to sort and also using "sort" inside awk but... (8 Replies)
Discussion started by: fifteate
8 Replies

6. Shell Programming and Scripting

Sorting the records for a file

hi, Please suggest me how to do this logic say i am dynamically taking a file name into the script. when ever a file name it should sort the all the records based on the first character in every line except the heading line and ending line. for example file1 heading... (1 Reply)
Discussion started by: angel12345
1 Replies

7. Shell Programming and Scripting

Sorting record

Hi all, Can any one help whether we can able to sort a record with delimiter plz ? (3 Replies)
Discussion started by: thelakbe
3 Replies

8. Shell Programming and Scripting

Please help in sorting record

dn: uid=peter@exmaple.com,ou=example-com,ou=mail,dc=example,dc=to cn: Peter sn: Norton displayName: Peter Norton dn: uid=ras@exmaple.com,ou=example-com,ou=mail,dc=example,dc=to cn: Ras sn: Kam displayName: Ras Kam i have a text file with 300 entries with multiple ldap entries... (5 Replies)
Discussion started by: learnbash
5 Replies

9. Shell Programming and Scripting

Sorting the records in the Flat file

Hi all, I am using this command "sort -d -u -k1 IMSTEST.74E -o tmp.txt" to the records in the flat. Can any tell me how to sort the file except first line in the file For ex: i/p First line: DXYZ Second line : jumy third : cmhk fourth : andy Output should... (5 Replies)
Discussion started by: sudhir_barker
5 Replies

10. UNIX for Advanced & Expert Users

Parsing records from one record

Hi, I got a file which is one huge record. I know each record should be 550 bytes long. How do I parse out the records from the one huge record. (1 Reply)
Discussion started by: bwrynz1
1 Replies
Login or Register to Ask a Question