Is there way to do something like thas:

cat somefile.txt | sort | uniq > somefile.txt

I.e. I want to list entire file, then pipe some actions to its content and finally put result back to source file overwriting it completely. For now I doing it by putting output to temporary file and after all moving it over original. I want to do it such simple way like linux piping allows.

Shell is bash in Linux and cmd in Windows if it is important.


(answering for bash)
No. The shell processes redirections first, which then truncates the file. Only then does cat start, and it's operating with an empty file.

There is a tool called sponge in the moreutils package that lets you do this:

cat somefile.txt | sort | uniq | sponge somefile.txt

This command can be simplified (remove UUOC):

sort -u somefile.txt | sponge somefile.txt

Without sponge you have to write to a temp file, and if the command succeeds, overwrite the input file

sort -u somefile.txt > "$tmpfile" && mv "$tmpfile" somefile.txt
  • 1
    sponge is the general answer, but since sort (unlike most utilities) needs to buffer all the data before outputting any (except for -m), for this specific case you can instead sort -u -o somefile.txt somefile.txt. – dave_thompson_085 Apr 13 '16 at 3:56

You can use Vim in Ex mode:

ex -sc 'sort u|x' somefile.txt
  1. sort u sort unique

  2. x save and close

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.