bash 排序和删除一行中的重复单词

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8802734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 21:28:07  来源:igfitidea点击:

Sorting and removing duplicate words in a line

bashsorting

提问by Village

The sortcommand 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 in order, and remove any duplicates. Is there a command for this?

sort命令让我按字母顺序排列行并删除重复的行。我需要类似的东西,可以在一行中对单词进行排序,按顺序排列,并删除任何重复项。有这个命令吗?

E.g.:

例如:

zebra ant spider spider ant zebra ant

Changes to:

更改为:

ant spider zebra

There is no space before the first word or after the last word.

第一个单词之前或最后一个单词之后没有空格。

回答by jcollado

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 -uand transform back to a single row with xargs.

您可以使用 将单行中的单词列表转换为单列xargs -n1,使用sort -u并将其转换回单行xargs

回答by Village

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.

外壳已经构建为解析[:blank:]单独的单词列表。因此 xargs 的使用是完全多余的。“独特”的东西可以完成,但它更容易使用排序。

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

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

回答by dogbane

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

使用tr到的变化空间,以新的线,然后sort,最后更换新线回位。

echo $(tr ' ' '\n' <<< "zebra ant spider spider ant zebra ant" | sort -u)

回答by Carlo Wood

All of the answers prior to this one can only sort a single line at time. The following can be used to pipe a whole list of such lines into and it will print the sorted list of unique words for each line.

在此之前的所有答案一次只能对一行进行排序。以下内容可用于将这些行的整个列表通过管道传输到其中,它将打印每行的唯一单词的排序列表。

awk '{ delete a; for (i=1; i<=NF; i++) a[$i]++; n=asorti(a, b); for (i=1; i<=n; i++) printf b[i]" "; print "" }'

Thanks @jaypai for a lot of the syntax used in this.

感谢 @jaypai 在这里使用了很多语法。

Example:

例子:

>cat file
group label wearable edit_group edit_group_order label_max camera_elevation camera_distance name label_min label_max value_min value_max camera_angle camera_elevation id
id group label wearable edit_group clothing_morph value_min value_max name value_default clothing_morph group
id label show_simple wearable name edit_group edit_group_order group clothing_morph clothing_morph camera_distance label_min label_max value_min value_max camera_distance camera_angle
id group label wearable name edit_group clothing_morph value_min value_max value_default
group label wearable id clothing_morph edit_group edit_group_order label_min label_max value_min value_max name camera_distance camera_angle camera_elevation
id group label wearable edit_group name label_min label_max value_min value_max wearable
name id group wearable edit_group id group wearable id group wearable id group wearable value_min value_max

>cat file | awk '{ delete a; for (i=1; i<=NF; i++) a[$i]++; n=asorti(a, b); for (i=1; i<=n; i++) printf b[i]" "; print "" }'
camera_angle camera_distance camera_elevation edit_group edit_group_order group id label label_max label_min name value_max value_min wearable 
clothing_morph edit_group group id label name value_default value_max value_min wearable 
camera_angle camera_distance clothing_morph edit_group edit_group_order group id label label_max label_min name show_simple value_max value_min wearable 
clothing_morph edit_group group id label name value_default value_max value_min wearable 
camera_angle camera_distance camera_elevation clothing_morph edit_group edit_group_order group id label label_max label_min name value_max value_min wearable 
edit_group group id label label_max label_min name value_max value_min wearable 
edit_group group id name value_max value_min wearable

回答by kev

Use python

python

$ echo "zebra ant spider spider ant zebra ant" | python -c 'import sys; print(" ".join(sorted(set(sys.stdin.read().split()))))'
ant spider zebra

回答by Birei

Using perl:

使用perl

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

Result:

结果:

ant spider zebra

回答by jaypal singh

Using awk:

使用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