문제

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