Skip to main content
added 565 characters in body
Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I am not sure I understand correctly since your description of the problem does not match your desired output, but if the different entries in your file are separated by consecutive newlines, you can use Perl and paragraph mode:

 $ perl -000ne 'print if /keyword/' file 
 Data engine-1
 Data engine-1
 Data
 Data
 Data engine-1 **keyword-1**
 Data engine-1

 Data engine-3
 Data engine-3 
 Data
 Data
 Data engine-3 **keyword-1**
 Data engine-3

The magic is the -000, this turns on Perl's paragraph mode which makes it split the files into paragraphs. In other words, records are separated by two or more consecutive \n characters. We then tell it to print if the current record contains the keyword.

You could also do this in gawk:

$ gawk 'BEGIN{RS=ORS="\n\n"}/keyword/' file

RS is the input record separator which we set to 2 consecutive new lines to correctly parse the file. ORS is the output record separator which also needs to be set to print a new line between each printed record.

I am not sure I understand correctly, but if the different entries in your file are separated by consecutive newlines, you can use Perl and paragraph mode:

 $ perl -000ne 'print if /keyword/' file 
 Data engine-1
 Data engine-1
 Data
 Data
 Data engine-1 **keyword-1**
 Data engine-1

 Data engine-3
 Data engine-3 
 Data
 Data
 Data engine-3 **keyword-1**
 Data engine-3

I am not sure I understand correctly since your description of the problem does not match your desired output, but if the different entries in your file are separated by consecutive newlines, you can use Perl and paragraph mode:

 $ perl -000ne 'print if /keyword/' file 
 Data engine-1
 Data engine-1
 Data
 Data
 Data engine-1 **keyword-1**
 Data engine-1

 Data engine-3
 Data engine-3 
 Data
 Data
 Data engine-3 **keyword-1**
 Data engine-3

The magic is the -000, this turns on Perl's paragraph mode which makes it split the files into paragraphs. In other words, records are separated by two or more consecutive \n characters. We then tell it to print if the current record contains the keyword.

You could also do this in gawk:

$ gawk 'BEGIN{RS=ORS="\n\n"}/keyword/' file

RS is the input record separator which we set to 2 consecutive new lines to correctly parse the file. ORS is the output record separator which also needs to be set to print a new line between each printed record.

Source Link
terdon
  • 252.2k
  • 69
  • 480
  • 718

I am not sure I understand correctly, but if the different entries in your file are separated by consecutive newlines, you can use Perl and paragraph mode:

 $ perl -000ne 'print if /keyword/' file 
 Data engine-1
 Data engine-1
 Data
 Data
 Data engine-1 **keyword-1**
 Data engine-1

 Data engine-3
 Data engine-3 
 Data
 Data
 Data engine-3 **keyword-1**
 Data engine-3