Shell脚本:合并日期范围内的文件

时间:2016-04-19 23:12:19

标签: linux bash shell date unix

我想在给定的日期范围内合并多个日志文件。例如,我在目录中有5天的日志文件:

server.log.2016-04-14-00 
server.log.2016-04-14-01
. . .
server.log.2016-04-18-23
server.log.2016-04-19-00
server.log.2016-04-19-01

我知道我可以使用cat来合并文件,但是如何在shell脚本中编码,以便只选择2016-04-17-22和2016-04-18-01之间的文件?

1 个答案:

答案 0 :(得分:1)

以下脚本接受服务器的日志文件作为其第一个参数。 两个重要变量是from_dateto_date,它们控制 from-to 范围。它们在脚本中是硬编码的,您可能希望更改它以增强脚本的使用灵活性。

#!/bin/bash

# Server's log file.
server_log_file=$1
# The date from which the relevant part of the log file should be printed.
from_date='2016/04/14 00:00'
# The date until which the relevant part of the log file should be printed.
to_date='2016/04/19 01:00'

# Uses 'date' to convert a date to seconds since epoch.
# Arguments: $1 - A date acceptable by the 'date' command. e.g. 2016/04/14 23:00
date_to_epoch_sec() { 
    local d=$1
    printf '%s' "$(date --date="$d" '+%s')"
}

# Convert 'from' and 'to' dates to seconds since epoch.
from_date_sec=$(date_to_epoch_sec "$from_date")
to_date_sec=$(date_to_epoch_sec "$to_date" )

# Iterate over log file entries.
while IFS=. read -r s l date; do
    # Read and parse the date part.
    IFS=- read -r y m d h <<< "$date"
    # Convert the date part to seconds since epoch.
    date_sec=$(date_to_epoch_sec "$y/$m/$d $h:00")

    # If current date is within range, print the enire line as it was originally read.
    if (( date_sec > from_date_sec && date_sec < to_date_sec )); then
        printf '%s.%s.%s\n' "$s" "$l" "$date"
    fi

done < "$server_log_file"

为了测试它,我创建了以下文件,名为 logfile

server.log.2016-04-14-00
server.log.2016-04-14-01
server.log.2016-04-18-23
server.log.2016-04-19-00
server.log.2016-04-19-01

用法示例(脚本名称为 sof ):

$ # Should print logs from 2016/04/14 00:00 to 2016/04/19 01:00 
$ ./sof logfile 
server.log.2016-04-14-01
server.log.2016-04-18-23
server.log.2016-04-19-00
相关问题