Type file content, pipe some actions and put result back

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.

Alex G.P.

Posted 2014-12-16T15:37:35.620

Reputation: 113

Answers

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

glenn jackman

Posted 2014-12-16T15:37:35.620

Reputation: 18 546

1sponge 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 – 2016-04-13T03:56:48.130

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

Steven Penny

Posted 2014-12-16T15:37:35.620

Reputation: 7 294