【问题标题】:execute a command if substitution succeded如果替换成功则执行命令
【发布时间】:2014-01-21 21:25:08
【问题描述】:

我想用 sed 从 perl 脚本中删除 \"

 sed -ne ' 
    #(here some substitutions...)
    s/print "\(.*[^"]\)"/\1/p;
 ' | \
 sed -e 's/\\"/"/g'

是否可以只在第一次替换完成的行上用" 替换\"?换句话说,将这个脚本放在一行中?

分支并不酷,因为如果已经完成了未列出的先前替换,则条件被认为是真实的(但尚未完成最新的替换)......

示例:

#! /usr/bin/perl
(...)
while (@someArray) {
    print "la variable \"$_\" est cool!\n"; 
    syslog ('info|machin', "la variable \"$_\" est cool!"); 
}

"la variable "$_" est cool!\n"

但在

中没有可能的替换
 syslog ('info|machin', "la variable \"$_\" est cool!"); 

如果之前选择了此行。

【问题讨论】:

  • 我认为一些具有预期输出的示例输入数据对那些希望帮助您的用户非常有帮助。
  • @MUYBelgium 能否提供样例输入输出???
  • 从您的描述来看,如果您坚持使用sed (single sed)tT(gnu sed) 是唯一的出路。

标签: sed


【解决方案1】:
sed -ne ' 
# if other substitution are to be made without regarding of s/print....
#(here some substitutions...) 

    s/print "\(.*[^"]\)"/\1/;
    t bs

# if other substitution are to be made if /print... is NOT found
#(here some substitutions...) 
    b

: bs
# if other substitution are to be made if /print... is found
#(here some substitutions...) 
    s/\\"/"/g
    p
'

s// 之后,有t 表示测试和转到标签(在这种情况下为bs)如果为真。

所以这里,替换,如果发生,转到bs 并进行其他替换而不是打印结果,如果不是真的,转到最后(b 后面没有标签)

(代码审查由于对其他替代的不同解释)

【讨论】:

  • 即使之前发生了替换,也会采用 bs 分支。不仅是最新的。
  • s/print.... 发生而不是对其进行测试,如果进行了替换,则转到 bs,如果没有,则转到 b(ranch) 以结束操作。测试来自上次替换(应用或不应用),因此测试在 s// 之后
  • :-) 解释可能会改变“其他替代”的结果。结果我修改了代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 1970-01-01
  • 2019-05-13
  • 2015-05-12
相关资源
最近更新 更多