Reputation: 117
I want to apply some operation(intersection and reunion) on two or more text files so after I put all rows together I have to apply sort and uniq on that file. The problem is that I want to have the same input and output file (-i doesn't work). Here is my code:
nr_param=$#
case $1 in
'-r')
if [ "$nr_param" = 3 ]
then
echo "reuniunea"
rm -f reuniune1.txt
cat $2 $3 >> reuniune1.txt
sort <reuniune1.txt | uniq >reuniune.txt
rm -f reuniune1.txt
else
echo "parametri incorecti"
fi;;
'-i')
if [ "$nr_param" = 3 ]
then
echo "intersectia"
rm -f intersectie.txt
grep -Fx -f $2 $3 > intersectie.txt
else
echo "parametri incorecti"
fi;;
Could you help me do the same thing without using an extra file? The same for grep if $2 would be "intersectie.txt".
Upvotes: 2
Views: 5986
Reputation: 37288
Edit --
I wrote below when I was half asleep, ;-) and there's a great shortcut for your case
sort -u -o file file
The -u
option makes the sorted data uniq, and as mentioned below, -o file
will save the output to any file your care to name, including the same name as the input.
If you want to do something like
sort < file | uniq -c > uniqFileWithCounts
Then the first idea won't help you.
Don't kid yourself, even when you use sort -o file file
to reuse the same filename for the sorted -o
(utput), behind the scenes the system has to write all of the data to a tmp file and then rename to the file specified by -o file
(Also sort is writing intermediate sort data to the /tmp
dir and deletes that when the final output is complete).
So you're best bet is something like
sort <reuniune1.txt | uniq > uniqd.txt && mv uniqd.txt reuniune1.txt
This will only overwrite reuniune1.txt if the sort | uniq
process exits without error.
IHTH
Upvotes: 6