[SOLVED] sed print text between two patterns inclusive, unless a third pattern is present
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.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Unfortunately the first and last lines of the blocks containg the undesired pattern are still printed even if the main contents of those blocks are not. Thus instead of a blank line, I get lines like this when the undesired pattern is present:
I'd like to use sed to find blocks of text that don't have a certain pattern.
I'll rephrase the problem this way ...
Extract all blocks of text such that:
1) The block begins with "Start-Date".
2) The block ends with "End-Date".
3) The block does not contain "Install".
Is this correct?
Solutions will vary in complexity depending on how "messy" the input file may be. You didn't give a sample so consider this one ...
Code:
Chevrolet
Install
Start-Date: 2017-09-08 22:14:04
Should
print #1
End-Date: 2017-09-10 12:07:07
Install
Install
Start-Date: 2017-09-14 03:04:04
Should not
Install
print #99
End-Date: 2017-09-16 04:05:06
Honda
Start-Date: 2017-09-18 22:14:04
Should notprint
Start-Date: 2017-09-22 08:08:08
Should print #2
End-Date: 2017-09-24 09:09:09
Should not print
Please post the desired output resulting from this input file.
It's solved above in #5, but parsing the input in #7 should give this output :
Code:
Start-Date: 2017-09-08 22:14:04
Should
print #1
End-Date: 2017-09-10 12:07:07
Start-Date: 2017-09-18 22:14:04
Should notprint
Start-Date: 2017-09-22 08:08:08
Should print #2
End-Date: 2017-09-24 09:09:09
I'm not sure what to do about the Start-Date with no corresponding End-Date except skip over it. It would not naturally occur. They are always paired in the actual data.
I'm not sure what to do about the Start-Date with no corresponding End-Date except skip over it. It would not naturally occur. They are always paired in the actual data.
Okay, my contrived InFile was too messy. In my (limited) real-world experience the data is not always as it should be. That leads to trouble.
Yes, awk is easier for this. It started simple and grew. There is a sweet spot for each tool and in looks like this use-case is getting close to the crossover between sed and awk
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.