Sorting and removing duplicate words in a line

前端 未结 7 1060
陌清茗
陌清茗 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:09

    Using perl:

    perl -lane '
      %a = map { $_ => 1 } @F;
      print join qq[ ], sort keys %a;
    ' <<< "zebra ant spider spider ant zebra ant"
    

    Result:

    ant spider zebra
    
    0 讨论(0)
  • 2020-12-03 07:14

    Use tr to change spaces to new lines, then sort, and finally change new lines back to spaces.

    echo $(tr ' ' '\n' <<< "zebra ant spider spider ant zebra ant" | sort -u)
    
    0 讨论(0)
  • 2020-12-03 07:17

    The shell was built to parse [:blank:] seperated word lists already. Therefore the use of xargs is completely redundant. The "unique" stuff can be done but its just easier to use sort.

    echo $(printf '%s\n' zebra ant spider spider ant zebra ant | sort -u)

    0 讨论(0)
  • 2020-12-03 07:21

    Use python

    $ echo "zebra ant spider spider ant zebra ant" | python -c 'import sys; print(" ".join(sorted(set(sys.stdin.read().split()))))'
    ant spider zebra
    
    0 讨论(0)
  • 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 
    
    0 讨论(0)
  • 2020-12-03 07:28

    This works for me:

    $ echo "zebra ant spider spider ant zebra ant" | xargs -n1 | sort -u | xargs
    ant spider zebra
    

    You can transform a list of words in a single row to a single column with xargs -n1 , use sort -u and transform back to a single row with xargs.

    0 讨论(0)
提交回复
热议问题