LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Match text using awk and print before and after until line break (https://www.linuxquestions.org/questions/programming-9/match-text-using-awk-and-print-before-and-after-until-line-break-4175650172/)

aungkhaingoo 03-14-2019 09:52 AM

Match text using awk and print before and after until line break
 
I am trying to print a blocking validation from simulation.txt. The matching string is "This is blocking validation." and i want to print two line before matching and print all line until blank break after match. What I am missing here?

===========================================
Miscellaneous Options.

This is a blocking validation.
===========================================
1 APPLE
2 BANANA
3 CAT
4 LEMON
5 LIME
6 LONDON
7 ORANGE
8 OAK
9 PENCIL
10 RED

This is not a blocking validation.
===========================================
Disable smilies in text

===========================================

Why is my mouse pointer wrong?

This is a blocking validation.
===========================================
1 spoil
2 dust
Code:

I am using this command but still need to print two line before match.

cat simulation.txt | awk '/This is a blocking validation./,/^$/'

    This is a blocking validation.
      ===========================================
      1    APPLE
      2    BANANA
      3    CAT
      4    LEMON
      5    LIME
      6    LONDON
      7    ORANGE
      8    OAK
      9    PENCIL
      10    RED

      This is a blocking validation.
      ===========================================
      1        spoil
      2        dust

Output | what i want is

Quote:

Miscellaneous Options.

This is a blocking validation.
===========================================
1 APPLE
2 BANANA
3 CAT
4 LEMON
5 LIME
6 LONDON
7 ORANGE
8 OAK
9 PENCIL
10 RED


Why is my mouse pointer wrong?

This is a blocking validation.
===========================================
1 spoil
2 dust

danielbmartin 03-14-2019 01:58 PM

I cannot see any difference between your problem statement and your output. Please review and correct if necessary.

Daniel B. Martin

.

scasey 03-14-2019 03:13 PM

Search for “awk print two lines before match” (without quotes). I think the answer was there, but don’t know awk well enough to be sure...
Please share the answer when you find it.

syg00 03-14-2019 08:41 PM

Quote:

Originally Posted by aungkhaingoo (Post 5973806)
i want to print two line before matching and print all line until blank break after match. What I am missing here?

You'll have to save records till you find the initial match. Best would be to only keep two, and rotate through, discarding the oldest until the match. Ugly/lazy would be to keep everything. Your choice.

aungkhaingoo 03-15-2019 12:44 AM

Quote:

Originally Posted by danielbmartin (Post 5973879)
I cannot see any difference between your problem statement and your output. Please review and correct if necessary.

Daniel B. Martin

.

My result is ==>

This is a blocking validation.
===========================================
1 APPLE
2 BANANA
etc

This is a blocking validation.
===========================================
1 spoil
etc

I also want to print also with Bold and underline text below ==>

Miscellaneous Options.

This is a blocking validation.
===========================================
1 APPLE
2 BANANA
3 CAT

Why is my mouse pointer wrong?

This is a blocking validation.
===========================================
1 spoil
2 dust

aungkhaingoo 03-15-2019 01:01 AM

Quote:

Originally Posted by syg00 (Post 5974001)
You'll have to save records till you find the initial match. Best would be to only keep two, and rotate through, discarding the oldest until the match. Ugly/lazy would be to keep everything. Your choice.

Can you show me what is still missing to save records. I am a very new user in linux environment.

grail 03-15-2019 03:39 AM

Look back at post #3

scasey 03-15-2019 08:44 AM

Quote:

Originally Posted by grail (Post 5974068)
Look back at post #3

Yes...and now that I'm back on my desktop, you can just click here

MadeInGermany 03-15-2019 11:58 AM

Here is what you initially requested.
A ring buffer holds the last lines.
When the /match/ is true; print the buffer, and keep printing until the NumberFields is 0.
Code:

awk '
  BEGIN { blen=2 }
  (n>=blen && /This is a blocking validation/) {
    for (i=bn; (i=(i+1)%blen)!=bn; ) print buf[i]
    print buf[i]
    pr=1
  }
  (pr)
  (NF==0) { pr=0 }
  { buf[bn=++n%blen]=$0 }
' file

Please add the decorations yourself!

aungkhaingoo 03-15-2019 02:45 PM

Quote:

Originally Posted by MadeInGermany (Post 5974194)
Here is what you initially requested.
A ring buffer holds the last lines.
When the /match/ is true; print the buffer, and keep printing until the NumberFields is 0.
Code:

awk '
  BEGIN { blen=2 }
  (n>=blen && /This is a blocking validation/) {
    for (i=bn; (i=(i+1)%blen)!=bn; ) print buf[i]
    print buf[i]
    pr=1
  }
  (pr)
  (NF==0) { pr=0 }
  { buf[bn=++n%blen]=$0 }
' file

Please add the decorations yourself!

WOW you save me sir. This is what I really wanted result. Thank you. :hattip:


All times are GMT -5. The time now is 07:55 PM.