如何对具有多个分隔符的文件进行排序?

如何对具有多个分隔符的文件进行排序?

列出 openSUSE 中的可用补丁会产生以下输出。

产生以下详细信息的命令是zypper patches。运行命令时也显示相同的结果zypper list-patches -a

Loading repository data...
Reading installed packages...
Repository                        | Name               | Category    | Severity  | Interactive | Status     | Summary                                                                                           
----------------------------------+--------------------+-------------+-----------+-------------+------------+---------------------------------------------------------------------------------------------------
openSUSE-Leap-15.0-Update         | openSUSE-2018-1000 | recommended | moderate  | ---         | not needed | Recommended update for gnuhealth, proteus, tryton, trytond, trytond_purchase, trytond_stock_supply
openSUSE-Leap-15.0-Update         | openSUSE-2018-1004 | security    | low       | ---         | not needed | Security update for ffmpeg-4                                                                      
openSUSE-Leap-15.0-Update         | openSUSE-2018-1005 | security    | moderate  | ---         | not needed | Security update for chromium                                                                      
openSUSE-Leap-15.0-Update         | openSUSE-2018-1006 | security    | moderate  | ---         | applied    | Security update for okular                                                                        
openSUSE-Leap-15.0-Update         | openSUSE-2018-1007 | recommended | moderate  | ---         | applied    | Recommended update for tigervnc     

例如,尝试使用命令对“状态”进行排序sort -k 6,6 listing.txt | less会产生以下输出。

----------------------------------+--------------------+-------------+-----------+-------------+------------+---------------------------------------------------------------------------------------------------
Loading repository data...
Reading installed packages...
openSUSE-Leap-15.0-Update-Non-Oss | openSUSE-2018-1082 | recommended | moderate  | ---         | not needed | Security update for opera                                                                         
openSUSE-Leap-15.0-Update-Non-Oss | openSUSE-2018-1240 | recommended | moderate  | ---         | not needed | Recommended update for opera                                                                      
openSUSE-Leap-15.0-Update-Non-Oss | openSUSE-2018-1294 | recommended | low       | ---         | not needed | Recommended update for Regina-REXX, THE, ooRexx                                                   
openSUSE-Leap-15.0-Update         | openSUSE-2018-1000 | recommended | moderate  | ---         | not needed | Recommended update for gnuhealth, proteus, tryton, trytond, trytond_purchase, trytond_stock_supply
openSUSE-Leap-15.0-Update         | openSUSE-2018-1007 | recommended | moderate  | ---         | applied    | Recommended update for tigervnc    
Repository                        | Name               | Category    | Severity  | Interactive | Status     | Summary                   

在使用 vim 中的语法高亮规则检查分隔符是否是空格和制表符的组合时,它仅返回空格。

:syntax on
:set syntax=whitespace 

在此输入图像描述

该文件似乎可能使用多个分隔符。

  • 确定使用的分隔符的最有效方法是什么?
  • 根据使用的分隔符对列进行排序的最佳和最简单的方法是什么?

答案1

这应该可以,除非您想将标题保留在适当的位置:

tail -n +5 listing.txt | sort -b -k6 -t'|'

如果文件经常使用空格(即它不混合和),您可以省略(“忽略前导空格”)-b选项。sort| applied| applied

但我猜想,a)该程序可能有一些选项可以将其格式调整为机器可读的格式b)您会更好地使用例如。awk -F'|' '$6~/not needed/'代替sort

答案2

你可以使用这个:

awk 'NR<=4 {print $0; next } { print $0 | "sort -k6,6 -t\\|" }' listing.txt

它运行awklisting.txt;相反,您可以将数据通过管道输入awklisting.txt当然,最后省略)。 -scriptawk按原样打印前四个标题行。其余行通过管道输送到sort.Sort对第 6 列进行排序,并使用等于管道符号的分隔符来标识它。

相关内容