2

I have a text file with multiple log data, I would like to search for 'Keyword-1' in a line, and when it finds the line, to search within that line for another keyword such as 'engine-1'. Then extract the section from the start of 'engine-1' until it reaches a different engine such as 'engine-2'. I would like all 'engine-1' section extracted, that includes all lines between the start and end of 'engine-1'. The end of 'engine-1' is defined by reaching any other number such as 'engine-2' or 'engine-xx'.

Note, not all lines contain the word engine, but I still want to extract the 'Data' between 'engine-1' start and 'engine-1' end.

Then moved on to the next section in the file and repeat the process by searching for keyword-1 first and finding out engine it uses, and extracted the start/end section of the new different engine. The link between all sections would be 'keyword-1'

The text file looks like this:

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


 Data engine-2
 Data engine-2
 Data
 Data
 Data engine-2
 Data engine-2

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

 Data engine-1
 Data engine-1 
 Data
 Data
 Data engine-1

Result should look like this after extraction:

 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
2
  • Is there always a blank line between different engines? Dec 20, 2013 at 23:33
  • Hi Gilles, no, blank lines are not always present. However, engine data is usually in clusters, but to complicate matters, some clusters do not have the word engine in them but the Data is indented xml style. Dec 23, 2013 at 17:58

1 Answer 1

4

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.

1
  • Hi terdon, I apologize for not describing the issue clearly. I tried to used the gawk statement, and it did not pull all the data associated engine for the keyword that I searched. Perhaps I can clarify more of the steps that are involved: 1) find the keyword I specify, 2) find what engine number it is using, 3)extract all the lines immediately above and below that are have that engine number, however, some lines have Data with no engine tag (no word of engine in the line and can have variable number of lines .. until the word engine appears again) and still need to be extracted.4)repeat 1-3 Dec 23, 2013 at 18:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .