On Linux the following would work:
xsel | sed ':rep /-$/ {N; s/-\n//; b rep}' | xsel -i On OSX I am largely guessing (although the sed script should work ok). pbcopy seems to the the command to use. Assuming there is a selection buffer on OSX and pbcopy works from it by default, this might work:
pbpaste | sed ':rep /-$/ {N; s/-\n//; b rep}' | pbcopy Looking at the online man page, it seems there are a number of 'pasteboards' that it can work with:
-pboard {general | ruler | find | font} specifies which pasteboard to copy to or paste from. If no pasteboard is given, the general pasteboard will be used by default. I have no idea which one (if any) is the one you want and I can't see any further documentation (although I haven't looked too deep). You could experiment with pbpaste -pboard xxx and see what comes out. There is a good chance one is for highlighted text while the other is for text copied with Cmd-C. Given a working option, you would just add it to both pbpaste and pbcopy.
The other thing that may go wrong is that pbcopy has issues reading and writing to the same pasteboard in at the same time. The simplest solution would be to use a non standard Unix utility called sponge. It is part of moreutils. Again Linux-centrically:
pbpaste | sed ':rep /-$/ {N; s/-\n//; b rep}' | sponge /dev/stdout | pbcopy Otherwise use a variable:
selection=$(pbpaste | sed ':rep /-$/ {N; s/-\n//; b rep}') echo "$selection" | pbcopy ###Update
Assuming Automator uses the stdout of the shell script, you would just select the to stdin option (this is where the data arrives if it is piped to the script). The the shell script would simply be:
#!/bin/sh sed ':rep /-$/ {N; s/-\n//; b rep}'