1. 程式人生 > >python和shell處理資料的程式整理

python和shell處理資料的程式整理

1把檔案按行拼接(shell腳步)
檔案f1
1
2
3


filename=$1
abc=`cat $filename | awk '
BEGIN{count=1;str=""}
{
    if(count==1)
        {
            count++
        }
    else if(count<NR)
        {
            str=sprintf("%s,%s",str,$1)
            count++
        }
    else
        {
            str+=$1
        }


}
END{print str}'`
變成 1,2,3


2.產生時間點前或者後幾天的日期(python指令碼和shell指令碼)
2.1 python指令碼
先把字串變成日期型別(strptime),在進行運算,運算結果變成字串(strftime)
#!/use/bin/python
#coding:UTF-8
import datetime


def pro_date(appointed_date,forward_num): #2016-10-12
    date_list = []
    forward_num = int(forward_num)
    for num in range(1,forward_num+1):
        forward_day = datetime.timedelta(days=num)
        this_day_time = datetime.datetime.strptime(appointed_date,"%Y-%m-%d") - forward_day
        this_day = this_day_time.strftime("%Y-%m-%d")
        print this_day
        date_list.append(this_day)
    return date_list


if __name__ == '__main__':
pro_date("2018-01-01",5)


2.2 shell指令碼
#/bin/bash
function pro_date()
{
appointed_date=$1
forward_num=$2


for((i=1;i<=$forward_num;i++))
do
    date=$(date -d "$i day ago  $appointed_date" +%Y-%m-%d  )
    echo $date
done
}
pro_date "2018-01-01" 5


3.產生兩個時間點內的日期(python指令碼和shell指令碼)
3.1python指令碼
#!/use/bin/python
#coding:UTF-8
import datetime


def pro_date(begin_day,end_day): #2018-01-01
    date_list = []
    begin_day_datetime = datetime.datetime.strptime(begin_day,"%Y-%m-%d")
    end_day_datetime = datetime.datetime.strptime(end_day,"%Y-%m-%d")
    diff = end_day_datetime - begin_day_datetime
    forward_num = diff.days


    for num in range(1,forward_num+1):
        forward_day = datetime.timedelta(days=num)
        this_day_time = datetime.datetime.strptime(end_day,"%Y-%m-%d") - forward_day
        this_day = this_day_time.strftime("%Y-%m-%d")
        print this_day
        date_list.append(this_day)
    return date_list




if __name__ == '__main__':
    pro_date("2017-01-01","2018-01-01")


3.2  shell指令碼
function pro_date ()
{
begin_day=$1
end_day=$2
time1=$(($(date +%s -d $end_day)-$(date +%s -d $begin_day)))
forward_num=$(($time1/3600/24))
echo $forward_num
for((i=1;i<=$forward_num;i++))
do
    date=$(date -d "$i day ago  $end_day" +%Y-%m-%d  )
    echo $date
done


}


pro_date "2017-01-01" "2018-01-01"