本文介绍了Unix-在shell脚本中排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

如何根据字段位置对文件进行排序?

How to sort a file based on field postion?

例如我需要对以下给定的文件进行排序.基于第4、5和8位.请帮忙.我尝试了以下命令,它不起作用:(

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

推荐答案

尝试以下命令:

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


sort手册中的


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.


在您的命令中:
-k 3.42,44 表示start from (42th char of 3rd field) to (44th field).
您是说 -k 3.42,3.44 吗?


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?

这篇关于Unix-在shell脚本中排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 11:09