菜鸟笔记
提升您的技术认知

用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脚本的语法非常的熟悉,这也可以看出基本功的重要性。