awk/sed/perl command to delete specific pattern and content above it...

Hi,

Below is my input file:

Data: 1
Length: 20



Got result.

Data: 2
Length: 30



No result.

Data: 3
Length: 20



Got result.

Data: 4
Length: 20



Got result.

Data: 5.5
Length: 20



Got result.


Desired output:

Data: 1
Length: 20



Got result.


Data: 3
Length: 20



Got result.

Data: 4
Length: 20



Got result.

Data: 5.5
Length: 20



Got result.



My purpose just want to delete based on specific pattern "No result." and file line before the pattern "No result."
I'm appreciate for any suggestion.
Thanks :slight_smile:

Hi

awk '/result/{ if ($0 ~ /Got/){for(j=1;j<=i;j++)print a[j];print;}i=1;next;}{a[i++]=$0}' i=1 infile

But, it looks like the above sol misses some blank lines.

Guru.

awk -vRS="." '!/No result/' file

Hi,

Instead of based on "Got" pattern to extract, do you got any idea that allow me to delete my content based on "No result."?
The reason I hope can delete the line based on "No result." is because some of the "Got result" maybe got more line content.
Thanks again for any of your advice :slight_smile:

---------- Post updated at 02:30 AM ---------- Previous update was at 02:27 AM ----------

Hi Bartus11,

I just try your command.
But it seems like it will affect some of the content of "Got result" due to the exist of "." in some content of "Got result".
Do you got any other suggestion to archive my goal?
Thanks first :slight_smile:

My suggestion is that you provide sample of the real input file.

Hi, bartus11.
I just include those content that might affect by the command you suggested.
Thanks a lot for your advice :slight_smile:

Try

awk -vRS="D" -vORS="D" '!/No result/' file
1 Like
# sed '/Got result./{n;N;N;N;N;N;N;h; /No result./{;x;$!d;}} ' infile
Data: 1
Length: 20
 
 
 
Got result.
 
 
Data: 3
Length: 20
 
 
 
Got result.
 
Data: 4
Length: 20
 
 
 
Got result.
 
Data: 5.5
Length: 20
 
 
 
Got result.