LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-14-2019, 09:52 AM   #1
aungkhaingoo
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Rep: Reputation: Disabled
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

Last edited by aungkhaingoo; 03-14-2019 at 11:56 AM.
 
Old 03-14-2019, 01:58 PM   #2
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,718

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

Daniel B. Martin

.
 
Old 03-14-2019, 03:13 PM   #3
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,579

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
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.

Last edited by scasey; 03-14-2019 at 08:47 PM.
 
Old 03-14-2019, 08:41 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 17,625

Rep: Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777Reputation: 2777
Quote:
Originally Posted by aungkhaingoo View Post
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.
 
Old 03-15-2019, 12:44 AM   #5
aungkhaingoo
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by danielbmartin View Post
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
 
Old 03-15-2019, 01:01 AM   #6
aungkhaingoo
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
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.
 
Old 03-15-2019, 03:39 AM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,671

Rep: Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004Reputation: 3004
Look back at post #3
 
1 members found this post helpful.
Old 03-15-2019, 08:44 AM   #8
scasey
Senior Member
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.5
Posts: 2,579

Rep: Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878Reputation: 878
Quote:
Originally Posted by grail View Post
Look back at post #3
Yes...and now that I'm back on my desktop, you can just click here
 
Old 03-15-2019, 11:58 AM   #9
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,029

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
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!
 
3 members found this post helpful.
Old 03-15-2019, 02:45 PM   #10
aungkhaingoo
LQ Newbie
 
Registered: Mar 2019
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by MadeInGermany View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to capture 1000 lines before a string match and 1000 line a string match including line of string match ? sysmicuser Linux - Newbie 12 11-14-2017 05:21 AM
[SOLVED] SED/AWK - Delete all lines until empty line is found after pattern match vikas027 Programming 13 03-28-2012 08:33 AM
[SOLVED] sed/awk to print every before line that pattern match niharikaananth Linux - Newbie 10 02-22-2012 10:47 PM
[SOLVED] AWK: match multiple strings in the file, print 1 when match and 0 when not cristalp Programming 12 11-15-2011 10:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration