我有一个包含URL的文件。我正在尝试使用 shell 脚本从该文件中获取 URL。
在文件中,网址是这样的:
('URL', 'http://url.com');
Run Code Online (Sandbox Code Playgroud)
我尝试使用以下内容:
cat file.php | grep 'URL' | awk '{ print $2 }'
Run Code Online (Sandbox Code Playgroud)
它给出的输出为:
'http://url.com');
Run Code Online (Sandbox Code Playgroud)
但我只需要进入url.comshell 脚本中的一个变量。我怎样才能做到这一点?
ter*_*don 14
您可以使用简单的方法完成所有操作grep:
grep -oP "http://\K[^']+" file.php
Run Code Online (Sandbox Code Playgroud)
来自man grep:
-P, --perl-regexp
Interpret PATTERN as a Perl regular expression (PCRE, see
below). This is highly experimental and grep -P may warn of
unimplemented features.
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
Run Code Online (Sandbox Code Playgroud)
诀窍是\K在 Perl 正则表达式中使用which 表示discard everything matched to the left of the \K. 因此,正则表达式查找以http://(然后由于\K)开头的字符串,后跟尽可能多的非'字符。与 结合使用-o,这意味着只会打印 URL。
你也可以直接在 Perl 中完成:
perl -ne "print if s/.*http:\/\/(.+)\'.*/\$1/" file.php\
Run Code Online (Sandbox Code Playgroud)
Fra*_*que 11
像这样的东西?
grep 'URL' file.php | rev | cut -d "'" -f 2 | rev
Run Code Online (Sandbox Code Playgroud)
或者
grep 'URL' file.php | cut -d "'" -f 4 | sed s/'http:\/\/'/''/g
Run Code Online (Sandbox Code Playgroud)
去掉http://。
| 归档时间: |
|
| 查看次数: |
19305 次 |
| 最近记录: |