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.
I need to search for a string (string1) in a file, but that sting has a few lines that need to go with it. The problem is that the number of lines vary. so I need to search for word in a file and find all of the other connecting lines with it. See below for example
(empty line)
/tmp
bad line
bad line
bad line
bad line
(empty line)
/tmp
good line
good line
good line
String1
good line
good line
good line
(empty line)
/tmp
bad line
bad line
bad line
bad line
(empty line)
now I want to search for string1 and get all of the good lines that are with it. All good lines start with "tmp" and it always ends in an empty line. So I need ONLY everything in between, and including, the "tmp" and the (empty line) that is associated with "string1"
I know sed can do this very easily I just am not sure of the syntax.
Any help would be great.
Thanks
Last edited by edwardcode; 04-16-2013 at 11:04 AM.
If I understand correctly, you want to get all the text between /tmp and [empty line] if a certain string is contained between the two?
I'm a base beginner at sed too, but perhaps this would work-
bad line 1
bad line 2
bad line 3
bad line 4
/tmp
good line 1
good line 2
good line 3
String1
good line 4
good line 5
good line 6
bad line 5
bad line 6
bad line 7
bad line 1
bad line 2
bad line 3
bad line 4
/tmp
good line 11
good line 12
good line 13
String2
good line 14
good line 15
good line 16
bad line 15
bad line 16
bad line 17
... this code ..
Code:
sed -n '/\/tmp/,/^$/p' $InFile >$OutFile
... produced this OutFile ...
Code:
/tmp
good line 1
good line 2
good line 3
String1
good line 4
good line 5
good line 6
/tmp
good line 11
good line 12
good line 13
String2
good line 14
good line 15
good line 16
I tried that but that dose not search for string1.
You provided a sample input file. It is also helpful if you provide a corresponding sample output file. This helps us understand your description of the desired result.
I did make a small change the original post. All "sections" start with /tmp. So I need to search for string and get all lines that go upto /tmp and down to the empty line.
See below for the sample input and the sample output for the search I need to do. Remember the number of lines before and after the string may vary. The only thing that seems to be consistent is the /tmp on top and the empty line on the bottom of string1.
Sample input:
(empty line)
/tmp
bad line
bad line
bad line
bad line
(empty line)
/tmp
good line
good line
good line
String1
good line
good line
good line
(empty line)
/tmp
bad line
bad line
bad line
bad line
(empty line)
Sample output:
/tmp
good line
good line
good line
String1
good line
good line
good line
(empty line)
Last edited by edwardcode; 04-16-2013 at 11:16 AM.
That output bad lines I need nothing but the block of good lines. Maybe I am using the wrong command for this. I am fairly certain that there is a way to do it with regex. I also thought you could do it with sed but the more I research it the more trouble I am getting.
You are looking for a "grep with context" where the context is defined by something other than a fixed number of lines before and/or after a string match.
With this InFile ...
Code:
bad line 1
bad line 2
bad line 3
bad line 4
/tmp
good line 1
good line 2
good line 3
String1
good line 4
good line 5
good line 6
bad line 5
bad line 6
bad line 7
bad line 1
bad line 2
bad line 3
bad line 4
/tmp
good line 11
good line 12
good line 13
String2
good line 14
good line 15
good line 16
bad line 15
bad line 16
bad line 17
The only problem I have with that set up is that this will be applied on gig's and gig's of text files and I would rather just have one command (even if it is regex) that would be able to parse it. I also think that awk might be able to work, but I am still not sure.
The only problem I have with that set up is that this will be applied on gig's and gig's of text files and I would rather just have one command (even if it is regex) that would be able to parse it. I also think that awk might be able to work, but I am still not sure.
Don't discard a solution before trying it.
A single complicated awk might not be any better than a pipe of several simpler transformations.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.