-1

怎么能“挤重复”的话?类似于“挤压重复字符”tr -s ''

我想改变例如:

hello.hello.hello.hello

hello
4

1 回答 1

1

这可以是一种方式:

$ cat a
hello hello bye but bye yeah
hello yeah
$ awk 'BEGIN{OFS=FS=" "} 
  {  for (i=1; i<=NF; i++) {
       if (!($i in a)) {printf "%s%s",$i,OFS; a[$i]=$i}
     }; 
    delete a;
    print ""
  }' a
hello bye but yeah 
hello yeah 

您可以更改字段分隔符:

$ cat a
hello|hello|bye|but|bye|yeah
hello|yeah
$ awk 'BEGIN{OFS=FS="|"} {for (i=1; i<=NF; i++) {if (!($i in a)) {printf "%s%s",$i,OFS; a[$i]=$i}}; delete a; print ""}' a
hello|bye|but|yeah|
hello|yeah|
于 2013-10-01T14:10:02.800 回答