2015-07-20 74 views
1

我有两个文件。Bash合并文件wihtin两个时间戳(日志文件)

test.log中

sadfsdfdasfasdf 

2015-07-07 11:23:33,006 DEBUG : Invoking handleMessage on interceptor [email protected] 

    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 [email protected] 
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合作。

感谢

+0

不replace.txt只有时间戳的一个段/范围(ifso为什么不直接使用文本编辑 - 他们是相当便宜;) – amdixon

+0

WOW,我不知道你能使用一个texteditor XD。但我需要这个逻辑来处理我的脚本。该脚本循环遍历目录中的所有日志文件。我想传递一个时间戳作为参数,以便该脚本仅从09:00到10:00取代,例如 – NewJavaPythonDev

+0

因此它是1.您对一个replace.txt运行的多个test.log文件,2。对每个test.log文件使用multilple replace.txt或者3.完全不同的东西 – amdixon

回答

1

我发现了另一个堆栈溢出后我的脚本解决

sed -i -ne '/2015-07-07 11:23:33,006/ {p; r replaced_time_search.txt' -e ':a; n; /2015-07-07 15:00:33,008/ {p; b}; ba}; p' logs/new_test.log 

感谢您的帮助

2

2种方式:

庆典:

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 
相关问题