Unix shell bash “one-liner”隔离包含包含“.mp3”的 URL 的所有括号
我对 Unix bash 完全陌生——这是第一个问题!希望你们能够提供帮助:)
问题:
我有大量凌乱的网络源代码(包装/未格式化),其中多次出现以下内容:
('http://www.example.com/path/audio .mp3')
能否请您帮助使用单行代码(sed/awk...)来隔离这些出现的括号,其中包含包含“.mp3”的 URL,干净的前导/尾随“ ()”和“ '”字符,然后作为列表(每行一个)打印到活动的 .txt 文件。
注意:单行代码将在 Mac 上的 Automator 中用作对“选定文本”进行操作的服务/工作流程。
任何帮助将不胜感激,因为(尽管浏览了所有在线图)我完全迷失了。
最好的问候,
戴夫
I'm completely new to this Unix bash stuff — and first question here! Hope you guys can help:)
Problem:
I have a mass of messy web source code (wrapping/unformatted) containing multiple occurrences of:
('http://www.example.com/path/audio.mp3')
Could you please help with a one-liner (sed/awk...) that will isolate these occurrences of parentheses containing a URL that includes ".mp3", clean leading/trailing "()" and " ' " characters, and then print as list (one per line) to an active .txt file.
Note: The one-liner will be used in Automator on Mac as a service/workflow to action on 'selected text.'
Any help would be greatly appreciated as (despite trawling through all the online tuts) I'm completely lost.
Best Regards,
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用
egrep
和-o
(仅输出匹配的部分)应该可以解决问题。尝试这样的事情:Using
egrep
with-o
(output only the parts that match) should do the trick. Try something like this:PERL,Mac 应该有。
PERL, which Mac should have.
尝试细化以下内容:
它一次一行读取标准输入(此处:
input_file
),查找该行中每次出现的“url”并将其打印到标准输出(此处:输出文件
),没有('
和')
。Try to refine the following:
It read stdin (here:
input_file
) one line at a time, looks for every occurrence of a "url" in that line and prints it to stdout (here:output_file
) without('
and')
.awk '{print $2}' FS="('|')" <文件名
cat 文件名| tr ')' '\n' | awk '{print $2}' FS="('|')" > output.txt
只需将
filename
替换为包含这些行的文件的名称。或者
尝试一下:
tr ')' '\n' | awk '{print $2}' FS="('|')"
awk '{print $2}' FS="('|')" < filename
cat filename | tr ')' '\n' | awk '{print $2}' FS="('|')" > output.txt
Just replace
filename
with the name of your file containing these lines..OR
JUST A TRY:
tr ')' '\n' | awk '{print $2}' FS="('|')"
这将匹配出现在括号和单引号内的 URL:
输出 URL,每行一个,不带括号或单引号。 Perl 兼容正则表达式的
-P
选项(至少)在 GNU 和 OS Xgrep
版本中可用。This will match the URLs that appear within parentheses and single quotes:
The URLs are output, one per line, without the parentheses or single quotes. The
-P
option for Perl-compatible regular expressions is available (at least) in GNU and OS Xgrep
versions.