【问题标题】:trim whitespace inside angle brackets in sed修剪sed中尖括号内的空格
【发布时间】:2013-02-28 21:03:35
【问题描述】:

我实际上在撰写问题时解决了这个问题,但我认为它可能比我做的方式更整洁。

除了出现在 s 中的 url 合法内容(来自 rdf/n3 实体)之外,我想修剪空格和大多数标点符号。

源文本示例如下:
<this is a problem> <this_is_fine> "this is ok too" . <http://WeDontNeedToTouchThis.> <http:ThisContains"Quotes'ThatWillBreakThings> "This should be 'left alone'." .

输出需要将空格转换为下划线并修剪引号以及 url/iri 中不合法的任何内容。

<http://This is a "problem"> => <http://This_is_a_problem>

这些都没用。
sed -e 's/\(<[^ ]*\) \(.*>\)/\1_\2/g' badDoc.n3 | head sed '/</,/>/{s/ /_/g}' badDoc.n3 | head

我的最终解决方案似乎可行,是:
sed -e ':a;s/\(<[^> ]*\) \(.*>\)/\1_\2/g;ta' badDoc.n3 | sed -e ':b;s/\(<[:/%_a-zA-Z0-9.\-]*\)[^><:/%_a-zA-Z0-9.\-]\(.*>\)/\1\2/g;tb' > goodDoc.n3

有没有更好的办法?

【问题讨论】:

  • 不知道你想做什么。源文本的输出是什么?
  • 我希望你意识到你不能在不改变文件含义的情况下改变尖括号内的字符。此外,“'”是 n3 中的保留字符,生成此类文件的任何内容都已损坏,应予以修复。
  • 我知道,我们正在生成 n3,虽然它在我们的导入过程中已修复,但我正在处理一批 n3,其中包含 IRI 中未转义的字符串(主要是文件名,包括引号),这些需要在我们处理该批次之前进行清洁。
  • +1 个有趣的问题。

标签: regex sed quotes n3


【解决方案1】:

首先,我想说这是一个有趣的问题。它看起来是一个简单的替换问题,但是如果进入它,它并不像我想象的那么容易。当我在寻找解决方案时,我确实想念 vim !!!... :)

我不知道sed 是否是这个问题的必要条件。我会用 awk 来做:

awk '{t=$0;
        while (match(t,/<[^>]*>/,a)>0){
                m[++i]=a[0];n[i]=a[0];t=substr(t,RSTART+RLENGTH)
        }
        for(x in n){
                gsub(/[\x22\x27]/,"",n[x])
                gsub(/ /,"_",n[x])
                sub(m[x],n[x])
        }}1' file

用你的例子测试一下:

kent$  cat file
<this is a problem> <this_is_fine> "this is ok too" . <http://WeDontNeedToTouchThis.> <http:ThisContains"Quotes'ThatWillBreakThings> "This should be 'left alone'." .

kent$  awk '{t=$0;
        while (match(t,/<[^>]*>/,a)>0){
                m[++i]=a[0];n[i]=a[0];t=substr(t,RSTART+RLENGTH)
        }
        for(x in n){
                gsub(/[\x22\x27]/,"",n[x])
                gsub(/ /,"_",n[x])
                sub(m[x],n[x])
        }}1' file
<this_is_a_problem> <this_is_fine> "this is ok too" . <http://WeDontNeedToTouchThis.> <http:ThisContainsQuotesThatWillBreakThings> "This should be 'left alone'." .

好吧,这并不是真正的单线,看看是否还有其他更短的解决方案。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 2014-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-05
    • 1970-01-01
    相关资源
    最近更新 更多