質問

How can "squeeze-repeated" words? similar to "squeeze repeated characters" with tr -s ''

I would like to change for example:

hello.hello.hello.hello

to

hello
役に立ちましたか?

解決

This can be a way:

$ 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 

You can change the field separator:

$ 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|
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top