从汇率网站
copy的原始格式数字保存在usdcny_From2007.data,共1521条记录
[root@] ~]# head -4 usdcny_From2007.data
Date Open High Low Close
Tue, Oct 30 2012 6.2447 6.2450 6.2446 6.2446
Mon, Oct 29 2012 6.2530 6.2662 6.2398 6.2446
Fri, Oct 26 2012 6.2437 6.2627 6.2437 6.2627
[root@] ~]#
[root@] ~]# cat usdcny_From2007.data | wc -l
1522
[root@] ~]# cat format.sh
- #!/bin/sh
- # 1 2 3 4 5_O 6_H 7_L 8_C
- # FROM ----- Mon, Jan 01 2007 7.8185 7.8185 7.8115 7.8185
- #
- # T O ----- 7.8115 2007_Jan_01____7.8115
- #
- awk -F' ' \
- '{
- if (NF == 8)
- {
- VAL=length($7);
- if(VAL == 1)
- print $7".0000 "$4"_"$2"_"$3"____"$7".0000";
- else if(VAL == 3)
- print $7"000 "$4"_"$2"_"$3"____"$7"000";
- else if(VAL == 4)
- print $7"00 "$4"_"$2"_"$3"____"$7"00";
- else if(VAL == 5)
- print $7"0 "$4"_"$2"_"$3"____"$7"0";
- else
- print $7" "$4"_"$2"_"$3"____"$7;
- }
- }' usdcny_From2007.data
[root@] ~]#
[root@] ~]# sh format.sh > usdcny_From2007_Lowest.data
[root@] ~]#
[root@] ~]# head -2 usdcny_From2007_Lowest.data
6.2446 2012_Oct_30____6.2446
6.2398 2012_Oct_29____6.2398
[root@] ~]#
[root@] ~]# tail -3 usdcny_From2007_Lowest.data
7.8135 2007_Jan_03____7.8135
7.8160 2007_Jan_02____7.8160
7.8115 2007_Jan_01____7.8115
[root@] ~]#
[root@] ~]# cat usdcny_From2007_Lowest.data | wc -l
1521
[root@] ~]# cat sort_by_lowest.sh
- #!/bin/sh
- CMP_FLAG=1
- SHOW_FLAG=0
- VAL=7.8115
- if [ $# -eq 2 ];then
- echo "input file is "$1" and the default compile val is "$2""
- VAL=$2
- echo VAL=$VAL
- else
- echo "USAGE : $0 FILE_NAME CMP_VAL"
- exit -1
- fi
- #
- # Other method of read files with reverse sequence
- #
- # sed -n '1!G;h;$p' urfile VS tac
- #
- for i in `tac $1`;
- do
- if [ $CMP_FLAG -eq 1 ];then
- CMP_FLAG=0;
- #
- # How to compare float data in BASH
- #
- # Method 1: echo 123.45 123.44 | awk '{if($1>$2){printf"%f greater then %f\n",$1,$2}else{printf"%f less then %f\n",$1,$2}}'
- # Method 2: or use command let: A=1;B=4;let "C=A+B";echo $C # but only works with integer data
- # Method 3: echo " 123.45 > 123.44" | bc
- #
- if [ `echo "$i < $VAL" | bc` -eq 1 ];then # For Lowest record
- VAL=$i
- SHOW_FLAG=1;
- fi
- else
- CMP_FLAG=1;
- if [ $SHOW_FLAG -eq 1 ];then
- SHOW_FLAG=0;
- #7.7260 2007_Mar_22____7.7260
- len=`expr length $VAL`;
- if [ $len -eq 1 ];then
- echo $VAL".0000 "$i".0000";
- elif [ $len -eq 3 ];then
- echo $VAL"000 "$i"000";
- elif [ $len -eq 4 ];then
- echo $VAL"00 "$i"00";
- elif [ $len -eq 5 ];then
- echo $VAL"0 "$i"0";
- else
- echo $VAL" "$i;
- fi
- fi
- fi
- done
[root@] ~]#
[root@] ~]# sh sort_by_lowest.sh usdcny_From2007_Lowest.data
USAGE : sort_by_lowest.sh FILE_NAME CMP_VAL
[root@] ~]#
[root@] ~]# time sh sort_by_lowest.sh usdcny_From2007_Lowest.data 7.8115 > usdcny_From2007_Lowest_new.data
real 0m10.247s
user 0m0.401s
sys 0m9.842s
[root@] ~]#
[root@] ~]# cat usdcny_From2007_Lowest_new.data
[root@] ~]#
[root@] ~]# cat usdcny_From2007_Lowest_new.data | wc -l
180
[root@] ~]# cat usdcny_From2007_Lowest_new.data | grep "2007" | wc -l
70
[root@] ~]# cat usdcny_From2007_Lowest_new.data | grep "2008" | wc -l
54
[root@] ~]# cat usdcny_From2007_Lowest_new.data | grep "2009" | wc -l
0
[root@] ~]# cat usdcny_From2007_Lowest_new.data | grep "2010" | wc -l
5
[root@] ~]# cat usdcny_From2007_Lowest_new.data | grep "2011" | wc -l
45
[root@] ~]# cat usdcny_From2007_Lowest_new.data | grep "2012" | wc -l
5
可见2007/2008/2011升值频率很高!
阅读(880) | 评论(0) | 转发(0) |