[SOLVED] Match text using awk and print before and after until line break
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.