Reading lines between two keywords

Can someone tell me what to do if I need to read between two keywords, for example

*System_Power 1 1.2 1.8 2 *System_Terminate 

In this case, asnwer will be

 1 1.2 1.8 2 

I tried using awk as

 awk '$0 =="*System_Power" # start printing when 1st field is *System_Power $0 != "*System_Terminate"{ # until reach the *System_Terminate print; } ' powerSummary > reportedFailure.k # Read from file powerSummary and pipe to reportedFailure.k exit 

where the above data is somewhere in the middle of the powerSummary file.

I would be grateful for your corrections.

+6
source share
4 answers

This will work for you:

 $ awk '/\*System_Power/{f=1;next}/\*System_Terminate/{f=0}f' infile 1 1.2 1.8 2 
+5
source

You can use sed to trivially select text between two matching lines, but this will also include the corresponding lines ... so we will explicitly delete them:

 sed -n '/System_Power/,/System_Terminate/ { /^\*/ d p }' < input 

Awk can also select text between the corresponding lines:

 awk '/System_Power/,/System_Terminate/ {print}' < input 

... but, like sed , this will include the terminators of the beginning and the end. You can fix this with awk , but I think you will get a cleaner solution with sed .

+2
source

Here's another one:

 awk '$1=="*System_Power",$1=="*System_Terminate" {if (!/System*/) print}' in.txt 
+1
source

This might work for you:

 sed '/^\*System_Power/,/^\*System_Terminate/!d;//d' file 

or that:

 awk '/^\*System_Terminate/{p=0};p;/^\*System_Power/{p=1}' file 
+1
source

Source: https://habr.com/ru/post/908643/


All Articles