【问题标题】:Extract just the depot path from perforce output仅从 perforce 输出中提取 depot 路径
【发布时间】:2011-06-10 06:37:19
【问题描述】:

已经在这里尝试了一些“提取到一个字符”的问题,但没有任何效果。 在 grep -o 中使用它

示例输入: //blah/blah/foo.txt#7 - 通过 blah@blah 编辑更改 12526(文本)

我想得到: //blah/blah/foo.txt

现在我正在使用正则表达式 '/[^#]*/' 但我明白了 //废话/废话/

【问题讨论】:

  • 什么工具?珀尔?红宝石? Python? grep?您确实需要提供更多信息
  • 对不起,这只是在 grep 上使用

标签: regex perforce


【解决方案1】:

grep 不使用斜线来分隔模式,因此斜线是按字面意思匹配的。

试试:

echo '//blah/blah/foo.txt#7 - edit change 12526 (text)' | grep -o '/[^#]*'

请注意,如果“文本”中有斜线,则该部分也将匹配,因此将由grep -o 打印。所以我会选择这样的东西:

echo '//blah/blah/foo.txt#7 - edit change 12526 (text)' | sed 's/#.*//'

(当然,这两个都假设你的文件名中没有#。)

【讨论】:

  • 做到了,谢谢。 # 后面的文字不应该有斜线:)
【解决方案2】:

使用cut

<whatever it takes to get your output> | cut -f1 -d"#"

所以,如果您正在 grepping 文件的内容或类似内容。

grep -o "whatever text" inputFile | cut -f1 -d"#"

-d"#" 表示使用“#”作为分隔符,-f1 表示第一个字段。

【讨论】:

    【解决方案3】:

    试试这个:/^.*(?=#)/ 它使用(非消耗)前瞻

    【讨论】:

      【解决方案4】:

      在编写这样的脚本时使用“标记”模式会很有帮助,因为标记输出更易于解析和处理。请参阅http://www.perforce.com/perforce/r10.2/manuals/cmdref/o.gopts.html#1040647 中的“-ztag”并查看http://www.perforce.com/perforce/r10.2/manuals/p4script/index.html

      【讨论】:

        猜你喜欢
        • 2015-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-20
        • 1970-01-01
        • 2019-04-27
        相关资源
        最近更新 更多