Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I execute any command editing its file (argument) "in place" using bash?

I have a file temp.txt, that I want to sort with the sort command in bash.

I want the sorted results to replace the original file.

This doesn't work for example (I get an empty file):

sortx temp.txt > temp.txt 

Can this be done in one line without resorting to copying to temporary files?


EDIT: The -o option is very cool for sort. I used sort in my question as an example. I run into the same problem with other commands:

uniq temp.txt > temp.txt. 

Is there a better general solution?

like image 790
jm. Avatar asked Sep 28 '08 18:09

jm.


People also ask

How can you edit command lines in bash?

Command line editing is enabled by default when using an interactive shell, unless the --noediting option is supplied at shell invocation. Line editing is also used when using the -e option to the read builtin command (see Bash Builtins). By default, the line editing commands are similar to those of Emacs.


2 Answers

sort temp.txt -o temp.txt 
like image 101
daniels Avatar answered Sep 28 '22 04:09

daniels


A sort needs to see all input before it can start to output. For this reason, the sort program can easily offer an option to modify a file in-place:

sort temp.txt -o temp.txt 

Specifically, the documentation of GNU sort says:

Normally, sort reads all input before opening output-file, so you can safely sort a file in place by using commands like sort -o F F and cat F | sort -o F. However, sort with --merge (-m) can open the output file before reading all input, so a command like cat F | sort -m -o F - G is not safe as sort might start writing F before cat is done reading it.

While the documentation of BSD sort says:

If [the] output-file is one of the input files, sort copies it to a temporary file before sorting and writing the output to [the] output-file.

Commands such as uniq can start writing output before they finish reading the input. These commands typically do not support in-place editing (and it would be harder for them to support this feature).

You typically work around this with a temporary file, or if you absolutely want to avoid having an intermediate file, you could use a buffer to store the complete result before writing it out. For example, with perl:

uniq temp.txt | perl -e 'undef $/; $_ = <>; open(OUT,">temp.txt"); print OUT;' 

Here, the perl part reads the complete output from uniq in variable $_ and then overwrites the original file with this data. You could do the same in the scripting language of your choice, perhaps even in Bash. But note that it will need enough memory to store the entire file, this is not advisable when working with large files.

like image 24
Bruno De Fraine Avatar answered Sep 28 '22 05:09

Bruno De Fraine