如何使用AWK或其他方法在双引号中提取子字符串?
我想在此示例字符串中提取 you :
See [ "you" later
但是,我的尝试无法正常工作:
awk '{ sub(/.*\"/, ""); sub(/\".*/, ""); print }' <<< "See [ \"you\" later"
结果:
later
使用尴尬或其他方法,如何在双引号中提取子字符串?
I want to extract you in this sample string:
See [ "you" later
However, my attempt does not work as expected:
awk '{ sub(/.*\"/, ""); sub(/\".*/, ""); print }' <<< "See [ \"you\" later"
result:
later
Using awk or other methods, how can I extract the substring in the double quotes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
1st解决方案: 您可以在此处使用
gsub
awk 的功能。只需简单地用零做2个替换即可。第1至第1出现“
,然后替换从Next”替换所有内容。
第二解决方案: 使用gnu
grep
解决方案。使用其-op
选项分别打印匹配的零件并启用PCRE REGEX选项。从启动匹配到第一次出现“
”,并使用\ k
选项忘记匹配的部分,然后在下一次出现“代码>根据要求,将在2
之间打印文本。
1st solution: You can make use of
gsub
function ofawk
here. Just simply do 2 substitutions with NULL. 1st till 1st occurrence of"
and then substitute everything from next"
occurrence to everything with NULL and print that line.2nd solution: Using GNU
grep
solution. Using its-oP
option to print matched part and enable PCRE regex option respectively. With regex from starting match till very first occurrence of"
and using\K
option to forget matched part and then again match everything just before next occurrence of"
which will print text between 2"
as per requirement.使用
bash
给出输出
说明:使用
ifs
告知bash
以“ ”,将splitted文本读为array <代码> arr 打印第二个元素(
[1]
为[0]
表示第一元素)。Using
bash
gives output
Explanation: use
IFS
to informbash
to split at"
, read splitted text into arrayarr
print 2nd element (which is[1]
as[0]
denotes 1st element).您也可以在此处使用
剪切
:它用双引号将字符串拆分并获取第二个项目。
输出:
请参阅在线演示。
You can also use
cut
here:It splits the string with a double quote and gets the second item.
Output:
See the online demo.
仅使用GNU AWK的几种方法:
Multi-Char
RS
和rt
:第三arg to
match()
:gensub(gensub)( )
(假设始终存在引用的字符串):fpat
:patsplit():
第四arg to
split> split()
:Just a few ways using GNU awk for:
multi-char
RS
andRT
:the 3rd arg to
match()
:gensub()
(assuming the quoted string is always present):FPAT
:patsplit():
the 4th arg to
split()
:这是一个没有任何正则的尴尬解决方案:
或带有正则解决方案:
另一种
awk
,带有match> match
:Here is an awk solution without any regex:
Or a
sed
solution with regex:Another
awk
withmatch
:使用
sed
Using
sed
提取所有引用的子字样,然后删除引号:
给予:
“”
在第二行输出上的空字符串匹配(使用GREP- o'“ [^”] \+“'
跳过空字符串)“ c
尚未完全引用,因此不匹配与小字符串不匹配,您可能需要使用纯shell。这提取 first 引用
中的substring $ str
:给予
Extract all quoted substrings, and remove the quotes:
Gives:
""
is matched as an empty string on the second line of output (usegrep -o '"[^"]\+"'
to skip empty strings)"c
is not fully quoted, so it doesn't matchFor a small string, you may want to use pure shell. This extracts the first quoted substring in
$str
:Gives
awk
在BSD和Linux中起作用的示例:对于
sed
,用于BSD和Linux:对于
php
,制作一个称为您的文件。 PHP:然后运行它:
对于Perl,请在引号中找到内容:
删除
(。)
和$ 2
如果您不想捕获Newline。An
awk
example that works in BSD and Linux:For
sed
, use for both BSD and Linux:For
php
, make a file called you.php:Then run it:
For perl, find the content inside quotes:
Remove the
(.)
and$2
if you don't wish to capture the newline.使用
awk
的免提驾驶:hands-free driving with
awk
: