Mah*_*leh 2 unix sorting shell io-redirection
我想在两个或多个文本文件上应用一些操作(交集和重聚),所以在我将所有行放在一起后,我必须在该文件上应用排序和 uniq。问题是我想要相同的输入和输出文件(-i 不起作用)。这是我的代码:
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;;
Run Code Online (Sandbox Code Playgroud)
你能帮我做同样的事情而不使用额外的文件吗?如果 $2 是“intersectie.txt”,则 grep 也是如此。
编辑 -
我在半睡半醒时写在下面,;-) 并且有一个很好的捷径可以解决您的问题
sort -u -o file file
Run Code Online (Sandbox Code Playgroud)
该-u选项使排序后的数据变得 uniq,如下所述,-o file将输出保存到您要命名的任何文件中,包括与输入相同的名称。
如果你想做类似的事情
sort < file | uniq -c > uniqFileWithCounts
Run Code Online (Sandbox Code Playgroud)
那么第一个想法对你没有帮助。
不要自欺欺人,即使您使用sort -o file file相同的文件名进行排序-o(输出),在幕后系统必须将所有数据写入 tmp 文件,然后重命名为由-o file(Also sort is将中间排序数据写入/tmp目录并在最终输出完成时将其删除)。
所以你最好的选择是
sort <reuniune1.txt | uniq > uniqd.txt && mv uniqd.txt reuniune1.txt
Run Code Online (Sandbox Code Playgroud)
如果sort | uniq进程没有错误退出,这只会覆盖 reuniune1.txt 。
IHTH
| 归档时间: |
|
| 查看次数: |
4462 次 |
| 最近记录: |