如何使用AWK或其他方法在双引号中提取子字符串?

发布于 2025-02-12 23:19:18 字数 286 浏览 1 评论 0原文

我想在此示例字符串中提取 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

战皆罪 2025-02-19 23:19:18

1st解决方案: 您可以在此处使用gsub awk 的功能。只需简单地用零做2个替换即可。第1至第1出现,然后替换从Next ”替换所有内容。

awk '{gsub(/^[^"]*"|".*/,"")} 1' Input_file


第二解决方案: 使用gnu grep解决方案。使用其-op选项分别打印匹配的零件并启用PCRE REGEX选项。从启动匹配到第一次出现”,并使用\ k选项忘记匹配的部分,然后在下一次出现“代码>根据要求,将在2 之间打印文本。

grep -oP '^.*?"\K[^"]*' Input_file

1st solution: You can make use of gsub function of awk 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.

awk '{gsub(/^[^"]*"|".*/,"")} 1' Input_file


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.

grep -oP '^.*?"\K[^"]*' Input_file
夜唯美灬不弃 2025-02-19 23:19:18

使用bash

IFS='"'
read -ra arr <<< "See [ \"you\" later"
echo ${arr[1]}

给出输出

you

说明:使用ifs告知bash“ ”,将splitted文本读为array <代码> arr 打印第二个元素([1][0]表示第一元素)。

Using bash

IFS='"'
read -ra arr <<< "See [ \"you\" later"
echo ${arr[1]}

gives output

you

Explanation: use IFS to inform bash to split at ", read splitted text into array arr print 2nd element (which is [1] as [0] denotes 1st element).

初吻给了烟 2025-02-19 23:19:18

您也可以在此处使用剪切

cut -d\" -f 2 <<< 'See [ "you" later '

它用双引号将字符串拆分并获取第二个项目。

输出:

you

请参阅在线演示

You can also use cut here:

cut -d\" -f 2 <<< 'See [ "you" later '

It splits the string with a double quote and gets the second item.

Output:

you

See the online demo.

半步萧音过轻尘 2025-02-19 23:19:18

仅使用GNU AWK的几种方法:

Multi-Char RSrt

$ echo 'See [ "you" later' |
    awk -v RS='"[^"]*"' 'RT{ print substr(RT,2,length(RT)-2) }'
you

第三arg to match()

$ echo 'See [ "you" later' |
    awk 'match($0,/"([^"]*)"/,a){ print a[1] }'
you

gensub(gensub)( )(假设始终存在引用的字符串):

$ echo 'See [ "you" later' |
    awk '{print gensub(/.*"([^"]*)".*/,"\\1",1)}'
you

fpat

$ echo 'See [ "you" later' |
    awk -v FPAT='[^"]*' 'NF>2{print $2}'
you

$ echo 'See [ "you" later' |
    awk -v FPAT='"[^"]*"' 'NF{print substr($1,2,length($1)-2)}'
you

patsplit():

$ echo 'See [ "you" later' |
    awk 'patsplit($0,f,/"[^"]*"/,s){print substr(f[1],2,length(f[1])-2)}'
you

第四arg to split> split()

$ echo 'See [ "you" later' |
    awk 'split($0,f,/"[^"]*"/,s)>1{print substr(s[1],2,length(s[1])-2)}'
you

Just a few ways using GNU awk for:

multi-char RS and RT:

$ echo 'See [ "you" later' |
    awk -v RS='"[^"]*"' 'RT{ print substr(RT,2,length(RT)-2) }'
you

the 3rd arg to match():

$ echo 'See [ "you" later' |
    awk 'match($0,/"([^"]*)"/,a){ print a[1] }'
you

gensub() (assuming the quoted string is always present):

$ echo 'See [ "you" later' |
    awk '{print gensub(/.*"([^"]*)".*/,"\\1",1)}'
you

FPAT:

$ echo 'See [ "you" later' |
    awk -v FPAT='[^"]*' 'NF>2{print $2}'
you

$ echo 'See [ "you" later' |
    awk -v FPAT='"[^"]*"' 'NF{print substr($1,2,length($1)-2)}'
you

patsplit():

$ echo 'See [ "you" later' |
    awk 'patsplit($0,f,/"[^"]*"/,s){print substr(f[1],2,length(f[1])-2)}'
you

the 4th arg to split():

$ echo 'See [ "you" later' |
    awk 'split($0,f,/"[^"]*"/,s)>1{print substr(s[1],2,length(s[1])-2)}'
you
彩虹直至黑白 2025-02-19 23:19:18
$ grep -oP '(?<=").*(?=")' <<< "See [ \"you\" later"
you
$ grep -oP '(?<=").*(?=")' <<< "See [ \"you\" later"
you
谈下烟灰 2025-02-19 23:19:18

这是一个没有任何正则的尴尬解决方案:

s='See [ "you" later'
awk -F '"' 'NF>2 {print $2}' <<< "$s"

you

或带有正则解决方案:

sed -E 's/[^"]*"([^"]*)".*/\1/' <<< "$s"
you

另一种awk,带有match> match

awk 'match($0, /"[^"]*"/) {print substr($0, RSTART+1, RLENGTH-2)}' <<< "$s"

you

Here is an awk solution without any regex:

s='See [ "you" later'
awk -F '"' 'NF>2 {print $2}' <<< "$s"

you

Or a sed solution with regex:

sed -E 's/[^"]*"([^"]*)".*/\1/' <<< "$s"
you

Another awk with match:

awk 'match($0, /"[^"]*"/) {print substr($0, RSTART+1, RLENGTH-2)}' <<< "$s"

you
伪心 2025-02-19 23:19:18

使用sed

$ sed -n 's/[^"]*"\([[:alpha:]]\+\)"[^"]*/\1 /gp' input_file
you

Using sed

$ sed -n 's/[^"]*"\([[:alpha:]]\+\)"[^"]*/\1 /gp' input_file
you
找回味觉 2025-02-19 23:19:18

提取所有引用的子字样,然后删除引号:

echo 'See [ "you" later, "" "a" "b" "c' |
grep -o '"[^"]*"' | tr -d \"

给予:

you

a
b
  • “”在第二行输出上的空字符串匹配(使用GREP- o'“ [^”] \+“'跳过空字符串)

  • “ c尚未完全引用,因此不匹配

与小字符串不匹配,您可能需要使用纯shell。这提取 first 引用中的substring $ str

str='Example "a" and "b".'
str=${str#*\"} # Cut up to first quote
case $str in
    *\"*) str=${str%%\"*};; # Cut from second quote onwards
    *) str= # $str contains less than two quotes
esac
echo "$str"

给予

a

Extract all quoted substrings, and remove the quotes:

echo 'See [ "you" later, "" "a" "b" "c' |
grep -o '"[^"]*"' | tr -d \"

Gives:

you

a
b
  • "" is matched as an empty string on the second line of output (use grep -o '"[^"]\+"' to skip empty strings)

  • "c is not fully quoted, so it doesn't match

For a small string, you may want to use pure shell. This extracts the first quoted substring in $str:

str='Example "a" and "b".'
str=${str#*\"} # Cut up to first quote
case $str in
    *\"*) str=${str%%\"*};; # Cut from second quote onwards
    *) str= # $str contains less than two quotes
esac
echo "$str"

Gives

a
笨笨の傻瓜 2025-02-19 23:19:18

awk在BSD和Linux中起作用的示例:

% echo 'See [ "you" later ' | awk -F\" '{print $2}'
you

对于sed,用于BSD和Linux:

% echo 'See [ "you" later ' | sed -e 's/^[^"]*"//' -e 's/"[^["]*$//'
you

对于php,制作一个称为您的文件。 PHP:

<?php
$you = explode('"', 'See [ "you" later ');
echo $you[1], PHP_EOL;

然后运行它:

% php you.php
you

对于Perl,请在引号中找到内容:

% echo 'See [ "you" later| perl -pe 's/^[^"]*"(.*)"[^"]*(.)$/$1$2/'

删除(。)$ 2如果您不想捕获Newline。

An awk example that works in BSD and Linux:

% echo 'See [ "you" later ' | awk -F\" '{print $2}'
you

For sed, use for both BSD and Linux:

% echo 'See [ "you" later ' | sed -e 's/^[^"]*"//' -e 's/"[^["]*$//'
you

For php, make a file called you.php:

<?php
$you = explode('"', 'See [ "you" later ');
echo $you[1], PHP_EOL;

Then run it:

% php you.php
you

For perl, find the content inside quotes:

% echo 'See [ "you" later| perl -pe 's/^[^"]*"(.*)"[^"]*(.)$/$1$2/'

Remove the (.) and $2 if you don't wish to capture the newline.

清君侧 2025-02-19 23:19:18

使用awk的免提驾驶:

echo 'See [ "you" later' | 

gawk ++NF OFS= FS='^[^\"]*\"|\".*
  # any one of these 3, 
                                     # specific for this case
gawk '$_ = $--NF' FS='\"' 
mawk '$!--NF=$NF' FS='\"'  


you 

hands-free driving with awk :

echo 'See [ "you" later' | 

gawk ++NF OFS= FS='^[^\"]*\"|\".*
  # any one of these 3, 
                                     # specific for this case
gawk '$_ = $--NF' FS='\"' 
mawk '$!--NF=$NF' FS='\"'  


you 
在你怀里撒娇 2025-02-19 23:19:18
perl -nE  'say 
amp; if /(?<=")\w+/' <<< "See [ \"you\" later"
perl -nE  'say 
amp; if /(?<=")\w+/' <<< "See [ \"you\" later"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文