6

Very simple question but can't seem to find a simple answer...

I am writing a bash script which needs to remove all non-alphabetic and non-numeric characters. Eg. I want...

INPUT_STRING="ABC# .1-2-3"

OUTPUT_STRING= # some form of processing on $INPUT_STRING #

echo $OUTPUT_STRING
ABC123

I realize that this would be best solved using regex, but not sure how to use this effectively in the script.

All help greatly appreciated...

4

2 に答える 2

11

、またはsedではないすべての文字を削除するために使用できます。a-zA-Z0-9

$ echo "ABC# .1-2-3" | sed 's/[^a-zA-Z0-9]//g'
ABC123

だからあなたの場合、

$ INPUT_STRING="ABC# .1-2-3"
$ OUTPUT_STRING=$(echo $INPUT_STRING | sed 's/[^a-zA-Z0-9]//g')
$ echo $OUTPUT_STRING
ABC123
于 2013-06-18T11:12:53.463 に答える
10
$ INPUT_STRING="ABC# .1-2-3"
$ printf '%s\n' "${INPUT_STRING//[![:alnum:]]}"
ABC123
于 2013-06-18T11:20:29.887 に答える