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

  • SEARCH
  • Home
  • 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 7598563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:26:12+00:00 2026-05-30T22:26:12+00:00

I have a CSV-like file, and I would like to sort it by column

  • 0

I have a CSV-like file, and I would like to sort it by column priority, like "ORDER BY" in SQL. For example, given the following rows,

3;1;2
1;3;2
1;2;3
2;3;1
2;1;3
3;2;1

If "ORDER BY" were column2, column1, column3, the result would be:

2;1;3
3;1;2
1;2;3
3;2;1
1;3;2
2;3;1

I’d like to know how to get this same result using the sort command on Unix.

  • 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-05-30T22:26:14+00:00Added an answer on May 30, 2026 at 10:26 pm

    You need to use two options for the sort command:

    • --field-separator (or -t)
    • --key=<start,end> (or -k), to specify the sort key, i.e. which range of columns (start through end index) to sort by. Since you want to sort on 3 columns, you’ll need to specify -k 3 times, for columns 2,2, 1,1, and 3,3.

    To put it all together,

    sort -t ';' -k 2,2 -k 1,1 -k 3,3
    

    Note that sort can’t handle the situation in which fields contain the separator, even if it’s escaped or quoted.

    Also note: this is an old question, which belongs on UNIX.SE, and was also asked there a year later.


    Old answer: depending on your system’s version of sort, the following might also work:

    sort --field-separator=';' --key=2,1,3
    

    Or, you might get "stray character in field spec".

    According to the sort manual, if you don’t specify the end column of the sort key, it defaults to the end of the line.

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

Sidebar

Related Questions

I have a CSV file that is formatted like: 0.0023709,8.5752e-007,4.847e-008 and I would like
I have a 4-column CSV file. I want to sort the lines, such that,
I have a CSV file with several thousand customer's details. I would like to
I have a CSV file that I would like to use as source data
I have a csv file encoded in UCS-2 Little Endian which I would like
Ok, I have a csv file like this: 14 ; 1234,56 ; 10203 ;
I have a CSV file formatted just like this: name,color,tasty,qty apple,red,true,3 orange,orange,false,4 pear,greenish-yellowish,true,1 As
I have various time-series I'd like to correlate and present as either a csv-file
I have a csv that looks like this: blah,blah, blah, blah ect, ect,column 3
I have a CSV file, formatted like this: 0001 @ word @ some information

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.