天天看點

用shell腳本合并多個檔案内容

用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=

return_fail()
{
    exit 
}

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  ]
             then   
                 echo"${line}" >> ${exportpath}/${YES_FILENAME} 
                            linenum=$[linenum+]
             fi            
        done <${file_name}_filter_firstline

        rm*_filter_firstline  
    done    

    if [ ${linenum} -gt  ]
    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 $*
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105

腳本說明 

第一,在腳本的第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!!
           
  • 1
  • 2
  • 3
  • 4

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

> ./file_merge_tool.sh

this merge tool begins running --------------------
[error]: /home/zhou/export has not existed!!
           
  • 1
  • 2
  • 3
  • 4

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

> ./file_merge_tool.sh

this merge tool begins running --------------------
YESTERDAY:
-- ::----begin to merge file

there are  yesterday file(s) to be merged.

warn: there is no yesterday filerecord!!,/home/zhou/export/txt is blank!
-------------end ---------------------
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

第四,現有四個檔案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:
-- ::----begin to merge file

there are  yesterday file(s) to be merged.

now to merge r

now to deal this line: N0

++

now to deal this line: N0

++

now to deal this line: Y0

++

now to deal this line: N0

++
now to merge r

now to deal this line: N0

++

now to deal this line: N0

++

now to deal this line: Y0


now to deal this line: Y0

++

now to deal this line: N0

++

now to deal this line: Y0

now to merge r

now to deal this line: N0

++

now to deal this line: N0

++

now to deal this line: Y0

++

now to deal this line: N0

++
Totally  lines have merged.
-------------end ---------------------
           
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67

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

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

總結 

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

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

繼續閱讀