Sed or Awk for lines between two strings multiple times and keep the last one

Hi,
I am trying to get lines between the last occurrences of two patterns. I have files that have several occurrences of �Standard� and �Visual�. I will like to get the lines between �Standard� and �Visual� but I only want to retain only the last one e.g.
Standard
Some words
Some words
Some words
Visual

Standard
Some words
Some words
Visual

Standard
Some words
Some words
Some words
Some words
Visual
Some words
Some words

I am trying to get the highlighted portion

Thank you so much
Dave

Standard
Some words
Some words
Some words
Visual
 
Standard
Some words
Some words
Visual
 
Standard
Some words
Some words
Some words
Some words
Visual
Some words
Some words

Please show an examples of file
original
desired output

1 Like
awk '/Standard/,/Visual/' inputfile | awk 'END{print $(NF-1)}' RS= FS="Standard|Visual"

If running on Solaris or SunOS plateform, just use nawk or /usr/xpg4/bin/awk instead of awk

1 Like
awk '
BEGIN {
  RS=""
  FS="\n"
}
$1=="Standard" && /Visual/ {
  s=$2
  for  (i=3 ; i<=NF ; ++i ) {
    if ( $i=="Visual" ) { break }
    s=sprintf("%s\n%s",s,$i)
  }
}
END {
  print s
}'
1 Like

Thank you Joeyg,
I just noticed that the highlighting did not work earlier. I successeed in finally highlighting the desired text.

---------- Post updated at 03:42 AM ---------- Previous update was at 03:16 AM ----------

Thank you ctsgnb, the code worked.

---------- Post updated at 03:43 AM ---------- Previous update was at 03:42 AM ----------

Thank you ctsgnb, the code worked.

---------- Post updated at 04:28 AM ---------- Previous update was at 03:43 AM ----------

Thank you chihung, I will check this code out.

---------- Post updated at 04:30 AM ---------- Previous update was at 04:28 AM ----------

Thank you chihung, I will check this code out.

---------- Post updated at 05:08 AM ---------- Previous update was at 04:30 AM ----------

Thank you chihung, I will check out this code.

---------- Post updated at 05:09 AM ---------- Previous update was at 05:08 AM ----------

Thank you chihung, I will check out this code.

---------- Post updated at 05:11 AM ---------- Previous update was at 05:09 AM ----------

Thank you chihung, I will check out this code.