删除本地文件URL的文件名部分

时间:2014-10-14 17:22:37

标签: regex macos bash awk sed

我有一个HTML文档,其中包含指向大约一百个本地文件的链接。我想使用sed,awk或perl(按照首选顺序)删除URL的文件名部分,直到URL中的最后一个反斜杠。在下面的示例中,我只显示构成本地文件路径的HTML代码的一部分。

示例:

<a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/SoftwarePackageName.dmg">

处理后的例子:

<a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/">

在测试中我尝试了不同的正则表达式组合来实现这一点但是我只得到“.dmg”或它和.dmg左边的所有内容我真的只想删除“SoftwarePackageName.dmg”部分。 BTW在某些情况下,它是“SoftwarePackageName.zip”,“CompanyName”或“SoftwarePackageName.dmg”中可能有一个空格显示为“%20”。我还在撰写此帖时回顾了“可能已经有您答案的问题”。

编辑:我感谢您花时间尝试和帮助,当然理解由于政策导致的困难,我无法提供更多我做的例子,因此我只需手动编辑html文档。我已经花了很多时间和其他人。下次还需要更多关于正则表达式的阅读。感谢所有贡献。 :)

4 个答案:

答案 0 :(得分:0)

试试这个:

sed 's|\(<a href="file:///[^>]*/\).*">|\1">|g'

演示:

$ sed 's|\(<a href="file:///[^>]*/\).*\.\(dmg\|zip\)">|\1">|g' <<EOF
> <a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/SoftwarePackageName.dmg">
> foo bar <a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/SoftwarePackageName.dmg"> baz quux
> EOF
<a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/">
foo bar <a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/"> baz quux

答案 1 :(得分:0)

您可以尝试以下sed命令。

sed 's/\(<a href="[^."]*\/\)[^."\/]*\.[^."\/]*">/\1">/g' file

答案 2 :(得分:0)

modded

我删除了之前的sed正则表达式(我无法测试它) 相反,我发布了一个扩展的正则表达式(详细),应该有助于你开始。

 # Unknown extension: (<a\s+[^>]*?href\s*=\s*(["'])[^>]*?/)([^/."'>]+\.[^/."'>]+)\2
 # Known extension: (<a\s+[^>]*?href\s*=\s*(["'])[^>]*?/)([^/."'>]+\.dmg\b[^/."'>]*)\2
 # Replacement: $1$2     

 (                                     # (1 start), Tag and Url part to keep
      <a \s+ [^>]*? href \s* = \s* 
      ( ["'] )                              # (2), Quote
      [^>]*? 
      /                                     # End of directories
 )                                     # (1 end)
 (                                     # (3 start), Throw away filename
      [^/."'>]+                             # - Filename (not /."'> chars)
      \.                                    # - Dot

      # - Extension and parameters
      # ----------------------------
      # Use one of these lines (but not both)

      # Known extensions ->
      #dmg \b [^/."'>]* 

      # Unknown extensions ->
      [^/."'>]+ 
 )                                     # (3 end)
 \2                                    # Backref to Quote

Sed不应该使用很多不同的替代结构s///g 可能是您必须转义括号元字符。但我认为那是 这个正则表达式。这些正则表达式处于原始状态。

这里它们用于示例Perl程序。从命令行可以很容易地使用Perl。

use strict;
use warnings;

$/ = undef;

my $html = <DATA>;  # slurp in the entire file
my $htmlcopy = $html;

$html =~ s|(<a\s+[^>]*?href\s*=\s*(["'])[^>]*?/)([^/."'>]+\.[^/."'>]+)\2|\1\2|g;
print "Replaced using Unknown extensions:\n", $html, "\n";

$htmlcopy =~ s|(<a\s+[^>]*?href\s*=\s*(["'])[^>]*?/)([^/."'>]+\.dmg\b[^/."'>]*)\2|\1\2|g;
print "Replace using Known extensions:\n", $htmlcopy, "\n\n";


__DATA__

<a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/SoftwarePackageName.dmg">
<a rel="nofollow" class="external text" href="http://www.ielts.org/researchers/analysis-of-test-data/">
<a rel="nofollow" class="external text" href="http://qiyas.sa/Sites/English/Tests/LanguageTests/Pages/Standardized-Test-for-English-Proficiency-(STEP).aspx">
<a rel="nofollow" class="external free" href="http://www.ielts.org/about_us.aspx">
<a href="/w/index.php?title=IELTS&amp;redirect=no" title="IELTS">
<a href="/wiki/File:IELTS_logo.svg" class="image">
<a href="/w/index.php?title=International_English_Language_Testing_System&amp;action=edit&amp;section=1" title="Edit section: IELTS characteristics">
<a href="/w/index.php?title=Band_score&amp;action=edit&amp;redlink=1" class="new" title="Band score (page does not exist)">
<a href="/w/index.php?title=International_English_Language_Testing_System&amp;action=edit&amp;section=2" title="Edit section: IELTS test structure">
<a href="/wiki/University_of_St._Andrews" title="University of St. Andrews" class="mw-redirect">
<a rel="nofollow" class="external text" href="http://bandscore.ielts.org/search.aspx">
<a rel="nofollow" class="external text" href="http://www.bristol.ac.uk/university/governance/policies/admissions/language-requirements.html#toc05">
<a href="#cite_ref-11">
<a href="/wiki/Special:BookSources/1405833122" class="internal mw-magiclink-isbn">

输出&gt;&gt;

Replaced using Unknown extensions:

<a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/">
<a rel="nofollow" class="external text" href="http://www.ielts.org/researchers/analysis-of-test-data/">
<a rel="nofollow" class="external text" href="http://qiyas.sa/Sites/English/Tests/LanguageTests/Pages/">
<a rel="nofollow" class="external free" href="http://www.ielts.org/">
<a href="/w/" title="IELTS">
<a href="/wiki/" class="image">
<a href="/w/" title="Edit section: IELTS characteristics">
<a href="/w/" class="new" title="Band score (page does not exist)">
<a href="/w/" title="Edit section: IELTS test structure">
<a href="/wiki/" title="University of St. Andrews" class="mw-redirect">
<a rel="nofollow" class="external text" href="http://bandscore.ielts.org/">
<a rel="nofollow" class="external text" href="http://www.bristol.ac.uk/university/governance/policies/admissions/">
<a href="#cite_ref-11">
<a href="/wiki/Special:BookSources/1405833122" class="internal mw-magiclink-isbn">


Replace using Known extensions:

<a href="file:///Volumes/VolumeName/Download/Mac%20Software/CompanyName/">
<a rel="nofollow" class="external text" href="http://www.ielts.org/researchers/analysis-of-test-data/">
<a rel="nofollow" class="external text" href="http://qiyas.sa/Sites/English/Tests/LanguageTests/Pages/Standardized-Test-for-English-Proficiency-(STEP).aspx">
<a rel="nofollow" class="external free" href="http://www.ielts.org/about_us.aspx">
<a href="/w/index.php?title=IELTS&amp;redirect=no" title="IELTS">
<a href="/wiki/File:IELTS_logo.svg" class="image">
<a href="/w/index.php?title=International_English_Language_Testing_System&amp;action=edit&amp;section=1" title="Edit section: IELTS characteristics">
<a href="/w/index.php?title=Band_score&amp;action=edit&amp;redlink=1" class="new" title="Band score (page does not exist)">
<a href="/w/index.php?title=International_English_Language_Testing_System&amp;action=edit&amp;section=2" title="Edit section: IELTS test structure">
<a href="/wiki/University_of_St._Andrews" title="University of St. Andrews" class="mw-redirect">
<a rel="nofollow" class="external text" href="http://bandscore.ielts.org/search.aspx">
<a rel="nofollow" class="external text" href="http://www.bristol.ac.uk/university/governance/policies/admissions/language-requirements.html#toc05">
<a href="#cite_ref-11">
<a href="/wiki/Special:BookSources/1405833122" class="internal mw-magiclink-isbn">

答案 3 :(得分:0)

首先,我想再次说明我真正感谢那些试图帮助的人所花的时间!其次,我不得不说在现实世界的应用程序中没有提供任何内容,而且至少,我认为没有我想修改的实际文件可以使用而且我很抱歉允许提供它。是的,你的演示工作但不幸的是,它们根本不代表文档中的实际html编码,也许是因为&#34;生成器&#34; &#34; Cocoa HTML Writer&#34;从RTF文档来看,这可能与它有关,目前还不确定。即使我只使用一个包含示例代码的完整行,将其单独放在一个文件中然后处理它,但所有解决方案都失败了。我希望我能提供文件或花时间弄清楚为什么在现实世界中使用它会失败,但我无法做到。

文档的一些背景是最初在TextEdit中创建为RTF文档时,包含了目标文件的FQP,因为OS X的版本将打开目标文件,但是在OS X的更高版本中它只打开Finder到目标文件的位置。因此,不再需要将FQP仅用于其位置的路径到目标文件。这实际上使得随着时间的推移更容易更新RTF文档。有时,此RTF文档将导出到要修改的HTML文档,然后另存为RTF文档。正如我之前提到的那样,也许是#34; Generator&#34;是&#34; Cocoa HTML Writer&#34;来自TextEdit中的RTF文档部分归咎于为什么处理因提议的解决方案而失败。

无论如何,我冗长回复的原因是以正确的角度来看待这个问题,并解释我是如何解决这个问题的。正如我之前提到的那样,我只是想手动编辑文件,但是在提供了慷慨的帮助之后,我想找到一些自动解决方案而且我做了。

主要常量是之前提供的示例代码,因此这里只关注它是我用来处理文件的命令行。

grep -o 'file:///[^"]*' Build_Out_Template.html | rev | cut -d / -f 1 | rev | while read LINE; do sed -i "s/${LINE}//" Build_Out_Template.html; done

使用&#34; grep -o&#39; file:/// [^&#34;] *&#39;&#34;使我能够只提取文档中行的目标部分。我通过转速将它通过转速来反转字符顺序并通过剪切管道它只给出了反向线中第一个斜线的部分(在原始线中的最后一个斜线之后)然后不得不再通过转换管道明显的原因。然后通过一个循环传递,其中sed使用非常简单的指令与复杂的正则表达式,使用字面上的SoftwarePackageName.dmg等文件名。虽然花了更多的时间在这上面,然后手动编辑文件,但我把它作为一个挑战,并记住,有时候思考的外包装解决方案更快更容易,我会记住这个为其他一些如果需要,可以申请。

再次感谢所有试图提供帮助的人,我们非常感激。

相关问题