shell - escape string and make it unix ready -
I would like to take a string that can contain many places and do the following:
1) One Change whitespace with underscore
2) Remove characters that are not AZ or 0-9
3) Make the result all lowercase
Then a variable Any ideas able to use the result in the form?
I think you can do whatever you like.
variable = $ (echo "$ {input}" | tr az az | tr [: empty:] _ | tr -cd [: alnum:] _)
Explanation:
tr az az
- translate the uppercase letters to lower.
tr [: blank:] _
- Translates the blank to underscore.
tr -cd [: alnum:] _
- Delete all characters that are not alphanumeric, or underscores.
Note: If you want to remove the underscores before converting the empty space into underscore, then the tr-cd _
can be added near the beginning of the pipe chain.
Comments
Post a Comment