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