Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a file in-place

When we use the sort file command, the file shows its contents in a sorted way. What if I don't want to get any output on stdout, but in the input file instead?

like image 583
Ali Sajid Avatar asked Mar 24 '15 22:03

Ali Sajid


People also ask

What is the command used to sort a file?

SORT command is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically. SORT command sorts the contents of a text file, line by line.

How do I sort a text file?

Although there's no straightforward way to sort a text file, we can achieve the same net result by doing the following: 1) Use the FileSystemObject to read the file into memory; 2) Sort the file alphabetically in memory; 3) Replace the existing contents of the file with the sorted data we have in memory.

How do I sort a file in bash?

Bash Sort Files Alphabetically By default, the ls command lists files in ascending order. To reverse the sorting order, pass the -r flag to the ls -l command, like this: ls -lr .

How do I do an in place sort?

In order to achieve an "in-place" sort, you can do this: This overwrites the input file with the sorted output. The -o switch, used to specify an output, is defined by POSIX, so should be available on all version of sort: -o Specify the name of an output file to be used instead of the standard output.

How do I sort a file in place in Linux?

To sort file in place, try: As explained in other answers, you cannot directly redirect the output back to the input file. But you can evaluate the sort command first and then redirect it back to the original file. In this way you can implement in-place sort.

How do I output the result of the sort command?

The sort command prints the result of the sorting operation to standard output by default. In order to achieve an "in-place" sort, you can do this: This overwrites the input file with the sorted output. The -o switch, used to specify an output, is defined by POSIX, so should be available on all version of sort:

How do I sort a list in descending order?

Descending (or decreasing) order is the opposite of ascending order - elements are arranged from highest to lowest value. To sort list items in descending order, you need to use the optional reverse parameter with the sort () method, and set its value to True. The general syntax to do this would look something like this:


7 Answers

You can use the -o, --output=FILE option of sort to indicate the same input and output file:

sort -o file file

Without repeating the filename (with bash brace expansion)

sort -o file{,}

⚠️ Important note: a common mistake is to try to redirect the output to the same input file (e.g. sort file > file). This does not work as the shell is making the redirections (not the sort(1) program) and the input file (as being the output also) will be erased just before giving the sort(1) program the opportunity of reading it.

like image 52
Sylvain Bugat Avatar answered Sep 30 '22 07:09

Sylvain Bugat


The sort command prints the result of the sorting operation to standard output by default. In order to achieve an "in-place" sort, you can do this:

sort -o file file

This overwrites the input file with the sorted output. The -o switch, used to specify an output, is defined by POSIX, so should be available on all version of sort:

-o Specify the name of an output file to be used instead of the standard output. This file can be the same as one of the input files.

If you are unfortunate enough to have a version of sort without the -o switch (Luis assures me that they exist), you can achieve an "in-place" edit in the standard way:

sort file > tmp && mv tmp file
like image 27
Tom Fenech Avatar answered Sep 28 '22 07:09

Tom Fenech


sort file | sponge file

This is in the following Fedora package:

moreutils : Additional unix utilities
Repo        : fedora
Matched from:
Filename    : /usr/bin/sponge
like image 35
SilverlightFox Avatar answered Oct 02 '22 07:10

SilverlightFox


Here's an approach which (ab)uses vim:

vim -c :sort -c :wq -E -s "${filename}"

The -c :sort -c :wq portion invokes commands to vim after the file opens. -E and -s are necessary so that vim executes in a "headless" mode which doesn't draw to the terminal.

This has almost no benefits over the sort -o "${filename}" "${filename}" approach except that it only takes the filename argument once.


This was useful for me to implement a formatter directive in a nanorc entry for .gitignore files. Here's what I used for that:

syntax "gitignore" "\.gitignore$"

formatter vim -c :sort -c :wq -E -s
like image 39
Anthony Sottile Avatar answered Sep 28 '22 07:09

Anthony Sottile


Do you want to sort all files in a folder and subfolder overriding them?

Use this:

find . -type f -exec sort {} -o {} \;
like image 30
luandrea Avatar answered Sep 30 '22 07:09

luandrea


To sort file in place, try:

echo "$(sort your_file)" > your_file

As explained in other answers, you cannot directly redirect the output back to the input file. But you can evaluate the sort command first and then redirect it back to the original file. In this way you can implement in-place sort.

Similarly, you can also apply this trick to other command like paste to implement row-wise appending.

like image 22
Bo Tian Avatar answered Oct 01 '22 07:10

Bo Tian


No answers about few files, so:

sort -u file1 file2 -o file1
like image 38
storenth Avatar answered Sep 30 '22 07:09

storenth