bash 删除标点标准输入。
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23509186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove punctuation standard input.
提问by JAC
I want the program can remove punctuation which read from the standard input My code is:
我希望程序可以删除从标准输入中读取的标点符号我的代码是:
echo $* | tr -d '[:punct:]'
It can handle some simple situations but when I type input in terminal (like: whatever ad!":)
它可以处理一些简单的情况,但是当我在终端中输入时(例如:任何广告!”:)
when the sentence within continuing several punctuation, the terminal will reflect the result: -bash: !": event not found
当句子内连续几个标点时,终端会反映结果: -bash: !": event not found
Can anyone give help?
任何人都可以提供帮助吗?
回答by user2485710
single quotes should be used to avoid expansion, e.g.
应该使用单引号来避免扩展,例如
echo 'whatever ad!' | tr -d '[:punct:]'
under a bash shell it prints out
在 bash shell 下它会打印出来
whatever ad
and if you want to use a variable
如果你想使用一个变量
BUFF='whatever ?_-!!!!ad!'; echo "$BUFF" | tr -d '[:punct:]'
EDIT 1
编辑 1
this is a complete script following your request
这是按照您的要求编写的完整脚本
#!/bin/sh
functionStripAndPrint()
{
echo "$@" | tr -d '[:punct:]'
}
functionStripAndPrint "$@"
assuming that this script is stored in the stripchars.sh
file, you can invoke it like so
假设这个脚本存储在stripchars.sh
文件中,你可以像这样调用它
./stripchars.sh 'das !adsa _sda ssad-'
and it will print
它会打印
das adsa sda ssad
EDIT 2
编辑 2
you can work around the interpretation of some of the special characters with set
, for example
您可以解决一些特殊字符的解释set
,例如
set +H
deactivates the H
optionwhich is linked to the !
symbol, so now !
is just an exclamation mark with no special meaning. You can then simplify your invocation a little bit
停用链接到符号的H
选项!
,所以现在!
只是一个没有特殊含义的感叹号。然后您可以稍微简化您的调用
./stripchars.sh sdfsa!fdsaf?\'
./stripchars.sh sdfsa!fdsaf?\'
as you can see the only problem at this point is the '
that still needs to be escaped.
正如您所看到的,此时唯一的问题是'
仍然需要转义的 。
If you want to re-enable the H
you do
如果你想重新启用H
你做的
set -H
set
is handy to modify the behaviour of your shell, I don't know if it's worth in your case, the shell is good and handy for some basic stuff, but I don't know if this will fit your needs, you know better, take a look at set
and see if it's enough.
set
修改你的外壳的行为很方便,我不知道它是否适合你的情况,外壳对于一些基本的东西来说很好而且很方便,但我不知道这是否适合你的需求,你知道得更好,看一看,set
看看是否足够。
回答by R Sahu
As you probably know, bash
uses !
to get commands from the history of commands. When you type
您可能知道,bash
用于!
从命令历史记录中获取命令。当你输入
echo Whatever ad!":
it tries to retrieve the command from its command history by using !":
. Since it does not find any command using that, it prints the message
它尝试使用!":
. 由于它没有找到任何使用该命令的命令,因此它会打印消息
bash: !": event not found
You can pass those special characters to bash
by (1) using single quote to let special characters be treated like normal characters, or (2) escaping the special characters.
您可以将这些特殊字符传递给bash
(1) 使用单引号让特殊字符被视为普通字符,或者 (2) 转义特殊字符。
echo 'Whatever ad!":' | tr -d '[:punct:]'
echo Whatever ad\!\": | tr -d '[:punct:]'