我有两个文件。
测试日志
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/