1

我编写了一个小脚本来搜索字符串并打印当前行。但是 m 有点困惑打印下一行。我对 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
4

3 回答 3

2

如果您的 grep 是 GNU:

 grep -A1 pattern file
于 2012-10-17T10:37:54.560 回答
0

我在这里用 bash 给你一个例子,这个例子考虑了一个目录中的一堆文本文件。您可以根据需要对其进行操作。

一个目录内

    grep "search string" *.txt

搜索或转到子目录

    find /full/path/to/dir -name "*.txt" -exec grep "search string" {} ;

希望这可以帮助你。

于 2012-10-17T10:39:25.770 回答
0

你可以使用 awk 来做到这一点:

awk '/Message/{print;getline;print}' your_file

以上是单个文件。此命令将显示匹配的模式行和文件中的下一行。

如果你想在目录结构中的所有文件中递归,那么:

find . -name -type f|xargs awk '/Message/{print;getline;print}'
于 2012-10-17T10:40:08.980 回答