Regex 如何从awk结果中提取文本?

Regex 如何从awk结果中提取文本?,regex,awk,sed,grep,Regex,Awk,Sed,Grep,此命令返回找到单词About的整行 ! lynx -source google.com/search?q=india | awk '/About */' 这将返回类似这样的内容 <div id=gbar><nobr><b class=gb1>Search</b> <a class=gb1 href="http://www.google.com/search?hl=en&tbm=isch&source=og&tab=w

此命令返回找到单词About的整行

! lynx -source google.com/search?q=india | awk '/About */'
这将返回类似这样的内容

<div id=gbar><nobr><b class=gb1>Search</b> <a class=gb1 href="http://www.google.com/search?hl=en&tbm=isch&source=og&tab=wi">Images</a> <a class=gb1 href="http://maps.google.com/maps?hl=en&tab=wl">Maps</a> <a class=gb1 href="https://play.google.com/?hl=en&tab=w8">Play</a> <a class=gb1 href="http://www.youtube.com/results?gl=US&tab=w1">YouTube</a> <a class=gb1 href="http://news.google.com/nwshp?hl=en&tab=wn">News</a> <a class=gb1 href="https://mail.google.com/mail/?tab=wm">Gmail</a> <a class=gb1 href="https://drive.google.com/?tab=wo">Drive</a> <a class=gb1 style="text-decoration:none" href="https://www.google.com/intl/en/about/products?tab=wh"><u>More</u> &raquo;</a></nobr></div><div id=guser width=100%><nobr><span id=gbn class=gbi></span><span id=gbf class=gbf></span><span id=gbe></span><a href="http://www.google.com/history/optout?hl=en" class=gb4>Web History</a> | <a  href="/preferences?hl=en" class=gb4>Settings</a> | <a target=_top id=gb_70 href="https://accounts.google.com/ServiceLogin?hl=en&passive=true&continue=http://www.google.com/search%3Fq%3Dindia" class=gb4>Sign in</a></nobr></div><div class=gbh style=left:0></div><div class=gbh style=right:0></div><font size="-2"><br clear="all"></font><table border="0" cellpadding="3" cellspacing="0"><tr><td valign="top"><a href="/webhp?hl="><img src="/images/branding/searchlogo/1x/googlelogo_desk_heirloom_color_150x55dp.gif" height="55" width="150" border="0"></a></td><td valign="bottom"><nobr><form name="gs" method="GET" action="/search"><input type="text" name="q" maxlength="2048" title="Search" value="india" size="41"><font size="-1">&nbsp;</font><input type="Submit" name="btnG" value="Search"><font size="-1">&nbsp;</font></form></nobr></td><td width="100%" valign="middle"><nobr><font size="-2"><a href="/advanced_search?q=india&amp;hl=">Advanced Search</a><br><a href="/preferences?q=india&amp;hl=">Preferences</a></font></nobr></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td bgcolor="#3366CC" height="1"><img height="1" width="1" alt=""></td></tr></table><table width="100%" border="0" cellpadding="0" cellspacing="0" bgcolor="#D5DDF3"><tr><td><img height="2" width="1" alt=""></td></tr><tr><td><table width="100%" border="0" cellpadding="0" cellspacing="4"><tr><td nowrap><font size="-1"><strong>Web</strong></font></td><td nowrap align="right"><font size="-1">About 10,730,000,000 results (<b>0.34</b> seconds)</font></td></tr></table></td></tr><tr><td><img height="1" width="1" alt=""></td></tr></table><p><a href="/url?q=https://en.wikipedia.org/wiki/India&amp;sa=U&amp;ved=2ahUKEwiih9Hri9niAhWvuVkKHXWUCLkQFjAAegQIDBAH&amp;usg=AOvVaw2q-I4x7L6MSaWE9ziLkwjR"><b>India</b> - Wikipedia</a><table cellpadding="0" cellspacing="0" border="0"><tr><td class="j"><font size="-1"><b>India</b> (ISO: Bh&#257;rat), also known as the Republic of <b>India</b> (ISO: Bh&#257;rat Ga&#7751;ar&#257;jya), <br>
但是我只想返回结果的数量。例如

预期结果:

约797000000个结果

或者只有数字没有像About这样的词

我实际上想要的是返回计数中的逗号数。例如,上面的示例有3个逗号,这意味着计数非常高,如果没有,少于1000个结果,计数应返回低

使用awk的grep intead。o选项只打印与regexp匹配的部分行,而不是整行

lynx -source google.com/search?q=india | grep -o 'About [0-9,]+ results'

这对于您来说应该很容易做到,其表达式类似于:

([0-9,]+)
对于逗号,我们只使用:

(,)

如果只需要数字中的逗号计数,可以使用:

pax> echo 'About 7,970,000,000 results' | awk '/About/{gsub(/[^,]/,"",$0);print length($0)}'
3
更详细地说:

/About/-选择带有About的行 gsub/[^,]/,$0-将整行中的所有非逗号替换为零 打印长度$0-逗号的打印输出计数 请记住,只有当Lynx的输出与您最初在我的echo中所说的完全一致时,才是如此,事实证明,它不是

对于大量HTML的实际输出,可能会有更多的逗号。在这种情况下,你需要去掉所有的东西,直到About,然后从下一个空格中去掉所有的东西,只留下数字,然后在那一位计算逗号

以下内容似乎与您的实际查询配合得很好,前两个gsub命令除去了数字本身之外的所有内容,剩下的就是我上面提出的内容:

awk '/About /{
       gsub(/^.*About /,"",$0);
       gsub(/ .*$/,"",$0);
       gsub(/[^,]/,"",$0);
       print length($0)}'
使用-dump代替-source。这消除了html代码:

$ lynx -dump google.com/search?q=india | awk -F, '/About/'
   Web About 7,410,000,000 results (0.33 seconds)
要仅打印号码,请执行以下操作:

$ lynx -dump google.com/search?q=india | awk '/About/{print $3}'
7,410,000,000
要仅打印逗号数,请执行以下操作:

$ lynx -dump google.com/search?q=india | awk -F, '/About/{print NF-1}'
3

我试过了。grep部分不返回任何内容。grep GNU grep 2.25当我将它传输到lynx命令而不是echo时,如您的示例所示,我得到的不是53@shantanuo:那么您的Lynx输出很可能不是您在问题中所述的。我已经检查过了,事实确实如此,所以我编辑了相应的答案。如果您想解析HTML,请使用HTML解析器。有很多选择,只要停止在HTML源代码上使用awk、sed或grep即可。