linux - Bash 在两个时间戳内合并文件(日志文件)

标签 linux bash awk sed grep

我有两个文件。

测试日志

sadfsdfdasfasdf 

2015-07-07 11:23:33,006 DEBUG : Invoking handleMessage on interceptor org.apache.cxf.interceptor.LoggingInInterceptor@2798240a

    name="Demo SG, St. Gallen/*"
    domicile="Ottikon"
    domicile="Zürich"
    domicile="Uster"
    name="Luca Gubler"

2015-07-07 15:00:33,008 DEBUG : Invoking handleMessage on interceptor org.apache.cxf.interceptor.

asdfasdfasdfdsgdg
adgasdasdgsdgasdsdg

2015-07-07 16:00:33,008 DEBUG : Invoking handleMessage on interceptor and replace.txt

和replace.txt

2015-07-07 11:23:33,006 DEBUG : Invoking handleMessage on interceptor org.apache.cxf.interceptor.LoggingInInterceptor@2798240a
name="name1"
domicile="domicile1"
domicile="domicile2"
domicile="domicile3"
name="name2"
2015-07-07 15:00:33,008 DEBUG : Invoking handleMessage on interceptor org.apache.cxf.interceptor.

所以我的问题是:如何将我的 replace.txt 文件合并到我的 test.log 文件中,以便它只替换 replace.txt 文件中两个时间戳之间的部分?我正在使用 bash。

谢谢

最佳答案

两种方式:

狂欢:

mapfile -t markers < <(sed -n '1p; $p' replace.txt)
print=true
while IFS= read -r line; do
    if [[ $line == "${markers[0]}" ]]; then
        print=false
        cat replace.txt
    fi
    $print && echo "$line"
    [[ $line == "${markers[1]}" ]] && print=true
done < test.log

GNU awk

gawk '
    NR==1 {start = $0; next} 
    NR==FNR {rep[++n] = $0; next} 
    ENDFILE {stop = $0} 
    $0 == start { 
        print; 
        while ($0 != stop) getline
        for (i=1; i<n; i++) print rep[i]
    }
    {print}
' replace.txt test.log

关于linux - Bash 在两个时间戳内合并文件(日志文件),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31514704/

相关文章:

linux - RuntimeError : Could not find firefox in your system PATH. 请指定 firefox 二进制位置或安装 firefox

bash - 从文件读取时如何在 while 循环内读取输入?

linux - 将 stderr 重定向到记录器,然后重定向到文件

awk - 通过搜索每行的所有列来提取某些模式,并将它们写入输出文件的指定列中

linux - 重定向 HTACCESS

linux - Linux下的访问权限

linux - 如何编写 awk 脚本来列出当前使用系统的用户以及他们登录的次数?

replace - Awk:用另一个文件中的一列替换一个文件中的列

linux - 如何在 sed 中将命令的结果作为字符串参数输入

linux - 退出 ssh -tt session