gpt4 book ai didi

linux - 如何使用shell实时连续处理tail -f换行?

转载 作者:太空狗 更新时间:2023-10-29 11:49:05 24 4
gpt4 key购买 nike

在 Linux 服务器上有一个文件,新行不定期附加到它。我需要处理新行,解析它并使用其他命令或脚本来处理它。定期检查文件是 Not Acceptable ,我需要一个实时解决方案。服务器上没有其他语言(Python、Perl)可用,只有 shell。

现在,我试图将新行分配给一个 shell 变量,然后对其进行处理。但是找不到一个好的方法来做到这一点。另一个问题是我在处理新行时需要依赖一些结果。例如,当我处理第 11 行时,可能需要第 5 行结果。所以之前需要一些变量来存储结果,我需要在循环中使用它们。

对我的情况有什么解决方案或更好的建议吗?

最佳答案

试试下面的代码

#!/bin/bash
process_new_line() {
local var= '' #declare some local variables here
read
while true
do
#process $REPLY
#The new line content is in the variable $REPLY
#store the result in the local variable according to your rules.
read
done
}

tail -f the_file | process_new_line

使用函数将解决您的问题。您可以使用局部变量来存储结果,并且 $REPLY 保存新行的内容。

关于linux - 如何使用shell实时连续处理tail -f换行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48778732/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com