Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8083919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:37:56+00:00 2026-06-05T17:37:56+00:00

I need to sort a flat file by third column leaving first column intact

  • 0

I need to sort a flat file by third column leaving first column intact [First column is already sorted] (in linux).
(second column may change)

Example i/p file:-

b:di:wave
b:di12:red
b:di12:wave
b:di06:pir

Should look like:-

b:di06:pir
b:di12:red
b:di12:wave
bast:di:wave

I tried several sorting options but I could sort only by second column but not third.

Can someone please help ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T17:37:58+00:00Added an answer on June 5, 2026 at 5:37 pm

    Try this:

    sort  -t: -k1,1 -k3 data.txt
    

    gives:

    bast:disp-san-d5-06:piranha 
    bast:display-san-12:redbird
    bast:display-san-07:waverider
    bast:display-san-12:waverider
    

    This will sort with the 1st field as primary key, and the 3rd field as secondary key splitting the line into fields by :

    Details:

    data.txt contains the 4 lines from your post.

    You can specify multiple fields as sorting keys, see the man page

    -k1,1 means sort on the first field (start at field 1 and end at field 1, otherwise it would continue using the rest of the line for determining the sort)

    -k3 means sort on the 3rd field as secondary key. Since there are no other fields behind it is not necessary to specify -k3,3 but it wouldn’t hurt either.

    -t: means delimit fields in lines with the : character, otherwise blank is used by default

    More information see this SO question Sorting multiple keys with Unix sort and the sort man page

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A third party is sending us a flat file that is supposed to contain
I need to sort a highscore file for my game I've written. Each highscore
i need to sort a large XML file and i've been reading that placing
I need to sort 20+ arrays, already on the GPU, each of the same
I need to sort file names as follows: 1.log, 2.log, 10.log But when I
I need to sort an array of pairs by second element. How do I
This is the first time I have done any sort of work with flat
I need to sort on a date-field type, which name is mod_date. It works
I need to sort close to a 1,00,000 floating point entries in Delphi. I
I need to sort string, and I came up with the following function. def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.