Two-column sorting using shell and Python, respectively

Source: Internet
Author: User

There is a file 1.txt below, requires the use of the shell and Python implementation of the sorting, first sorted by the first column, the first column is the same and then sorted by the second column, and output to another file 2.txt

lzdemacbook-pro:~ lz$ Cat 1.txt

D

-T

Y

H

5 k

A

Shell implementations:

lzdemacbook-pro:~ lz$ sort-k1,1n-k2,2 1.txt > 2.txt

lzdemacbook-pro:~ lz$ Cat 2.txt

5 k

A

Y

D

-T

H

Python implementations:

Sort_lines = sorted (open ("1.txt"), KEY=LAMBDA L: (Int (L.split ("") [0]), L.split ("") [1])

Open ("2.txt", "W"). Write ("". Join (Sort_lines))

Two-column sorting using shell and Python, respectively

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.