How to check if sed has changed a file

后端 未结 9 1341
难免孤独
难免孤独 2020-12-01 06:57

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

9条回答
  •  醉话见心
    2020-12-01 07:32

    You can diff the original file with the sed output to see if it changed:

    sed -i.bak s:$pattern:$new_pattern: "$filename"
    if ! diff "$filename" "$filename.bak" &> /dev/null; then
      echo "changed"
    else
      echo "not changed"
    fi
    rm "$filename.bak"
    

提交回复
热议问题