sd icon indicating copy to clipboard operation
sd copied to clipboard

Allow backup of input file

Open ghost opened this issue 7 years ago • 3 comments

-b flag, which creates a inputfile.bak backup before replacing.

ghost avatar Dec 27 '18 11:12 ghost

It will be great. But I think -o output flag will be more beneficial to create new file along with a choice to rename it as well. i.e.

❯ cat url.txt | sd -s 'google.com' 'google.test' -o url2.txt

Should output url2.txt with replaced string. But at the same time, it will easily be achieved via stdout

❯ cat url.txt | sd -s 'google.com' 'google.test' > url2.txt

But I agree what @whitfin has said https://github.com/chmln/sd/issues/1#issue-394260751

rahilwazir avatar Dec 28 '18 03:12 rahilwazir

I believe backing up files before replacing them is an edge case for the majority of people. If someone is worried about making the wrong replacements, is easy enough to just use pipes and create a new file instead. e.g. sd abc def < important_file.txt > important_file.new.txt

chmln avatar Jun 18 '19 20:06 chmln

I disagree about it being "easy enough" to use pipes instead, especially because

  1. your example doesn't even accomplish the desired behavior here; the new content should be in the old filename, and the original content should be in the new filename. The way you have it requires extra work afterward to get to the final state, whereas the desired behavior only requires paying any attention to the backup file if something goes wrong/surprising.
  2. if you want to do this on a huge tree of globbed files, you'll have to make a loop that carefully manages each placement, and this increases the probability of user error by a lot

Thanks for the great project, btw!

AndydeCleyre avatar Jun 13 '20 20:06 AndydeCleyre