閱讀340 返回首頁    go 技術社區[雲棲]


用shell腳本合並多個文件內容

需求描述
現有多個具有相同命名格式及內容格式的文件,要求編寫shell腳本將它們合並到一個文件中。

被合並文件的命名格式為:YYYYMMDDHHMISS.r,例如:20161018030205.r;文件中包含了若幹行記錄,每行記錄包含26個字符,其中第一個字符為標識位,第7到12個字符為時間(格式:YYMMDD),例如:000000161019002925000003N0,該記錄的第一個字符0為標識位,第7到12個字符161019表示時間,即16年的10月19日;合並之後的文件的命名格式為:YYYYMMDD.txt,例如:20161018.txt。

對於合並操作,具體要求為:
1)當天隻合並前一天的文件,如今天(10月20日)隻合並昨天(10月19日)的文件,文件時間通過文件命名即可看出。

2)標識位為0的記錄會被寫到合並之後的文件中,其他記錄將被過濾掉。

3)時間(即第7到12個字符的值)為前一天的記錄會被寫到合並之後的文件中,其他記錄將被過濾掉。

shell腳本

#!/bin/bash

srcparh=/home/zhou/src
exportpath=/home/zhou/export
linenum=0

return_fail()
{
    exit 1
}

function check_config_dir
{
    if [ ! -d ${srcparh} ];then
        echo "[error]:${srcparh} has not existed!!"
        return_fail
    fi

    if [ ! -d ${exportpath}]; then
        echo "[error]:${exportpath} has not existed!!"
        return_fail
    fi
}

function merge_file
{
    ##YESTERDAY DATE YYMMDD
    YES_DATE_YY=`date -dyesterday +%y%m%d`

    ##YESTERDAY filename
    YES_FILENAME=`date -dyesterday +%Y%m%d`.txt

    ONE_DAY_AGO=`date -dyesterday +%y%m%d`

    echo"YESTERDAY:${ONE_DAY_AGO}"

    echo "`date+%Y-%m-%d` `date +%T`----begin to merge file"

    if [ -s ${YES_FILENAME}]; then
        echo "warn:yesterday file ${YES_FILENAME} has existed!! now backup it to${YES_FILENAME}_bak."
        mv ${YES_FILENAME}${YES_FILENAME}_bak
    fi

    cd ${srcparh}

    file_list_temp=`ls | grep-E "${ONE_DAY_AGO}"`
    file_list_count=`ls |grep -E "${ONE_DAY_AGO}" | wc -l`

    echo " "
    echo "there are${file_list_count} yesterday file(s) to be merged."
    echo " "

    >${exportpath}/${YES_FILENAME}

    for file_name in$file_list_temp
    do
         echo "now to merge ${file_name}"
         cat ${file_name} | grep "^0" >${file_name}_filter_firstline

        while read line
        do   
             echo ""
             echo "nowto deal this line: ${line}"     
             echo ""

             start_data=+${line:6:6}+

             echo"${start_data}" | grep "+${ONE_DAY_AGO}+"
             if [ $? -eq 0 ]
             then   
                 echo"${line}" >> ${exportpath}/${YES_FILENAME} 
                            linenum=$[linenum+1]
             fi            
        done <${file_name}_filter_firstline

        rm*_filter_firstline  
    done    

    if [ ${linenum} -gt 0 ]
    then
        echo "Totally ${linenum} lines havemerged."
    fi

    if [ ! -s${exportpath}/${YES_FILENAME}  ]
    then
        echo "warn:there is no yesterday file record!!,${exportpath}/${YES_FILENAME} isblank!"
        echo " ">${exportpath}/${YES_FILENAME}
    fi                   
}  

main()
{
    echo "  "

    echo "this mergetool begins running --------------------"

    check_config_dir;

    merge_file;

    echo"-------------end ---------------------"
}

## Execute main function
main $*

**腳本說明 **
第一,在腳本的第3到5行,定義了三個變量,其中srcparh用於存放被合並的文件,exportpath用於存放合並之後的文件,linenum用於表示本次寫到合並之後的文件中的記錄的條數。

第二,return_fail用於在執行出現異常(如srcparh或exportpath所表示的路徑不存在)時退出程序而不進行後續處理。

第三,check_config_dir函數用於檢查srcparh或exportpath所表示的路徑是否存在,如不存在,則不進行後續處理。

第四,merge_file函數是本腳本的核心,它的主要功能是找出srcparh下滿足時間條件的文件,並按照需求要求將文件中的記錄篩選出來,放到結果文件中。如果有滿足條件的記錄,那麼腳本會顯示寫入到結果文件中的記錄的條數。

第五,main函數是整個程序的入口(就像C語言中的main函數一樣),它調用了check_config_dir和merge_file函數。

**腳本執行結果 **
第一,當srcparh所表示的路徑不存在時,執行結果如下:

./file_merge_tool.sh
this merge tool begins running --------------------
[error]: /home/zhou/src has not existed!!

第二,當exportpath所表示的路徑不存在時,執行結果如下:

./file_merge_tool.sh
this merge tool begins running --------------------
[error]: /home/zhou/export has not existed!!

第三,當srcparh所表示的路徑存在但不包含任何文件時,執行結果如下:

./file_merge_tool.sh
this merge tool begins running --------------------
YESTERDAY:161019
2016-10-20 16:30:06----begin to merge file
there are 0 yesterday file(s) to be merged.
warn: there is no yesterday filerecord!!,/home/zhou/export/20161019.txt is blank!
-------------end ---------------------

第四,現有四個文件20161018030205.r、20161019030254.r、20161019182531.r、20161019213456.r,每個文件的內容如下:
20161018030205.r文件:

000000161019002925000003N0
000000161019002931000003N0
300000161018002931000003N0
000000161019002926000009Y0
000000161019003150000003N0

20161019030254.r文件:

000000161019004925000003N0
000000161019006931000003N0
100000161019006971000004N0
000000161019007926000009Y0
200000161019006871000004N0
000000161019008150000003N0

20161019182531.r文件:

000000161019001925000003N0
000000161019004931000003N0
000000161018007926000009Y0
000000161019007926000009Y0
000000161019009150000003N0
000000161017007926000009Y0
600000161019007426000009Y0

20161019213456.r文件:

000000161019002925000003N0
000000161019002931000003N0
000000161019002926000009Y0
800000161019002961000003N0
000000161019003150000003N0

將它們上傳到srcparh目錄下,運行腳本,結果如下:

> ./file_merge_tool.sh

this merge tool begins running --------------------
YESTERDAY:161019
2016-10-20 17:08:24----begin to merge file

there are 3 yesterday file(s) to be merged.

now to merge 20161019030254.r

now to deal this line: 000000161019004925000003N0

+161019+

now to deal this line: 000000161019006931000003N0

+161019+

now to deal this line: 000000161019007926000009Y0

+161019+

now to deal this line: 000000161019008150000003N0

+161019+
now to merge 20161019182531.r

now to deal this line: 000000161019001925000003N0

+161019+

now to deal this line: 000000161019004931000003N0

+161019+

now to deal this line: 000000161018007926000009Y0


now to deal this line: 000000161019007926000009Y0

+161019+

now to deal this line: 000000161019009150000003N0

+161019+

now to deal this line: 000000161017007926000009Y0

now to merge 20161019213456.r

now to deal this line: 000000161019002925000003N0

+161019+

now to deal this line: 000000161019002931000003N0

+161019+

now to deal this line: 000000161019002926000009Y0

+161019+

now to deal this line: 000000161019003150000003N0

+161019+
Totally 12 lines have merged.
-------------end ---------------------

對照被合並的文件和結果文件,一共有4個文件,但隻有3個文件(20161019030254.r、20161019182531.r、20161019213456.r)滿足時間條件,這3個文件中滿足過濾條件(標識位為0、時間為前一天)的記錄條數為12條,和腳本執行結果一致。

大家也可對本腳本進行更多的測試。

總結
shell腳本在基於Linux的開發中有極為廣泛的應用,因為它靠近底層,執行效率高、部署方便。本文中的腳本也可以作為定時任務部署到機器上,讓它在每天的同一個時間裏自動執行。

當然,要想編寫出功能強大的shell腳本,其前提條件是大家必須要對shell腳本的語法非常的熟悉,這也可以看出基本功的重要性。

最後更新:2017-09-28 09:03:10

  上一篇:go  阿裏雲服務器怎麼購買?詳細購買教程如下
  下一篇:go  eclipse中spring的Spring JdbcTemplate訪問access的簡易實現