Reputation: 29
Im trying to append a line in a file using following sed command
sed -i 's/id="1asda"/id="1asda"\n peer="120"/g' a.xml.
Now the problem is that even if I'm giving a wrong id, sed is not popping an error.How to get an error if the pattern is not found.
Upvotes: 0
Views: 32
Reputation: 158050
I would suggest to use awk
:
awk 'BEGIN{e=1}gsub(/foo1/, "bar",$0){e=0};1;END{exit e}' file
The above command will give you a return value of 1
if no substitution has been performed, otherwise 0
.
Upvotes: 0
Reputation: 17761
sed
is used to replace text. Having nothing to replace is not an error.
Use something like grep
to check whether there's something to replace or not. For example:
if grep -q 'id="1asda"' a.xml
then
sed -i 's/id="1asda"/id="1asda"\n peer="120"/g' a.xml
else
echo 'nothing to do' >& 2
fi
Upvotes: 1