这是我拥有的文件的一个示例:
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上正确显示)
请提前帮忙!
答案 0 :(得分:1)
因此您可以使用以下脚本:
#!/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
结果:
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
答案 1 :(得分:0)
如果我正确了解您。在bash中,您可以使用:
cat data | tr " " "\n" | sort -t ";" -nk 7
“数据”是包含您数据的文件。