grep 屏幕输出与文件的输出不同

grep 屏幕输出与文件的输出不同

使用 grep (以及 sed)时,我在终端中得到的结果与将输出定向到文件时得到的结果不同。我的起始文件是从 darknet yolo mAP 运行捕获的输出,如下所示:

     CUDNN_HALF=1 
net.optimized_memory = 0 
mini_batch = 1, batch = 32, time_steps = 1, train = 0 
Create CUDA-stream - 0 
 Create cudnn-handle 0 
nms_kind: greedynms (1), beta = 0.600000 
nms_kind: greedynms (1), beta = 0.600000 
nms_kind: greedynms (1), beta = 0.600000 

 seen 64, trained: 96 K-images (1 Kilo-batches_64) 

 calculation mAP (mean average precision)...
 Detection layer: 139 - type = 28 
 Detection layer: 150 - type = 28 
 Detection layer: 161 - type = 28 

 detections_count = 3688, unique_truth_count = 2114  
 rank = 0 of ranks = 3688 
 rank = 100 of ranks = 3688 
 rank = 200 of ranks = 3688 
.
.
.
 rank = 3400 of ranks = 3688 
 rank = 3500 of ranks = 3688 
 rank = 3600 of ranks = 3688 
class_id = 0, name = car, ap = 87.89%        (TP = 1414, FP = 130) 
class_id = 1, name = motorcycle, ap = 90.91%     (TP = 10, FP = 0) 
class_id = 2, name = truck, ap = 96.37%      (TP = 270, FP = 33) 
class_id = 3, name = gcp, ap = 96.08%        (TP = 305, FP = 13) 

 for conf_thresh = 0.25, precision = 0.92, recall = 0.95, F1-score = 0.93 
 for conf_thresh = 0.25, TP = 1999, FP = 176, FN = 115, average IoU = 83.92 % 

 IoU threshold = 75 %, used Area-Under-Curve for each unique Recall 
 mean average precision ([email protected]) = 0.902287, or 90.23 % 

Set -points flag:
 `-points 101` for MS COCO 
 `-points 11` for PascalVOC 2007 (uncomment `difficult` in voc.data) 
 `-points 0` (AUC) for ImageNet, PascalVOC 2010-2012, your custom dataset

我尝试了多种安排,当我尝试提取 class_id 结果行时,仍然得到相同的结果,例如

grep class_id results.txt > class_results.txt

这会在文件中生成以下文本:

 rank = 0 of ranks = 3688 
 rank = 100 of ranks = 3688 
 rank = 200 of ranks = 3688 
.
.
.
 rank = 3400 of ranks = 3688 
 rank = 3500 of ranks = 3688 
 rank = 3600 of ranks = 3688 
class_id = 0, name = car, ap = 87.89%        (TP = 1414, FP = 130) 
class_id = 1, name = motorcycle, ap = 90.91%     (TP = 10, FP = 0) 
class_id = 2, name = truck, ap = 96.37%      (TP = 270, FP = 33) 
class_id = 3, name = gcp, ap = 96.08%        (TP = 305, FP = 13) 

如果我忽略该> class_results.txt部分,输出看起来是正确的(或如我所期望和期望的那样):

class_id = 0, name = car, ap = 87.89%        (TP = 1414, FP = 130) 
class_id = 1, name = motorcycle, ap = 90.91%     (TP = 10, FP = 0) 
class_id = 2, name = truck, ap = 96.37%      (TP = 270, FP = 33) 
class_id = 3, name = gcp, ap = 96.08%        (TP = 305, FP = 13) 

我尝试使用 sed 删除包含“rank”的行,即sed '/rank/d' class_results.txt > class_results_concise.txt但它不仅删除包含“rank”的行,还删除了班级 ID = 0我想保留在输出中的行。我尝试过 sed 的就地编辑表单并得到同样的结果。

如何将包含 class_id 的行输出到文本文件?

这一切都在 Ubuntu 20.04 上进行。我猜我误解了有关标准输出等的一些内容。将非常感谢您的解决方案。提前谢谢了。


附加信息:在检查 ASCII 后,我发现包含单词“rank”的行前面有一个 CR (ASCII 13),而包含 class_id 的行前面有一个 LF (ASCII 10)。我怀疑我需要一种将 CR 转换为 LF 的方法???

答案1

感谢@Paul_Pedant 提供的 tr 提示。

在运行 grep 之前通过管道传输文件tr '\r' '\n'可以解决问题。看来我试图过滤的输出文本文件在某些​​地方有 CR,在其他地方有 LF。 grep 等程序似乎对这些内容进行了不同的处理。 Ed Morton 建议 sed ' s/\r$//' 一般来说是一个更好的解决方案,根据他的评论:

tr '\r' '\n' 会将输入中的所有 \rs 转换为 \ns,但 \rs 出现在输入中的其他位置是完全有效的,如果出现,那么 tr 会将输入行分开。对于行尾不需要的 \rs 的正确解决方案是简单地使用例如 sed 's/\r$//' 删除行尾的 \rs 。

相关内容