Sort

如何對具有多個分隔符的文件進行排序?

  • January 20, 2019

列出 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 

在此處輸入圖像描述

似乎該文件可能使用多個分隔符。

  • 確定正在使用的分隔符的最有效方法是什麼?
  • 根據使用的分隔符對列進行排序的最佳和最簡單的方法是什麼?

這應該可以,除非您想保留標題:

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

如果文件經常使用空格(即它不混合and ),您可以省略(“ignoreleading blanks)-b選項。sort``| applied``| applied

但我想a)該程序可能有一些選項可以將其格式調整為更機器可讀的東西b)你會更好,例如。awk -F'|' '$6~/not needed/'而不是sort.

你可以使用這個:

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

它繼續awk執行listing.txt;相反,您可以將數據輸入awklisting.txt當然,最後省略)。-script 按原樣列印前awk四個標題行。其餘的行通過管道傳輸到sort. Sort對第 6 列進行排序,使用等於管道符號的分隔符對其進行標識。

引用自:https://unix.stackexchange.com/questions/495623