#!/bin/bash

定义记录位置的变量

last_pos=0

循环读取文本

while true do # 从上次记录的位置开始读取 content=$(tail -n +$last_pos text.txt)

# 查找第一个'2J'所在的行数
line=$(echo '$content' | grep -n '2J' | head -n 1 | cut -d ':' -f 1)

# 如果找到了'2J',输出内容并退出循环
if [ -n '$line' ]
then
    echo '${content:0:$line}'
    break
fi

# 如果没有找到'2J',输出内容并继续读取
echo '$content'

# 更新记录的位置
last_pos=$(echo '$content' | wc -l)

done

Shell脚本:循环读取文本并获取特定行内容

原文地址: https://www.cveoy.top/t/topic/nVBn 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录