Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash- How to convert non-alphanumerical character to "_"

Tags:

string

regex

bash

I am trying to store user input in a variable and clean that variable in order to keep only alphanumerical caract + some others (I mean [a-zA-Z0-9-_]).

I tried using this but it isn't exhaustive :

SERVICE_NAME=$(echo $SERVICE_NAME | tr A-Z a-z | tr ' ' _ | tr \' _ | tr \" _)

Do you have some help for this?

like image 753
kheraud Avatar asked Jun 15 '11 16:06

kheraud


2 Answers

Bash's string substitution is a fine thing: ${var//pat/rep}

val='Foo$%!*@BAR###baZ'
echo ${val//[^a-zA-Z_-]/_}
Foo_____BAR___baZ

A small explanation: The slash introduces a search/replace, a little like in sed (where it just delimits patterns). But you use a single slash for one replacement:

val='Foo$%!*@BAR###baZ'
echo ${val/[^a-zA-Z_-]/_}
Foo_%!*@BAR###baZ

Two slashes // mean replace all. Uncommon, but it has some logic, multiple slashes to mean multiple replace (please excuse my poor English).

And note how the $ is separated from the variable, but it is hard to modify a literal constant this way (which would be nice for testing). Modifying $1 isn't a no-brainer as well, afaik.

like image 56
user unknown Avatar answered Nov 16 '22 01:11

user unknown


$ echo 'asd!@QCW@@D' | tr A-Z a-z | sed -e 's/[^a-zA-Z0-9\-]/_/g'
asd__qcw__d

I would use sed for this and use the ^ (not) operator in your set of valid characters and replace everything else with an underscore. The above shows the syntax with the output.

And, as a bonus, if you want to replace a run of invalid characters with one underscore, just add + to your regular expression (and use the -r switch to sed to make it use extended regular expressions:

$ echo 'asd!@QCW@@D' | tr A-Z a-z | sed -r 's/[^a-zA-Z0-9\-]+/_/g'
asd_qcw_d
like image 27
Steve Prentice Avatar answered Nov 16 '22 03:11

Steve Prentice