Unix - Sorting in shell script

2019-03-01 14:19发布

问题:

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, its not working :(

sort  -d -k 3.42,44 -k 4.47,57 -k 5.59,70 -k 8.73,82 


010835         03          0000000010604CAQZ  0912104072  QNZAW AZ ATC  1704698441
010835         03          0000000010604CZWX  7823775785  WDXSD GZ DDF  2804698441
010835         03          0000000010604CBEC  8737518498  DICDC CY HWT  0904698441
010835         03          0000000010604CERV  5648240160  FFVFV DZ UXE  8404698441
010835         03          0000000010604CTTV  2555338251  TTBGB FZ EZS  9504698441
010835         03          0000000010604CADB  1465045344  BINHH TZ QKZ  4604698441
010835         03          0000000010604CIFN  2374902637  NOMJU VZ XHU  6704698441
010835         03          0000000010604COGM  3281553523  JSLKI YZ CLK  5804698441
010835         03          0000000010604CPCL  4190899186  PQJLL QZ UPL  3004698441

回答1:

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?



回答2:

You could try :

sort -d -t $'\n' -k 1.42,1.44 -k 1.47,1.57 -k 1.59,1.70 -k 1.73,1.82 input.txt>

You would get this :

010835         03          0000000010604CADB  1465045344  BINHH TZ QKZ  4604698441
010835         03          0000000010604CAQZ  0912104072  QNZAW AZ ATC  1704698441
010835         03          0000000010604CBEC  8737518498  DICDC CY HWT  0904698441
010835         03          0000000010604CERV  5648240160  FFVFV DZ UXE  8404698441
010835         03          0000000010604CIFN  2374902637  NOMJU VZ XHU  6704698441
010835         03          0000000010604COGM  3281553523  JSLKI YZ CLK  5804698441
010835         03          0000000010604CPCL  4190899186  PQJLL QZ UPL  3004698441
010835         03          0000000010604CTTV  2555338251  TTBGB FZ EZS  9504698441
010835         03          0000000010604CZWX  7823775785  WDXSD GZ DDF  2804698441

the idea is to use the $'\n' (newline) char is field separator so every line is 1 field

Inspiration from http://www.computing.net/answers/unix/sort-file-by-position-/7735.html