如何检查是否sed的改变了文件 [英] How to check if sed has changed a file

查看:141
本文介绍了如何检查是否sed的改变了文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种聪明的方法来计算出,如果传递给sed的文件已经成功或没有改变。

I am trying to find a clever way to figure out if the file passed to sed has been altered successfully or not.

基本上,我想知道,如果该文件已被更改或不,而不看文件的修改日期。

Basically, I want to know if the file has been changed or not without having to look at the file modification date.

为什么我需要的,这是因为我需要做一些额外的东西,如果用sed已经成功地取代了图案的原因。

The reason why I need this is because I need to do some extra stuff if sed has successfully replaced a pattern.

我目前有:

    grep -q $pattern $filename
    if [ $? -eq 0 ]
    then
        sed -i s:$pattern:$new_pattern: $filename
                # DO SOME OTHER STUFF HERE
    else
        # DO SOME OTHER STUFF HERE
    fi

以上code是有点贵,我很想能够在这里使用一些黑客。

The above code is a bit expensive and I would love to be able to use some hacks here.

推荐答案

您可以使用 AWK 而不是:

awk "/$pattern/"' { gsub( "'"$pattern"'", "'"$repl"'" ); t=1 }
    1; END{ exit( !t )}'

我忽略了 -i 功能:可以使用shell做做重定向必要

I'm ignoring the -i feature: you can use the shell do do redirections as necessary.

这篇关于如何检查是否sed的改变了文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆