Skip to main content
11 events
when toggle format what by license comment
Aug 17, 2021 at 11:34 comment added stackprotector In that case, one could then use cut -d , -f 1,3 <foo >foo.new && chmod --reference foo foo.new && chown --reference foo foo.new && mv -f foo.new foo to retain perms/ownership and protect against interruptions. Thx for clarification!
Aug 17, 2021 at 11:08 comment added Gilles 'SO- stop being evil' @stackprotector That works too, provided that the file isn't so large that you'd mind temporarily duplicating the data.
Aug 17, 2021 at 10:46 comment added stackprotector What about cp -f foo foo.new && cut -d , -f 1,3 <foo >foo.new && mv -f foo.new foo to retain perms/ownership AND protect against interruptions?
Dec 21, 2018 at 7:41 comment added Gilles 'SO- stop being evil' @LoMaPh Indeed, I don't know why I renamed the old file: it doesn't have any advantages over renaming the new file. It's also simpler because you don't need the step rm foo. And you shouldn't call rm foo, because mv foo.new foo is atomic: it removes the old version and puts the new version in place at the same time.
Dec 21, 2018 at 7:39 history edited Gilles 'SO- stop being evil' CC BY-SA 4.0
when creating a new file, don't affect the old file until the new version is ready
Dec 21, 2018 at 1:13 comment added LoMaPh I think this is simpler: cut -d , -f 1,3 foo > foo.new rm foo mv foo.new foo
Oct 26, 2018 at 7:06 comment added GypsyCosmonaut o.O Thanks, I didn't know there might be problems with null byte
Oct 26, 2018 at 6:48 comment added Gilles 'SO- stop being evil' @GypsyCosmonaut No, this is not “better”. It's more fragile. Its only benefit is that it's shorter to type. The main problem with your method is that if an error happens while processing the input file or writing the output, the data is lost. It also doesn't work with binary data: the output will be truncated at the first null byte. Even with text files, it removes empty lines from the end of the file. With large files, it may fail because the data has to be stored as a string in the shell's memory (and remember, if this happens, the data is lost).
Oct 26, 2018 at 3:10 comment added GypsyCosmonaut Better than .old method for in-place changes, echo "$(cut -d , -f 1,3 <foo)" > foo
Jan 14, 2011 at 1:05 vote accept Hank Gay
Jan 13, 2011 at 23:13 history answered Gilles 'SO- stop being evil' CC BY-SA 2.5