Sort based on positions in flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sort based on positions in flat file
# 1  
Old 11-25-2013
Sort based on positions in flat file

Hello,

For example:

Code:
12........6789101112..............20212223242526..................50  ( Positions)
LName       FName                     DOB                                    (Lastname starts from 1 to 6 , FName from 8 to 15 and date of birth from 21 to29)
 
CURTIS     KENNETH                   19770607
WALKER    CHARLES                   19380105
COHAN      TREVA                      19510506

I want to sort above records based on First name, lastname and Date of Birth using Unix Script.
I have stated specific positions for firstname, last name and date of birth in the above.

Output look like

Code:
COHAN      TREVA                      19510506
CURTIS     KENNETH                   19770607
WALKER    CHARLES                   19380105

How can we achieve this?
Moderator's Comments:
Mod Comment We added CODE tags to your original posting. You then removed them when you changed your sample data. Without the CODE tags, your statements about column positions make absolutely no sense. Please use CODE tags as required by forum rules. I have no idea whether I have placed the CODE tags in the proper place. Some of the line I included may be headers that you want to be ignored, or they might just be descriptive text before the contents of your sample input file.

Last edited by Don Cragun; 11-25-2013 at 04:35 PM.. Reason: Code tags
# 2  
Old 11-25-2013
Code:
bash-3.2$ cat x
12345678910121314151617181920 (Positions)
SAchin      Tendular           100
Virat         Kolhi                 99
AAA          BBBB                00
BBB           XXX                 10
bash-3.2$ 
bash-3.2$ sort -nr -k 3 x
SAchin      Tendular           100
Virat         Kolhi                 99
BBB           XXX                 10
AAA          BBBB                00
12345678910121314151617181920 (Positions)

# 3  
Old 11-25-2013
Quote:
Originally Posted by duplicate
Hi,

Code:
12345678910121314151617181920 (Positions)
SAchin      Tendular           100
Virat         Kolhi                 99
AAA          BBBB                00
BBB           XXX                 10


How can sort above records in alphabetical order with Positions in unix script?
I don't understand what
Quote:
in alphabetical order with Positions
means, but I'm guessing that reverse numeric order of the values in the 3rd column (as provided by MR.bean's suggestion) is not what you want.

Please explain more clearly the fields you want to be used as sort keys and show us the output you want to be produced.
# 4  
Old 11-25-2013
Please use code tags as required by forum rules!

You seem to have edited your initial post#1 after the comments of Don Cragun. Still, due to missing code tags, your input sample does NOT obey the specification (field positions) that you have given. Plus, the desired output does NOT conform to your specs, either - it is sorted for last name, not first name as desired. Try this (based on guesses and input modified by me to be reasonable) and come back with results/comments:
Code:
sort -k1.8,1.15 -k1.1,1.6 -k1.21,1.29n file

# 5  
Old 11-25-2013
Now that I have put CODE tags back into the modified 1st message in this thread, we can see that the DOB and FName fields are not in the columns specified.

Ignoring header lines that might or might not be part of the input file, sorting by FName and LName can be done with the command:
Code:
sort -k 2,2 -k1,1 file

If you want to sort by LName and FName (as shown in the sample output you said you want), that can be done with the command:
Code:
sort file

# 6  
Old 11-26-2013
python OR perl

python
Code:
def leo(x):
 return(x[0],x[1],int(x[2]))
d=[]
with open("a.txt") as file:
 for line in file:
  line=line.replace("\n","")
  items = line.split(" ")
  d.append(items)
for i in sorted(d,key=leo):
 print(i)

perl
Code:
my @result;
while(<DATA>){
	my @arr=split(" ",$_);
	push @result,\@arr;
}
for my $val (sort {$a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] || $a->[2] <=> $b->[2]} @result){
	print join " ", @$val;
	print "\n";
}
__DATA__
c d 10
a b 20
d c 10
c a 10
c d 20
b b 100
d a 13
d c 8

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Filter lines based on values at specific positions

hi. I have a Fixed Length text file as input where the character positions 4-5(two character positions starting from 4th position) indicates the LOB indicator. The file structure is something like below: 10126Apple DrinkOmaha 10231Milkshake New Jersey 103 Billabong Illinois ... (6 Replies)
Discussion started by: kumarjt
6 Replies

2. UNIX for Dummies Questions & Answers

Filling positions based on frequency

I have files with hundreds of sequences with frequency values reported as "Freq X" and missing characters represented by a dash ("-"), something like this >39sample Freq 4 TAGATGTGCCCGTGGGTTTCCCGTCAACACCGGATAGTAGCAGCACTA >22sample Freq 15 T-GATGTCGTGGGTTTCCCGTCAACACCGGCAAATAGTAGCAGCACTA... (12 Replies)
Discussion started by: Xterra
12 Replies

3. Shell Programming and Scripting

Join based on positions

I have two text files as shown below cat file1.txt Id leng sal mon 25671 34343 56565 5565 44888 56565 45554 6868 23343 23423 26226 6224 77765 88688 87464 6848 66776 23343 63463 4534 cat file2.txt Id number 25671 34343 76767 34234 23343 23423 66776 23343 (4 Replies)
Discussion started by: halfafringe
4 Replies

4. Shell Programming and Scripting

Getting data from a flat file based on condition

Hi, I have a flaty file from which i am fetching few columns in tablular form as below code. Now i want to fetch the column 6 and 7 in below code only if it either of them is non zero.However below startement awk -F, '$6==0 && $7==0{exit 1}' ${IFILE} is not working..Not sure where is the... (36 Replies)
Discussion started by: Vivekit82
36 Replies

5. Shell Programming and Scripting

Sort flat file by 3rd column in perl

Hello Guys I want to sort a flat file by the third column (numeric ) and store it in some other name I/P 9924873|20111114|00000000000013013|130|13|10/15/2010 12:36:22|W860944|N|00 9924873|20111114|00000000000013009|130|09|10/15/2010 12:36:22|W860944|N|00... (12 Replies)
Discussion started by: Pratik4891
12 Replies

6. Shell Programming and Scripting

seds to extract fields based on positions

Hi My file has a series of rows up to 160 characters in length. There are 7 columns for each row. In each row, column 1 starts at position 4 column 2 starts at position 12 column 3 starts at position 43 column 4 starts at position 82 column 5 starts at... (7 Replies)
Discussion started by: malts18
7 Replies

7. Shell Programming and Scripting

Filling positions based on consensus character

I have files with hundreds of sequences with missing characters represented by a dash ("-"), something like this I need to go sequence by sequence and if a dash is found, it should be replaced with the most common character in that particular position. Thus, in my example the dash in the second... (6 Replies)
Discussion started by: Xterra
6 Replies

8. Shell Programming and Scripting

Shell script to email based on flat file output

Hi All, I am a newbee in unix but still have written a shell script which should trigger a mail based on certain conditions but the problem is that my file is not being read. Below is the code please advise. I do not know where is it failing. Note $ and the no followed with it is the no of... (1 Reply)
Discussion started by: apoorva
1 Replies

9. Shell Programming and Scripting

Merge lines in Flat file based on first 5 characters

Hi I have the fixed width flat file having the following data 12345aaaaaaaaaabbbbbbbbbb 12365sssssssssscccccccccc 12365sssss 12367ddddddddddvvvvvvvvvv 12367 vvvvv Here the first column is length 5 second is length 10 third is length 10 if the second or third column exceeds... (3 Replies)
Discussion started by: Brado
3 Replies

10. Shell Programming and Scripting

Sorting a flat file based on multiple colums(using character position)

Hi, I have an urgent task here. I am required to sort a flat file based on multiple columns which are based on the character position in that line. I am restricted to use the character position instead of the space and sort +1 +2 etc to do the sorting. I understand that there is a previous... (8 Replies)
Discussion started by: cucubird
8 Replies
Login or Register to Ask a Question