Linux 使用 awk 或 sed 删除不需要的字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8008546/
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 unwanted character using awk or sed
提问by AabinGunz
I have a command output from which I want to remove the double quotes "
.
我有一个命令输出,我想从中删除双引号"
。
Regex:
正则表达式:
strings -a libAddressDoctor5.so |\
grep EngineVersion |
awk '{if(NR==2)print}' |
awk '{print}'
Output:
输出:
EngineVersion="5.2.5.624"
I'd like to know how to remove unwanted characters with awk
or sed
.
我想知道如何使用awk
或删除不需要的字符sed
。
采纳答案by Piotr Praszmo
Use sed's substitution: sed 's/"//g'
使用 sed 的替换: sed 's/"//g'
s/X/Y/
replaces X with Y.
s/X/Y/
用 Y 替换 X。
g
means all occurrences should be replaced, not just the first one.
g
意味着应该替换所有出现的事件,而不仅仅是第一个。
回答by gregswift
Using just awk you could do (I also shortened some of your piping):
仅使用 awk 您就可以做到(我还缩短了您的一些管道):
strings -a libAddressDoctor5.so | awk '/EngineVersion/ { if(NR==2) { gsub("\"",""); print } }'
I can't verify it for you because I don't know your exact input, but the following works:
我无法为您验证,因为我不知道您的确切输入,但以下方法有效:
echo "Blah EngineVersion=\"123\"" | awk '/EngineVersion/ { gsub("\"",""); print }'
See also this questionon removing single quotes.
又见这个问题上删除单引号。
回答by Matthias Braun
tr
can be more concise for removing characters than sed
or awk
, especially when you want to remove different characters from a string.
tr
可以比sed
or更简洁地删除字符awk
,尤其是当您想从字符串中删除不同的字符时。
Removing double quotes:
去除双引号:
echo '"Hi"' | tr -d \"
# Produces Hi without quotes
Removing different kinds of brackets:
删除不同类型的括号:
echo '[{Hi}]' | tr -d {}[]
# Produces Hi without brackets
-d
stands for "delete".
-d
代表“删除”。