Sorting and removing duplicate words in a line

前端 未结 7 1084
陌清茗
陌清茗 2020-12-03 06:50

The sort command lets me put lines in alphabetical order and remove duplicate lines. I need something similar that can sort the words on a single line, put them

7条回答
  •  难免孤独
    2020-12-03 07:24

    Using awk:

    awk '{for(i=1;i<=NF;i++) a[$i]++} END{for(i in a) printf i" ";print ""}' INPUT_FILE
    

    Test:

    [jaypal:~/Temp] cat file
    zebra ant spider spider ant zebra ant
    [jaypal:~/Temp] awk '{for (i=1;i<=NF;i++) a[$i]++} END{for (i in a) printf i" ";print ""}' file
    zebra spider ant 
    

提交回复
热议问题