我目前正在编写一个 bash 脚本,该脚本将其执行的函数的输出作为日志文件返回。当在记事本中查看时,显示函数过程的文本占据了文件的大部分。理想情况下,我不想要所有这些批量,我只想显示进度文本的最后一个输出片段,显示该过程已成功执行。
output_log_18_01_2024.log - Notepad
+---------------------------------------------------------------------------+
Reading flash ............... 0x00000000 (0%)
Reading flash ............... 0x00001000 (0%)
Reading flash ............... 0x00002000 (0%)
Reading flash ............... 0x00003000 (0%)
Reading flash ............... 0x00004000 (0%)
Reading flash ............... 0x00005000 (0%)
Reading flash ............... 0x00006000 (0%)
Reading flash ............... 0x00007000 (0%)
Reading flash ............... 0x00008000 (0%)
Reading flash ............... 0x00009000 (0%)
Reading flash ............... 0x0000A000 (0%)
Reading flash ............... 0x0000B000 (0%)
Reading flash ............... 0x0000C000 (0%)
Reading flash ............... 0x0000D000 (0%)
Reading flash ............... 0x0000E000 (0%)
Reading flash ............... 0x0000F000 (0%)
Reading flash ............... 0x00010000 (0%)
Reading flash ............... 0x00011000 (0%)
Reading flash ............... 0x00012000 (0%)
Reading flash ............... 0x00013000 (0%)
...
在命令行上的 nano 或 vi 中查看时,此过程文本显示为由回车符 ^M 分隔的一行。
GNU nano 5.6.1 output_log_18_01_2024.log
+---------------------------------------------------------------------------+
Reading flash ............... 0x00000000 (0%) ^M Reading flash ............... 0x00001000 (0%) ^M Reading flash ............... 0x00002000 (0%) ^M Reading flash ............... 0x00003000 (0%) ^M Reading flash ............... 0x00004000 (0%) etc...
因此,我尝试使用 python 脚本来检测这些回车符并相应地处理它们。这是我尝试过的脚本:
import sys, re
f = open(sys.argv[1], "r")
data = f.read()
counter = 0
for char in data:
print(char, end="")
if char == r'\r':
counter += 1
print(counter)
# always returns 0
rx = r'\r'
rr = re.search(rx, data)
print(rr)
# always returns None
f.close()
为什么python看不到这些回车符?是否只是将它们解释为新线而我无能为力?我愿意接受 python 或 bash 中的任何支持,基本上任何能让我删除大部分日志并保留重要文本的支持。
编辑:删除错误图像,替换为文本副本。
这是一个最终对我有用的答案。可能有一种更有效的方法来做到这一点。但这是脚本的一部分已经非常非常慢了。
# formatter.py
#!/usr/bin/python3
import sys
### Split at newlines.
testlist = repr(sys.argv[1]).split(r'\n')
for i in range(0, len(testlist)):
line = testlist[i]
if line[1] == " ":
line = line.replace(" ","",1)
### If a return carriage is found in line
if (line.find(r'\r') > -1):
old_idx = 0
curr_idx = line.find(r'\r', old_idx)
### Find last occurence of \r
while curr_idx != -1:
old_idx = curr_idx
curr_idx = line.find(r'\r', (old_idx+1))
### Return line from position of penultimate carriage return to end of line.
print(line[old_idx+2:].replace(r'\r',""))
### If no carriage returns are found, return script as is.
else:
print(line)
# echoline.sh
#!/bin/bash
echoLine()
{
outfile="test.txt"
# get input
set -f # stops * being expanded
if test ! -t 0 # pipe
then
indata=$(</dev/stdin)
elif test -n "$1"
then
indata="$@"
fi
indata="${indata//^M/'\r'}"
IFS='$'; arrIN=($indata); unset IFS;
for (( x=0; x<${#arrIN[@]}; x++))
do
outdata=$(python3 soanswer.py "${arrIN[x]}" 2>/dev/null || python3 soanswer.py "${arrIN[x]: -300}")
outdata="${outdata//"'"/""} \n"
echo -ne "$outdata" >> $outfile
echo -ne "$outdata"
done
}
echoLine -s $(cat -e output_log_18_01_2024_old.log)