刪除 uniq -c 輸出的空格


uniq 是將重複資料過濾的工具, 加上 -c 參數會計算每個結果的數量, 但它會在輸出結果前加入空格, 例如:

如果要刪除統計結果前的空格, uniq 本身不能實現, 需要借助其他指令, 將輸出結果用管線 pipe 放到 sed 或 grep 來去除前面的空格, 例如:

sed:

/linux/remove-uniq-space

  • $ sort | uniq -c | sed ‘s/^ *//’

grep:

/linux/remove-uniq-space

  • $ sort | uniq -c | grep -Po ‘\d.*’

Leave a Reply