这是我拥有的文件的示例:
C; S; tous; PMJ100; 1; 40; 1
C; S; tous; PMJ100; 1; 40; 11
C; S; tous; PMJ100; 1; 40; 2
C; S; tous; PMJ100; 1; 60; 1
C; S; tous; PMJ200; 1; 40; 1
(其他一些文件有数千行!)
我想要的是最后一列要进行数字排序,例如1 2 ... 9 10 11 etcetc但它必须在6首列中保存原始的“古典”排序!所以结果应该是:
C; S; tous; PMJ100; 1; 40; 1
C; S; tous; PMJ100; 1; 40; 2
C; S; tous; PMJ100; 1; 40; 11
C; S; tous; PMJ100; 1; 60; 1
C; S; tous; PMJ200; 1; 40; 1
尽我所能,并没有在Google上找到任何有用的东西:(
(nb:空行仅用于在stackoverflow上正确显示)
提前感谢帮助的人!
最佳答案
因此,您可以使用以下脚本:
例如,将“结束”添加到您的数据文件以使用:
#!/bin/bash
# Attention!
if [ -f tosort.tmp ]
then
rm tosort.tmp
fi
if [ -f tosort_done.tmp ]
then
rm tosort_done.tmp
fi
if [ -f result.out ]
then
rm result.out
fi
# And a data file must contain a last "end" line.
nosort_previous_symb=""
while read line_data
do
tosort_symb=`echo $line_data | rev | cut -d";" -f1-1 | rev`
nosort_symb=`echo $line_data | rev | cut -d";" -f2- | rev`
if [[ "$nosort_previous_symb" == "" || "$nosort_previous_symb" == "$nosort_symb" ]]
then
echo $tosort_symb >> tosort.tmp
#refresh nosort_symb
nosort_previous_symb="$nosort_symb"
else
#old nosort_symb to out
cat tosort.tmp | sort -nk 1 > tosort_done.tmp
while read tosort_symb_done
do
echo ""$nosort_previous_symb";"$tosort_symb_done"" >> result.out
done < tosort_done.tmp
# new nosort
echo $tosort_symb > tosort.tmp
#refresh no sort
nosort_previous_symb="$nosort_symb"
fi
done < data
# Attention!
rm -f *.tmp
exit
结果:
C;S;tous;PMJ100;1;40;1
C;S;tous;PMJ100;1;40;11
C;S;tous;PMJ100;1;40;2
C;S;tous;PMJ100;1;60;1
C;S;tous;PMJ200;1;40;1
end