gpt4 book ai didi

python - shell脚本查找字符串并递归打印当前行和下一行

转载 作者:行者123 更新时间:2023-12-01 05:55:10 26 4
gpt4 key购买 nike

我编写了一个小脚本,用于搜索字符串并打印当前行。但我对打印下一行有点困惑。我对 bash/perl/python 没问题

#!/bin/bash  

CURRENT_DIR=`pwd`
cnt=0

for dir in $(find $CURRENT_DIR -type d)
do
for myFile in $dir/*
do
if [ -f "$myFile" ]; then
cat $myFile | while myLine=`line`
do
allFile="$myLine"
if echo "$myLine" | grep -q $1 ; then
echo "$myFile" "$allFile" ""
fi
#echo 'expr $count+1'
#echo "$allFile" ""
done #LINE
fi
done #FILE
done # DIRECTORY

最佳答案

如果您的 grep 是 GNU:

 grep -A1 pattern file

关于python - shell脚本查找字符串并递归打印当前行和下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12932298/

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