Unix - Sorting in shell script

前端 未结 2 1622
我寻月下人不归
我寻月下人不归 2021-01-24 09:18

How to sort a file based on field postion?

For eg. I need to sort the below given file. Based on 4th,5th and 8th positions. Please help. I tried the following command, i

2条回答
  •  梦谈多话
    2021-01-24 10:03

    Try this command:

    sort -k4,4 -k5,5 -k8,8 input.txt
    

    From the sort manual:

       -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.
    

    In your command:
    -k 3.42,44 means start from (42th char of 3rd field) to (44th field).
    Do you mean -k 3.42,3.44?

提交回复
热议问题