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.
The reason why I need this is because I need to do some extra stuff if sed has successfully replaced a pattern.
I currently have:
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
The above code is a bit expensive and I would love to be able to use some hacks here.
This might work for you (GNU sed):
Explanation:
/'"$old_pattern"'/{s//'"$new_pattern"'/;h}
if the pattern space (PS) contains theold pattern
, replace it by thenew pattern
and copy the PS to the hold space (HS).${x;/./{x;q1};x}
on encountering the last line, swap to the HS and test it for the presence of any string. If a string is found in the HS (i.e. a substitution has taken place) swap back to the original PS and exit using the exit code of1
, otherwise swap back to the original PS and exit with the exit code of0
(the default).I know it is a old question and using awk instead of sed is perhaps the best idea, but if one wants to stick with sed, an idea is to use the -w flag. The file argument to the w flag only contains the lines with a match. So, we only need to check that it is not empty.
You could use
awk
instead:I'm ignoring the
-i
feature: you can use the shell do do redirections as necessary.I believe you may find these GNU sed extensions useful
and
It seems like exactly what are you looking for.
You can diff the original file with the sed output to see if it changed:
A bit late to the party but for the benefit of others, I found the 'w' flag to be exactly what I was looking for.
changelog.txt
will contain each change (ie the changed text) on it's own line. If there were no changes,changelog.txt
will be zero bytes.A really helpful sed resource (and where I found this info) is http://www.grymoire.com/Unix/Sed.html.