Sorting based on a particular colum


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sorting based on a particular colum
# 8  
Old 07-30-2012
Operating System is SunOS.

Couldnt get the sort version
Code:
sort: illegal option -- version


Last edited by Scott; 07-30-2012 at 07:00 AM.. Reason: Code tags
# 9  
Old 07-30-2012
If maintaining the original order of lines is really important for lines with matching 2nd fields, this should work for you:
Code:
nl -ba -fa -ha -p -s"|" input|sort -t"|" -k3,3 -k1,1n|cut -d"|" -f2-

Note that your desired output is missing the following line from the input file:
Quote:
ACUST|K50034|400|1|99|999|||6403996075|
# 10  
Old 07-30-2012
Quote:
Originally Posted by Don Cragun
If maintaining the original order of lines is really important for lines with matching 2nd fields, this should work for you
Exactly. Basically, you prepend every line with a line number, then sort - which will again result in first sorting along the sort key, then sorting by beginning of line as a secondary key - and as a final step removing the line number again.

<snipped> some apparent nonsense example i credit to not being fully awake at all.

I hope this helps.

bakunin

Last edited by bakunin; 07-30-2012 at 07:12 AM..
# 11  
Old 07-30-2012
Quote:
Originally Posted by bakunin
Exactly. Basically, you prepend every line with a line number, then sort - which will again result in first sorting along the sort key, then sorting by beginning of line as a secondary key - and as a final step removing the line number again.

The same effect would have to use (note that it is basically O/Ps second variation, which he says is not working correctly - 3rd field instead of 2nd field, just because we prepend one field to every line):

Code:
sed 's/^/'=|/' sampleinput.dat | sort -t "|" -k 3.1,3.6 | cut -d'|' -f2-

"=" in the right side of a regexp evaluates to the line number. In fact this is just an application of what i said: make sure you have distinct key contents.

I hope this helps.

bakunin
You lost me. You have mismatched single quotes on your sed statement.

I'm assuming you meant
Code:
sed 's/^/=|/' sampleinput.dat | sort -t "|" -k 3.1,3.6 | cut -d'|' -f2-

There is an "=" command in sed, but it is not a flag that can be used on an "s" command and it is not a special character in the replacement string on a substitution on the versions of the sed utility that I use.

Even if you were able to join the output of the = command and a "|" onto the start of a line, you'd still need the second -k option to sort because you need an n flag for that field. If you sort on line numbers without the -n flag, the input:
Quote:
1
2
3
4
5
6
7
8
9
10
11
12
would sort to
Quote:
1
10
11
12
2
3
4
5
6
7
8
9
Note that the second sort key and (with an n flag) wasn't needed (even though I used it) when using nl because the default line numbering format it uses is a right justified six digit field with leading <space> fill (which will sort correctly both as a string and as a numeric field). The sed = command, however, produces a variable width field with no leading spaces and no leading zeros to make all line numbers occupy the same number of columns.
# 12  
Old 07-30-2012
Quote:
Originally Posted by Don Cragun
You lost me. You have mismatched single quotes on your sed statement.
True - my bad. Somehow the superfluous single quote slipped in upon copying from my terminal to the edit window.

Quote:
Originally Posted by Don Cragun
There is an "=" command in sed, but it is not a flag that can be used on an "s" command and it is not a special character in the replacement string on a substitution on the versions of the sed utility that I use.
Even more true - somehow several lines from my terminal got intermingled without me noticing that and, even worse, confusing even my thoughts. I apologize for answering threads before having had the first coffee of the day. (i do hope this being the case - getting senile, after all, is an even bleaker prospect.)

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting based on File name

Hi All I have a requirement to list all the files in chronological order based on the date value in the file name.For ex if I have three files as given below ABC_TEST_20160103_1012.txt ABC_TEST_20160229_1112.txt ABC_TEST_20160229_1112.txt I have written code as given below to list out... (2 Replies)
Discussion started by: ginrkf
2 Replies

2. Shell Programming and Scripting

Sorting based on filename

Hello , I have to write a bash script. I will explain the logic based on a scenario. Scenario : Suppose I have few files in a Folder X : FILE_201508.list FILE_201510.list FILE_201507.list abc_201510.csv xyz_201508.csv abc_201507.csv def_201507.csv 1) Now ,... (3 Replies)
Discussion started by: smamrm
3 Replies

3. Shell Programming and Scripting

Sorting based on the second field

Oracle Enterprise Linux 6 This is my file. Two fields separated by space $ cat testfile.txt MARCH9 MARCH4 MARCH1 MARCH5 MARCH2 MARCH326 MARCH821 MARCH7 MARCH6 MARCH2 $ $ The following numeric sort, based on the first field's 6th character works as expected. $ $ sort -n -k 1.6... (7 Replies)
Discussion started by: John K
7 Replies

4. Shell Programming and Scripting

Sorting file based on name

Hi team, We have few files landing to our server based on sequence number. These files have to be processed in the sequence number order. Once the sequence number has reached its maximum, the files with sequence number 0000 has to be processed. For example: IN9997 IN9998 IN9999 IN0000... (7 Replies)
Discussion started by: anijan
7 Replies

5. Shell Programming and Scripting

sorting based on a field

the below is sorted as it is. the fields that i'm interested in are the 4th and 5th field. i want to sort the based on the 4th field. my past attempt to do this was to do something like this: awk '{print $4}'| awk '{print $1":"$2}' datafile | sort | uniq however, if i do that, i lose... (2 Replies)
Discussion started by: SkySmart
2 Replies

6. UNIX for Dummies Questions & Answers

Sorting words based on length

i need to write a bash script that recive a list of varuables kaka pele ronaldo beckham zidane messi rivaldo gerrard platini i need the program to print the longest word of the list. word in the output appears on a separate line and word order in the output is in the order Llachsicografi costs.... (1 Reply)
Discussion started by: yairpg
1 Replies

7. Shell Programming and Scripting

sorting based on alternative lines

Hello, I have a file with multiple entries. @SFGF-GA2-1_58:5:36:11009:999#0/1 NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN +SFGF-GA2-1_58:5:36:11009:999#0/1 ################################################################################... (10 Replies)
Discussion started by: Diya123
10 Replies

8. Shell Programming and Scripting

Sorting based on multiple delimiters

Hello, I have data where words are separated by a delimiter. In this case "=" The number of delimiters in a line can vary from 4to 8. The norm is 4. Is it possible to have a script where the file could be separated starting with highest number of delimiters and ending with the lowest An... (8 Replies)
Discussion started by: gimley
8 Replies

9. Shell Programming and Scripting

Sorting based on Multiple columns

Hi, I have a requirement whereby I have to sort a flat file based on Multiple Columns (similar to ORDER BY Clause of Oracle). I am getting 10 columns in the flat file and I want the file to be sorted on 1st, 3rd, 4th, 7th and 9th columns in ascending order. The flat file is pipe seperated. Any... (15 Replies)
Discussion started by: dharmesht
15 Replies

10. Shell Programming and Scripting

Sorting based on columns

Hi, I want a list of entries in 3 space delimited columns. I want to sort entries based on the very first column. Rows can't be changed. For example: If I have... Abc Abc Acc Bca Bda Bdd Cab Cab Cbc Dbc Dca Dda Abc Abc Acc the output should be... Abc Abc Acc Abc Abc Acc Bca... (7 Replies)
Discussion started by: MobileUser
7 Replies
Login or Register to Ask a Question