I am writing a bash script that needs to parse filenames.
我正在编写一个需要解析文件名的bash脚本。
It will need to remove all special characters (including space): "!?.-_ and change all uppercase letters to lowercase. Something like this:
它需要删除所有特殊字符(包括空格):“!”-_并将所有大写字母改为小写字母。是这样的:
Some_randoM data1-A
More Data0
to:
:
somerandomdata1a
moredata0
I have seen lots of questions to do this in many different programming languages, but not in bash. Is there a good way to do this?
我在许多不同的编程语言中看到过很多这样的问题,但在bash中却没有。有什么好办法吗?
4 个解决方案
#1
27
cat yourfile.txt | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]'
The first tr
deletes special characters. d
means delete, c
means complement (invert the character set). So, -dc
means delete all characters except those specified. The \n
and \r
are included to preserve linux or windows style newlines, which I assume you want.
第一个tr删除特殊字符。d表示删除,c表示补码(反转字符集)。所以-dc表示删除除指定字符外的所有字符。\n和\r是用来保存linux或windows风格的换行符,我想您会喜欢的。
The second one translates uppercase characters to lowercase.
第二个将大写字母转换为小写字母。
#2
10
Pure bash solution:
纯bash的解决方案:
$ filename='Some_randoM data1-A'
$ f=${filename//[^[:alnum:]]/}
$ echo "$f"
SomerandoMdata1A
$ echo "${f,,}"
somerandomdata1a
A function for this:
一个函数:
clean() {
local a=${1//[^[:alnum:]]/}
echo "${a,,}"
}
Try it:
试一试:
$ clean "More Data0"
moredata0
#3
3
if you are using mkelement0 and Dan Bliss approach. You can also look into sed + POSIX regular expression.
如果你使用mkelement0和Dan Bliss方法。您还可以查看sed + POSIX正则表达式。
cat yourfile.txt | sed 's/[^a-zA-Z0-9]//g'
Sed matches all other characters that are not contained within the brackets except letters and numbers and remove them.
Sed匹配除字母和数字之外不在括号内的所有其他字符,并删除它们。
#4
2
I've used tr
to remove any characters that are not part of [:print:]
class
我使用tr来删除不属于[:print:]类的任何字符
cat file.txt | tr -dc '[:print:]'
or
或
echo "..." | tr -dc '[:print:]'
Additionally you might want to |
(pipe) the output to od -c
to confirm the result
此外,您可能希望|(管道)输出到od -c以确认结果
cat file.txt | tr -dc '[:print:]' | od -c
#1
27
cat yourfile.txt | tr -dc '[:alnum:]\n\r' | tr '[:upper:]' '[:lower:]'
The first tr
deletes special characters. d
means delete, c
means complement (invert the character set). So, -dc
means delete all characters except those specified. The \n
and \r
are included to preserve linux or windows style newlines, which I assume you want.
第一个tr删除特殊字符。d表示删除,c表示补码(反转字符集)。所以-dc表示删除除指定字符外的所有字符。\n和\r是用来保存linux或windows风格的换行符,我想您会喜欢的。
The second one translates uppercase characters to lowercase.
第二个将大写字母转换为小写字母。
#2
10
Pure bash solution:
纯bash的解决方案:
$ filename='Some_randoM data1-A'
$ f=${filename//[^[:alnum:]]/}
$ echo "$f"
SomerandoMdata1A
$ echo "${f,,}"
somerandomdata1a
A function for this:
一个函数:
clean() {
local a=${1//[^[:alnum:]]/}
echo "${a,,}"
}
Try it:
试一试:
$ clean "More Data0"
moredata0
#3
3
if you are using mkelement0 and Dan Bliss approach. You can also look into sed + POSIX regular expression.
如果你使用mkelement0和Dan Bliss方法。您还可以查看sed + POSIX正则表达式。
cat yourfile.txt | sed 's/[^a-zA-Z0-9]//g'
Sed matches all other characters that are not contained within the brackets except letters and numbers and remove them.
Sed匹配除字母和数字之外不在括号内的所有其他字符,并删除它们。
#4
2
I've used tr
to remove any characters that are not part of [:print:]
class
我使用tr来删除不属于[:print:]类的任何字符
cat file.txt | tr -dc '[:print:]'
or
或
echo "..." | tr -dc '[:print:]'
Additionally you might want to |
(pipe) the output to od -c
to confirm the result
此外,您可能希望|(管道)输出到od -c以确认结果
cat file.txt | tr -dc '[:print:]' | od -c