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 8791427
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:50:29+00:00 2026-06-13T22:50:29+00:00

In unix (specifically, I’m using bash ), I can sort the contents of a

  • 0

In unix (specifically, I’m using bash), I can sort the contents of a file easily; given I have a file:

chris@chris:~$ cat testSort.txt 
3       1       test01
8       2       test02
6       3       test03
7       4       test04
1       5       test05

Running sort will return the sorted values based on the 1st column

chris@chris:~$ sort testSort.txt
1       5       test05
3       1       test01
6       3       test03
7       4       test04
8       2       test02

the results of which I can pipe into a file or a program as necessary; is there a way as easy as this in python, or do I need to read my file in, save it as a data structure of some sort, and then save it again?

Confusion

I’m not sure why I’ve received a downvote – I asked if there was a way as simple as in unix (i.e. a 1 word command) to achieve a sort. I even provided an example to show what I wanted? In what way does this mean that “the question shows no research effort, it is unclear or not useful”?

  • 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-13T22:50:30+00:00Added an answer on June 13, 2026 at 10:50 pm

    How easily do you want to do it? This sorts lexicographically, but you can modify it to sort based on any arbitrary criterion you have by passing an additional key function to sorted.

    with open('input') as f:
        sorted_file = sorted(f)
    
    #save to a file
    with open('output') as f:
        f.writelines(sorted_file)
    
    #write to stdout
    import sys
    sys.stdout.writelines(sorted_file)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The UNIX sort command can sort a very large file like this: sort large_file
On Unix, everything is a file, so you can use file i/o functions with
On Unix, we can specify the ftp username/password in ~/.netrc file and next time
Is there any (unix) command that can locate a given directory? So for instance,
On the UNIX bash shell (specifically Mac OS X Leopard) what would be the
How can I convert a COleDateTime::GetCurrentTime() to a unix time stamp or more specifically
I have JUST started looking into the world of scripting, specifically with with bash,
Unix 'file' command has a -0 option to output a null character after a
In Unix we can suspend a process execution temporarily and resume it with signals
In Unix, I have got three main files. One of them is a library

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.