【问题标题】:Bash: how to use sed to replace only the last occurence in a file?Bash:如何使用 sed 仅替换文件中的最后一次出现?
【发布时间】:2012-04-23 18:41:37
【问题描述】:

有一个包含重复注释行的文件,例如:

# ScriptAlias /cgi-bin/ "somepath"
# ScriptAlias /cgi-bin/ "otherpath"

我只想在最后一次出现后添加一行,导致

# ScriptAlias /cgi-bin/ "somepath"
# ScriptAlias /cgi-bin/ "otherpath"
ScriptAlias /cgi-bin/ "mypath"

为此,我使用以下命令:

sed -i 's:^\(.*ScriptAlias /cgi-bin/.*\):\1 \nScriptAlias /cgi-bin/ "mypath":' file

但这会导致在每次出现后添加我的行,例如:

# ScriptAlias /cgi-bin/ "somepath"
ScriptAlias /cgi-bin/ "mypath"
# ScriptAlias /cgi-bin/ "otherpath"
ScriptAlias /cgi-bin/ "mypath"

我如何告诉 sed 只替换最后一次出现的内容?

已编辑:
如果无法使用 sed 解决它(如 cmets 中所述),请提供达到相同结果的替代方法,谢谢。



已编辑
重复的行可以分开,并且可以在它们之间使用其他行,例如

# ScriptAlias /cgi-bin/ "somepath"
# ScriptAlias /cgi-bin/ "otherpath"

# ScriptAlias /cgi-bin/ "another-path"
ScriptAlias /foo/ "just-jump"
# ScriptAlias /cgi-bin/ "that's the last"

【问题讨论】:

  • 这不是 sed 的工作。也许是 awk,但我会用 Python 或 Perl 来做。
  • @MK.Ok .. 我要求 sed 因为我正在使用它。如果无法使用 sed 解决它,欢迎使用任何其他方法。谢谢
  • 哦,你可以用 sed 做到这一点,在我看来这并不理想。

标签: bash replace sed awk last-occurrence


【解决方案1】:

使用tac,以便在您看到该模式时第一次打印新行:

tac file | awk '/ScriptAlias/ && ! seen {print "new line"; seen=1} {print}' | tac

【讨论】:

  • 我个人认为这是一个绝妙的主意,而且它仍然很容易阅读。我对其进行了一些个性化,并添加了直接写入文件的能力,(有没有办法将您的解决方案与交互式 -i 一起使用?)。我的命令终于是tac $file | awk '/ScriptAlias \/cgi-bin\// && ! seen {print "ScriptAlias /cgi-bin/ \"mypath\""; seen=1} {print}' | tac > "/var/lock/${file}" && mv "/var/lock/${file}" "${file}"
  • 无法直接写入输入文件。您必须使用临时文件(就像您所做的那样)。这就是sed -i 在后台所做的事情
【解决方案2】:

awk 的替代方案:

awk '/ScriptAlias \/cgi-bin\//{x=NR} {a[NR]=$0;}END{for(i=1;i<=NR;i++){if(i==x+1)print "$$$here comes new line$$$"; print a[i];}}' file

测试:

kent$  echo "# ScriptAlias /cgi-bin/ "somepath"
fooo
# ScriptAlias /cgi-bin/ "otherpath"
bar
"|awk '/ScriptAlias \/cgi-bin\//{x=NR} {a[NR]=$0;}END{for(i=1;i<=NR;i++){if(i==x+1)print "$$$here comes new line$$$"; print a[i];}}'

输出:

# ScriptAlias /cgi-bin/ somepath
fooo
# ScriptAlias /cgi-bin/ otherpath
$$$here comes new line$$$
bar

【讨论】:

    【解决方案3】:
    tail -r temp | awk '{line="yourline"}{if($0~/ScriptAlias/&&last==0){print line"\n"$0;last=1}else print}' | tail -r
    

    测试如下:

    krithika.337> cat temp
    # ScriptAlias /cgi-bin/ "somepath" 
    # ScriptAlias /cgi-bin/ "otherpath" 
    # ndmxriptAlias /cgi-bin/ "otherpath" 
    # ScriptAlias /cgi-bin/ "otherpath" 
    # bdjiptAlias /cgi-bin/ "otherpath" 
    krithika.338> tail -r temp | awk '{line="yourline"}{if($0~/ScriptAlias/&&last==0){print line"\n"$0;last=1}else print}' | tail -r
    # ScriptAlias /cgi-bin/ "somepath" 
    # ScriptAlias /cgi-bin/ "otherpath" 
    # ndmxriptAlias /cgi-bin/ "otherpath" 
    # ScriptAlias /cgi-bin/ "otherpath" 
    yourline
    # bdjiptAlias /cgi-bin/ "otherpath" 
    krithika.339>
    

    【讨论】:

    • GNU tail(来自 coreutils)没有-r 选项。
    【解决方案4】:

    这是编辑的任务。

    ex input_file << "DONE"
    /ScriptAlias \/cgi-bin\/ "otherpath"
    a
    ScriptAlias /cgi-bin/ "mypath"
    .
    :1
    /ScriptAlias \/cgi-bin\/ "another-path"
    a
    ScriptAlias /cgi-bin/ "just-jump"
    .
    :x
    DONE
    

    处于最后出现模式。

    ex input_file << "DONE"
    $
    ?ScriptAlias \/cgi-bin\/ "otherpath"
    a
    ScriptAlias /cgi-bin/ "mypath"
    .
    $
    ?ScriptAlias \/cgi-bin\/ "another-path"
    a
    ScriptAlias /cgi-bin/ "just-jump"
    .
    :x
    DONE
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多