grep /search文件中的多行

假设我有一个长嵌套数组的文件,格式如下:

array( 'key1' => array( 'val1' => 'val', 'val2' => 'val', 'val3' => 'val', ), 'key2' => array( 'val1' => 'val', 'val2' => 'val', 'val3' => 'val', ), //etc... ); 

我想要做的是有一种方法来grep /search一个文件,并通过知道关键1,获取它包含的所有行(子数组)。 这可能吗?

不是用grep但你应该可以用awksed来做:

 sed -n '/key1/,/)/p' file.txt 

如果没有更多级别的嵌套数组,那么这应该工作:

 awk '/key1/,/\)/' my_input_file 

基本上,它从key1打印到下一个右括号

如果它是数组中固定数量的项目,则可以使用-A (切换后的行)和grep:

 grep -A4 'key1' myfile -A NUM, --after-context=NUM Print NUM lines of trailing context after matching lines. Places a line containing a group separator (--) between contiguous groups of matches. With the -o or --only-matching option, this has no effect and a warning is given. 

在线之前也有-B