Shell one-liner to add a line to a sorted file

I want to add a line to a text file so that the result is sorted where the text file was originally sorted. For instance:

cp file tmp; echo "new line" >> tmp; sort tmp > file; rm -f tmp

I would REALLY want to do this without a temporary file and without semicolons (use pipes instead?); use sedwould be acceptable. Is this possible, and if so, how?

+3
source share
5 answers
echo "New Line" | sort -o file - file

-o file ( ). - " ", . file ' file'. Unix- ( ) 7- UNIX ™ 1978 , , . .

, "", , , , , :

echo "New Line" | sort -o file -m - file

.

+7

, - :

$ echo "something" >> file; sort file -o file
+7

, , comm ( ), , ( ).

: comm -3 file <(echo "new line") |tr -d '\t'

"": sort -m file <(echo "new line")

+1

, , , , (, awk perl).

?

: , :

val=$(cat file); { echo "$val"; echo "new line"; } | sort > file
0

All Articles