Linux 双引号内的 Grep 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15435056/
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
Grep string inside double quotes
提问by Grimlockz
Trying to grep a string inside double quotes at the moment I use this
在我使用它的那一刻,试图在双引号内 grep 一个字符串
grep user file | grep -e "[\'\"]"
This will get to the section of the file I need and highlight the double quotes but it will not give the sting in the double quotes
这将到达我需要的文件部分并突出显示双引号,但它不会在双引号中引起刺痛
采纳答案by Gilles Quenot
Try doing this :
尝试这样做:
$ cat aaaa
foo"bar"base
$ grep -oP '"\K[^"7]+(?=["7])' aaaa
bar
I use look aroundadvanced regextechniques.
我使用环顾高级正则表达式技术。
If you want the quotes too :
如果你也想要引号:
$ grep -Eo '["7].*["7]'
"bar"
Note :
笔记 :
7
is the octal ascii representation of the single quote
是单引号的八进制ascii表示
回答by Paul Calabro
Try:
尝试:
grep "Test" /tmp/junk | tr '"' ' '
This will remove quotes from the output of grep
这将从 grep 的输出中删除引号
Or you could try the following:
或者您可以尝试以下操作:
grep "Test" /tmp/junk | cut -d '"' -f 2
This will use the quotes as a delimiter. Just specify the field you want to select. This lets you cherry-pick the information you want.
这将使用引号作为分隔符。只需指定要选择的字段。这让您可以挑选所需的信息。
回答by Fredrik Pihl
$ cat aaaa
foo"bar"base
$ grep -o '"[^"]\+"'
"bar"
回答by Alan Saldanha
This worked for me. This will print the data including the double quotes also.
这对我有用。这也将打印包括双引号在内的数据。
grep -o '"[^"]\+"' file.txt
If we want to print without double quotes, then we need to pipe the grep output to a sed command.
如果我们想在没有双引号的情况下打印,那么我们需要将 grep 输出通过管道传输到 sed 命令。
grep -o '"[^"]\+"' file.txt | sed 's/"//g'