How can I extract a substring in a line on linux

时间:2015-06-27 02:56:54

标签: linux shell terminal

I want to extract a substring inside of a google fonts css. In the text below all I need is the url so I can download the fonts locally to my machine. Any help will be appreciated.

src: local('Source Sans Pro Semibold Italic'), local('SourceSansPro-SemiboldIt'), url(https://fonts.gstatic.com/s/sourcesanspro/v9/fpTVHK8qsXbIeTHTrnQH6EfrksRSinjQUrHtm_nW72g.ttf) format('truetype');

2 个答案:

答案 0 :(得分:0)

在ksh或bash脚本中,您可以使用perl:

UrlExtract=$(perl -ne 'print $1 if m/\burl\b\(([^)]+)/' css_file_name)

或seds:

UrlExtract=$(sed -r 's/.*\burl\b\(([^)]+).*/\1/' css_file_name)

如果它不在shell脚本中,请告诉我们您是如何尝试提取此信息的。

答案 1 :(得分:0)

您还可以使用反向引用

wantedurl=$(sed -e 's/^.*\(http.*ttf\).*$/\1/' filename)

(例如,您在文件url.txt中的示例)

$ sed -e 's/^.*\(http.*ttf\).*$/\1/' url.txt
https://fonts.gstatic.com/s/sourcesanspro/v9/fpTVHK8qsXbIeTHTrnQH6EfrksRSinjQUrHtm_nW72g.ttf