Frank
Frank

Reputation: 2173

Unix - Sort particular output

I need to sort this output:

/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_04092008/es3.sml: some content .... with words and symbols
/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_05012004/es3.sml: some content .... with words and symbols
/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_24072010/es3.sml: some content .... with words and symbols
/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_05012004/es3.sml: some content .... with words and symbols

Based on the year of the date like this "05012004" that would be "ggmmyyyy" so based on "yyyy".

So in the following way it's sorted:

/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_05012004/es3.sml: some content .... with words and symbols
/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_05012004/es3.sml: some content .... with words and symbols
/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_04092008/es3.sml: some content .... with words and symbols
/dir1NameWithSpaces/dir2NameWithSpaces/dir3NameWithSpaces/File1_24072010/es3.sml: some content .... with words and symbols

Thanks

Upvotes: 0

Views: 100

Answers (2)

Chris F.A. Johnson
Chris F.A. Johnson

Reputation: 207

Use the -k option with sort:

sort -t '/' -k5.11

Upvotes: 1

nosid
nosid

Reputation: 50044

You can use sed to extract the sort key, sort the lines and remove the sort key with cut:

sed -e 's/^.*[0-9]\{4\}\([0-9]\{4\}\)/\1,&/' |
sort |
cut -d, -f2-

Upvotes: 1

Related Questions