1

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.

2 Answers 2

2

(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

tmpfile=$(mktemp)
sort -u somefile.txt > "$tmpfile" && mv "$tmpfile" somefile.txt
1
  • 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. Apr 13, 2016 at 3:56
0

You can use Vim in Ex mode:

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

  2. x save and close

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .