Linux   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了linux – 合并已排序的文件,具有最小的缓冲大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

我有两个以可排序的timesetamp为前缀的日志文件. 我希望按顺序看到它们,而生成日志文件的进程仍在运行.这是一个非常忠实的模拟情况: slow() { # print stdout at 30bps exec pv -qL 30 } timestamp() { # prefix stdin with a sortable timestamp exec tai6
我有两个以可排序的timesetamp为前缀的日志文件.
我希望按顺序看到它们,而生成日志文件的进程仍在运行.这是一个非常忠实的模拟情况:

slow() {
    # print stdout at 30bps
    exec pv -qL 30
}
timestamp() {
    # prefix stdin with a sortable timestamp
    exec tai64n
}

# Simulate two slowly-running batch jobs:
seq 000 099 | slow | timestamp > seq.1 &
seq1=$!
seq 100 199 | slow | timestamp > seq.2 &
seq2=$!

# I'd like to see the combined output of those two logs,in timestamp-sorted order
try1() {
    # this shows me the output as soon as it's available,# but it's badly interleaved and not necessarily in order
    tail -f seq.1 --pid=$seq1 &
    tail -f seq.2 --pid=$seq2 &
}
try2() {
    # this gives the correct output,# but outputs nothing till both jobs have stopped
    sort -sm <(tail -f seq.1 --pid=$seq1) <(tail -f seq.2 --pid=$seq2)
}


try2
wait@H_301_13@

解决方法

solution using tee(写入文件便标准输出仍然进入控制台)将无法工作,因为tee引入了不必要的延迟并且无法解决问题.同样,我无法使用tail -f -s 0.01(它将轮询改为100 / s)和/或某种类型的调用(如split -filter =’sort -sm’)来解决小批量的问题.

我也没有tai64n,所以我的测试代码实际上使用了这个功能相同的perl代码

tai64n() {
  perl -MTime::HiRes=time -pe '
    printf "\@4%015x%x%n",split(/\./,timE),$c; print 0 x(25-$C) . " "'
}@H_301_13@ 
 

在用sh和bash解决这个问题之后,我运用了标准的@R_675_10772@,perl:

slow() {
    # print stdout at 30bps
    pv -qL 30
}

tai64n_and_tee() {
  # prefix stdin with a sortable timestamp and copy to given file
  perl -MTime::HiRes=time -e '
    $_ = shift;
    open(TEE,"> $_") or die $!;
    while (<>) {
      $_ = sprintf("\@4%015x%x%n",$C) . 0 x(25-$C) . " $_";
      print TEE $_;
      print $_;
    }
  ' "$1"
}

# Simulate two slowly-running batch jobs:
seq 000 099 | slow | tai64n_and_tee seq.1 &
seq 100 199 | slow | tai64n_and_tee seq.2 &
wait@H_301_13@ 
 

这对我来说很方便,因为我已经使用perl作为时间戳.我没有做到这一点,perl充当tai64n和一个单独的perl调用充当tee,但它可能适用于真正的tai64n.

大佬总结

以上是大佬教程为你收集整理的linux – 合并已排序的文件,具有最小的缓冲全部内容,希望文章能够帮你解决linux – 合并已排序的文件,具有最小的缓冲所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。