Jump to content

Remove certain chars from copied text

Recommended Posts

I would like a workflow that would remove all spaces, dots and hyphens from a string, leaving just the normal characters (letters and numbers).


The string input would be whatever is already in the clipboard, and the output (resulting string) would also go to the clipboard (like the Case Converter workflow does).


Has anyone created such workflow? Or how could I create it? Thanks :)

Edited by juanpazos
Link to comment

for BASH using sed:

pbpaste | sed 's/[./ /-]//g' | pbcopy

This will grab the clipboard, filter out the " ", "." & "-" and replace the clipboard with the results.


You can set this up however you want with either a keyword or shortcut to script {bin/bash}.

Link to comment

An alternative to RodgerWW's answer is:


export LC_CTYPE=UTF-8
pbpaste | sed 's/[^[:alpha:][:digit:]]//g' | pbcopy
The LC_CTYPE=UTF-8 is very important for pbcopy and pbpaste to work correctly in a workflow (it's normally not necessary in a shell). If you don't set LC_CTYPE, pbcopy etc. will assume input/output is ASCII and garble the text if it isn't ASCII.

The big difference to RodgerWW's code is that it uses a whitelist, not a blacklist, removing anything that isn't in the list of characters instead of only removing what is.

In some cases, this behaviour is preferable, in others the blacklist approach is better.

Edited by deanishe
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Create New...