0

我从目录中 grep 模式和该模式之前的 4 行,我需要从每个结果中进一步 grep 顶行,但不知道如何做。请就此提出建议。

用示例解释的问题:

在目录'direktory'中有多个不同名称的文件,如20130611和2013400等。我感兴趣的文件中写入的数据是这样的:

[
My name is 
.....
......
......
Name has been written above
]

现在在每个实例中,“名称已写在上面”都以行为单位写入,但值不断变化,而不是“我的名字是”,所以我想从每次出现时 grep 这条特定的行。

请提出一些方法来获得结果。

提前致谢。

4

2 回答 2

0

尝试类似的东西

for file in $(ls <wherever>)
do

    # Tell the user which file we're looking at

    echo ""
    echo $file
    echo ""

    # Output the first line of the file

    head -1 $file

    # Output the line continaing <pattern> and the four
    # preceding lines

    <your grep command here>
done
于 2013-06-18T15:13:37.863 回答
0
a@x:/tmp$ cat namefile
[
My name is 
.....
......
......
Name has been written above
]
a@x:/tmp$ cat namefile | grep -B 4 "Name has been written above" | head -1
My name is 

其中“4”可以替换为 N 即目标数据位于 grepped 行上方的行数

于 2013-06-18T12:53:12.363 回答