我怎么能执行此grep命令 [英] How can I execute this grep command
问题描述
我是想匹配正则表达式这个模式101
< A HREF =http://google.com> Google.com< / A>
< A目标=_空白的href =HTTP://example.com/files.html'>一个例子< / A>
<一个ID =link23HREF =file23.html目标=_ TOP>文件#23℃; / A>
< A HREF =图像/ mypic.png>看到我的照片<!/ A>
< A HREF =邮寄地址:joelross@uw.edu>电子邮件乔尔< / A>
和我做了这个regex-<?[AA]的 \\ S(HREF | HREF)\\ S = \\ S(|)。的(|)> * < / [AA]>
现在,当我试图通过我的命令行中使用grep命令,它会引发我一个错误。
./ mdlinks.sh:3号线:在寻找匹配`'意外EOF
./mdlinks.sh:4号线:语法错误:意外的文件结束
下面是源文件
#!在/ usr /斌/ bash的ENV
CONTENT = $(卷曲$ 1)
回声$内容| grep的-E -o'< AA] * \\ S(HREF | HREF)\\ S = \\ S。?(|)*(|)> *< \\ / [AA] >' >> mdlinks.txt
您需要逃避的正则表达式的单引号,也是你的家当有一个额外的空间(虽然这只是风格):
#!的/ usr / bin中/ env的庆典
CONTENT = $(卷曲$ 1)
回声$内容| grep的-E -o'< AA] * \\ S(HREF | HREF)\\ S = \\ S。?('\\''|)*。('\\''|)> *&LT ; \\ / [AA]>' >> mdlinks.txt
这可能是值得使用双引号的正则表达式,而不是单引号。你还是要躲避前pression里面的双引号,但是转义双引号是干净了一点:
#!的/ usr / bin中/ env的庆典
CONTENT = $(卷曲$ 1)
回声$内容|的grep -E -o。&所述[AA] * \\秒(HREF | HREF)\\ S = \\秒(?'| \\)*('| \\)&GT *下; \\ / [。 AA]>中>> mdlinks.txt
I was trying to match this pattern in regex 101
<a href="http://google.com">Google.com</a>
<A target="_blank" href='http://example.com/files.html'>An Example</A>
<a id="link23" HREF = "file23.html" target="_TOP">File #23</a>
<a href="images/mypic.png">See my picture!</a>
<a href="mailto:joelross@uw.edu">Email Joel</a>
and I made this regex- <[aA].\s(HREF|href)\s?=\s?('|").('|")>.*</[aA]>
now when I am trying to use the grep command via my command line,it throws me an error.
./mdlinks.sh: line 3: unexpected EOF while looking for matching `"'
./mdlinks.sh: line 4: syntax error: unexpected end of file
Here is the source file
#! /usr/bin/env bash
CONTENT=$(curl $1)
echo "$CONTENT" | grep -E -o '<[aA].*\s(HREF|href)\s?=\s?('|").*('|")>.*<\/[aA]>' >> mdlinks.txt
You need to escape the single quotes in the regex, and also your shebang has an extra space (although that's just style):
#!/usr/bin/env bash
CONTENT=$(curl $1)
echo "$CONTENT" | grep -E -o '<[aA].*\s(HREF|href)\s?=\s?('\''|").*('\''|")>.*<\/[aA]>' >> mdlinks.txt
It might be worth using double quotes for the regex, rather than single quotes. You'll still have to escape the double quotes inside the expression, but escaping double quotes is a little cleaner:
#!/usr/bin/env bash
CONTENT=$(curl $1)
echo "$CONTENT" | grep -E -o "<[aA].*\s(HREF|href)\s?=\s?('|\").*('|\")>.*<\/[aA]>" >> mdlinks.txt
这篇关于我怎么能执行此grep命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!