Extracting Multiple Lines from a Text File

Hello. I am sorry if this is a common question but through all my searching, I haven't found an answer which matches what I want to do.

I am looking for a sed command that will parse through a large text file and extract lines that start with specific words (which are repeated throughout the file).

Example Text
Line 1: This is example line 1
Line 2: This is example line 2
Line 3: This is example line 3
Line 4: This is example line 4
Line 5: This is example line 5
<blankline>
Line 1: This is example line 1
Line 2: This is example line 2
Line 3: This is example line 3
Line 4: This is example line 4
Line 5: This is example line 5
<blankline>
Line 1: This is example line 1
Line 2: This is example line 2
Line 3: This is example line 3
Line 4: This is example line 4
Line 5: This is example line 5
etc.

Output Needed (in a new text file):
Line 2: This is example line 2
Line 4: This is example line 4
<blankline>
Line 2: This is example line 2
Line 4: This is example line 4
<blankline>
Line 2: This is example line 2
Line 4: This is example line 4
etc.

I tried grep but couldn't get the command to understand the separate lines.

Again, sorry if this is an obvious answer but finding it fails me.
Thanks.

grep solution

grep -e '^Line 2' -e '^Line 4' -e '^$'  inputfile > newfile

The ^ indicates start of a line '^$' is a blank line with no spaces.

Thanks Jim. The 'blank line' part hangs the process but getting the two lines is very helpful.

Thank you again.

Hangs? Makes no sense. I am assuming you are "translating" the example script to fit what you think the real data is.

Please show the output of

od -c inputfilename | head -40

This should show the real characters the file. If you cannot post the data, you can use the output to determine what a blank line really has in it. Note the red stuff in my original post - that was the hang. Gone now.

Thank you again Jim for your support. I noticed the missing "e" in your original post and edited it but that didn't make a difference. :slight_smile:

Regarding your second command post, I can't post the output but I can see every new line is "\r\n".

BTW, I am not sure if I was that clear about the blank line aspect, I was hoping the grep command would grab the two lines and then insert a blank line for readability. I am not sure if that was what your suggestion of " -e '^$' " would do but regardless, I am at least getting the two lines which is helpful.

Thank you again for your support. It is appreciated.