Extracting text between two strings, multiple instances


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Extracting text between two strings, multiple instances
# 1  
Old 10-12-2012
Extracting text between two strings, multiple instances

Hi experts,

Ive got a text file which has the following text which will occur in this format at least one time:

+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
<<=========================+

It is likely that this will occur numerous times in a file which shouldn't have more than 200 lines.

The text between >> and << is what I want to extract to a different file.

The following code will grab it but only the first instance:
Code:
#----------------------------------------------------------------#
#      Scan Newly created text file for action summaries         #
#----------------------------------------------------------------#


sed 's/\(>>\|<<\)/\n&\n/g' ${LOG_PREV}/${file1}.TXT | 
/usr/xpg4/bin/awk  '/<</{p=0;printf s;s=""}p{s=s$0"\n"};/>>/{p=1}'> ${LOG_path}/${toolnr}PROB_Des.TXT

How can I get this to loop through to EOF?

Thanks in advance for help!

M
# 2  
Old 10-12-2012
Hi

Code:
sed -n '/>>$/,/^<</{/>>$\|^<</d;p;}' file

Guru.
# 3  
Old 10-12-2012
Hi Guru,

Thanks for the code - It does 90% of what I want:
The text it is lanalysing looks like this:

+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc
<<=========================+


+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
<<=========================+
And the output is:

+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc
<<=========================+
+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad

How can I get it to output only the text between the ">>" and "<<" strings?

Thanks again.

M
# 4  
Old 10-12-2012
Hi Martin
When I run the sed command with your input file, it gives what is expected. 90% means what? Please clearly state what you are getting right now, and how did you expect the output to appear?

Guru.
# 5  
Old 10-12-2012
Hi Guru, as in my above post the text I am looking through looks like the following:

Quote:
+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc
<<=========================+
Some other info
some other info
I dont want this

I dont want this either

+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
<<=========================+
And the command is currently outputting the following:

Quote:
+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc
<<=========================+
+=========================>>
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
Ideally I would want to output just the following:

Quote:
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc

Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
Thx again
# 6  
Old 10-12-2012
Which sed implemetation are you using? Try this (slight modification to Guru's solution):
Code:
sed -n '/>>$/,/^<</{;/>>$/d;/^<</d;p;}' file

# 7  
Old 10-12-2012
Hi elixir_sinari,

Thanks a bunch works great!
Now to try an make it even better (not a necessity)...

Is there a way to put a line space between each output of the instance?

I'm currently getting:

Quote:
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
Would it be possible to get the output to look like:
Quote:
Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajc

Some stuff that evreryone should knnow
other stufsjdokajkajokajda
aijhjajcdjajcisajcqsqdqwdqad
I know I'm asking alot and I really appreciate it!

Thx

M

Last edited by martin0852; 10-12-2012 at 03:30 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Extracting strings at various positions of text file

Hi Team - I hope everyone has been well! I export a file from one of our source systems that gives me more information than I need. The way the file outputs, I need to extract certain strings at different positions on the file and echo them to another file. I can do this in batch easily,... (2 Replies)
Discussion started by: SIMMS7400
2 Replies

2. Shell Programming and Scripting

Grep for text between twp strings for multiple occurances.

I need to get text between two strings <app-deployment file=" and </app-deployment> as it appears more than once in the file then how can i store the text between each occurrence in a separate file ? Thus, i need the below to go in found1.tmp and the below to go in found2.tmp ... (8 Replies)
Discussion started by: mohtashims
8 Replies

3. Shell Programming and Scripting

Extracting values based on line-column numbers from multiple text files

Dear All, I have to solve the following problems with multiple tab-separated text file but I don't know how. Any help would be greatly appreciated. I have access to Linux mint (but not as a professional). I have multiple tab-delimited files with the following structure: file1: 1 44 2 ... (5 Replies)
Discussion started by: Bastami
5 Replies

4. UNIX for Dummies Questions & Answers

Extracting 22-character strings from text using sed/awk?

Here is my task, I feel sure this can be accomplished with see/awk but can't seem to figure out how. I have large flat file from which I need to extract every case of a pairing of characters (GG) in this case PLUS the previous 20 characters. The output should be a list (which I plan to make... (17 Replies)
Discussion started by: Twinklefingers
17 Replies

5. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: MrDumbQuestion
4 Replies

6. Shell Programming and Scripting

Extracting text between two constant strings

Hi All, I have a file whose common patter is like this: .I 1 .U 87049087 .S Some text here too .M This is a text .T Some another text here .P Name of the book .W Some lines of more text. This text needs to be extracted. .A more text goes here too .I 2 (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

7. Programming

Control multiple program instances - open multiple files problem

Hello. This shouldn't be an unusual problem, but I cannot find anything about it at google or at other search machine. So, I've made an application using C++ and QtCreator. I 've made a new mime type for application's project files. My system (ubuntu 10.10), when I right click a file and I... (3 Replies)
Discussion started by: hakermania
3 Replies

8. Shell Programming and Scripting

Extracting text between two strings

Hi, I've looked at a few existing posts on this, but they don't seem to work for my inputs. I have a text file where I want to extract all the text between two strings, every time that occurs. Eg my input file is Anna said that she would fetch the bucket. Anna and Ben moved the bucket.... (9 Replies)
Discussion started by: JamesForeman
9 Replies

9. Shell Programming and Scripting

Extracting text between two strings, first instance only

There are a lot of ways to extract text from between two strings, but what if those strings occur multiple times and you only want the text from the first two strings? I can't seem to find anything to work here. I'm using sed to process the text after it's extracted, so I prefer a sed answer, but... (4 Replies)
Discussion started by: fubaya
4 Replies

10. Shell Programming and Scripting

extracting a set of strings from a text file

i have textfiles that contain a series of lines that look like this: string0 .................................................... column3a column4a string1**384y0439 ..................................... column3b column4b... (2 Replies)
Discussion started by: Deanne
2 Replies
Login or Register to Ask a Question