Saturday, May 4, 2024
18
rated 0 times [  18] [ 0]  / answers: 1 / hits: 17015  / 1 Year ago, fri, january 13, 2023, 7:03:18

Is there any short way to save the pipe output to the same file that it's processed. For example, this is what I am actually doing



$ cat filename | sort | uniq > result
$ rm -f filename
$ mv result filename


I was wondering if there was a way to do it in just one line (not appending those commands using &&)



This is not the way, but to get an idea



$ cat filename | sort | uniq > filename

More From » command-line

 Answers
3

You can use sponge from moreutils package:



LC_ALL=C sort -u filename | sponge filename


You also don't need pipe to uniq, since when sort has -u option to unique lines when sorting.



Note that on GNU system with UTF-8 locales, sort -u or sort | uniq didn't give you unique lines, but the first from sequence of lines which sort the same in current locale.



$ printf '%b
' 'U2460' 'U2461' | LC_ALL=en_US.utf8 sort | LC_ALL=en_US.utf8 uniq



gave you only . Changing locale to C force the sorting order based on the byte values:



$ export LC_ALL=C
$ printf '%b
' 'U2460' 'U2461' | LC_ALL=C sort | LC_ALL=C uniq



[#23067] Saturday, January 14, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
arbaour

Total Points: 346
Total Questions: 115
Total Answers: 105

Location: Oman
Member since Wed, Apr 12, 2023
1 Year ago
;