【问题标题】:Sort a merged file by second field in a reverse order按相反顺序按第二个字段对合并文件进行排序
【发布时间】:2020-06-19 10:17:49
【问题描述】:

我已将两个文件合并为一个文件,我正在尝试对合并后的文件进行排序,但我似乎遗漏了一些内容,因为我已合并的文件之一是空的。 下面我展示一下我所做的输入和输出的描述:

  File1 has this data:
    John#Smith#3400#2000.00
    Blue#Light##2300#9000.00
    John#Smith#3400#2000.00
    Hidey#George#1000#2000.00
    Blue#Light##2300#9000.00
    Trasy#Brown#2000#4000.00
    Hidey#George#1000#2000.00

 1_ I looked for unique values by using this command and stored the output in a new file:
   $ uniq -u File1 > File2 
   and the output of File2 is empty because there were not unique values in File1

 2_ I looked for duplicate values by using this command and stored the output in a new file:
   $ uniq -d File1 > File3
    Here I got the unique values and I stored them in File3

 3_ I combined the two files (File2 and File3)
    Here I got the unique values and I stored them in File3
 4_ When I came to merge File2 with File3
    I used this command: cat File2 File3 > File4
   I got a result as showing below:
  -----------------------------------
  |$      John#Smith#3400#2000.00   |
  |$      Hidey#George#1000#2000.00 |
  |$      Blue#Light##2300#9000.00  |
  |$      Trasy#Brown#2000#4000.00  |
  |$      Hidey#George#1000#2000.00 |
  -----------------------------------

这意味着两个文件被合并了,左边的空间是因为空文件而发生的。

在按第二个字段和相反的顺序对 File4 进行排序时,我需要帮助。 我试过这个命令: $sort -k2,2 -r File4 我没有看到不同的数据顺序显示,它只是颠倒了它。 如何对两个合并的文件进行排序,其中一个为空并导致新文件中有空间。

【问题讨论】:

  • 这对您的系统有效吗? sort -t# -rk2 File4
  • 是的,它工作得很好,谢谢。 -t# 在代码中做了什么? @Milag

标签: linux unix


【解决方案1】:
sort -t# -rk2 File4

-t# 使用 # 作为字段分隔符而不是默认值

【讨论】:

  • 我明白了。非常感谢。
猜你喜欢
  • 2017-11-13
  • 1970-01-01
  • 1970-01-01
  • 2013-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
相关资源
最近更新 更多