Given a file like this:x y y z xx x y z z yx x x z yy z z y x x xx x x x xI would like the output to be:x y+ z xx+ y z+ yx+ z yy z+ y x+x+Is that possible to do with awk or perl in a oneliner? i.e. is it possible to find any number of similar values in rows and merge them?
merge duplicate rows in columns
bash;text processing;awk;perl
This perl version can also deal with arbitrary field lengths, not only those of a single character:$ perl -lpae 'for $i (@F){s/($i\s*){2,}/$i+ /g}' file x y+ z xx+ y z+ yx+ z yy z+ y x+ x+ On a more complex file:$ cat filefoo foo foo bar foobar foo bar bar foofoo foo x x x bar$ perl -lpae 'for $i (@F){s/($i\s*){2,}/$i+ /g}' file foo+ bar foobar foo bar+ foofoo+ x+ barExplanationThe -l trims newlines from each input line, the -a splits input fields on whitespace into the array @F and the -p prints each input line after applying the script given by -e.The script itself iterates over each input field (the @F array), saving each as $i. The substitution looks for 2 or more consecutive $i followed by 0  or more spaces and replaces them with $i+.