sorting - Sort the file in unix using first six characters of a line -


i want sort file using first 6 characters of line. should ignore default sort order after sixth character. have tried using below command, system takes default sort order after sixth character.

sort -k 1,6 filename.txt 

input file : "filename.txt"

09289720150531n201505220820d20150514 09289720150531n201505220820a20150516 08806020150531n201505290810d20150526 08806020150531n201505290810a20150528 

output should be:

08806020150531n201505290810d20150526 08806020150531n201505290810a20150528 09289720150531n201505220820d20150514 09289720150531n201505220820a20150516 

but command output is:

08806020150531n201505290810a20150528 08806020150531n201505290810d20150526 09289720150531n201505220820a20150516 09289720150531n201505220820d20150514 

the option shown uses field position. if change -k1.1,1.6 use character position in first field. extended posix feature, provided on platforms.

however, in example there 2 distinct values in character positions 1-6: 088060 , 092897. standard sort command not have feature ignoring columns, using columns. while gnu sort provides extension (-s "disabling last-resort comparison"), solaris sort not have such extension. after sort-keys have been taken account, sorts remainder of lines.

there is vague wording in manual hints -u want:

when there multiple key fields, later keys compared after earlier keys compare equal. except when -u option specified, lines otherwise compare equal ordered if none of options -d, -f, -i, -n or -k present (but -r still in effect, if specified) , bytes in lines significant comparison.

however — revisiting — wording misleading since -u used filter duplicates.

a comment suggests -k1.1,1.6 shortened -k1.6, , testing solaris 10 confirmed work. /usr/bin/sort, of course. on copy of solaris 10, there additional copy of sort, in /opt/sfw/bin/sort:

$ /opt/sfw/bin/sort --version sort (gnu coreutils) 5.97 

and that program supports -s option noted above. option, program produces output requested.


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -