Lets say you want to run a sed command, but you want it to save a backup before editing a file.

Here is how you do that:

sed -i.bak -e 'SED EXPRESSION GOES HERE' filename1<span style="font-family: Lato, sans-serif; font-size: 16px; line-height: 1.5; background-color: #ffffff;"> </span>

This will take filename1 and apply the SED EXPRESSION to it, but it will make an original untouched copy of filename1 to filename1.bak. Why .bak? well because thats what we told it to do with -i.bak.

Here is an example:

# cat dog.txt
black=3
white=2
stripped=5

Now run this:

sed -i.backup -e 's/^black=.*/black=10/g' dog.txt

End result:

# cat dog.txt
black=10
white=2
stripped=5

Here is our backup (notice now its .backup, because thats what I put after the -i)

# cat dog.txt.backup
black=3
white=2
stripped=5

 

Leave a Reply

Your email address will not be published. Required fields are marked *