Linux shell sort file according to the second column?

后端 未结 4 1573
囚心锁ツ
囚心锁ツ 2020-12-02 17:54

I have a file like this:

FirstName, FamilyName, Address, PhoneNumber

How can I sort it by FamilyName?

4条回答
  •  借酒劲吻你
    2020-12-02 18:34

    If this is UNIX:

    sort -k 2 file.txt
    

    You can use multiple -k flags to sort on more than one column. For example, to sort by family name then first name as a tie breaker:

    sort -k 2,2 -k 1,1 file.txt
    

    Relevant options from "man sort":

    -k, --key=POS1[,POS2]

    start a key at POS1, end it at POS2 (origin 1)

    POS is F[.C][OPTS], where F is the field number and C the character position in the field. OPTS is one or more single-letter ordering options, which override global ordering options for that key. If no key is given, use the entire line as the key.

    -t, --field-separator=SEP

    use SEP instead of non-blank to blank transition

提交回复
热议问题