Operating System - Linux
1754419 Members
3045 Online
108813 Solutions
New Discussion юеВ

Re: extracting data from string

 
SOLVED
Go to solution
Chartier Jerome
Frequent Advisor

extracting data from string

Hi All,

I need to extract some tagged line from a file formated like this:

flag_start
line1
line2
...
...
Serial_number1
linea
lineb
...
...
flag_stop
flag_start
line1
line2
...
...
Serial_number2
linea
lineb
...
...
flag_stop

In the file, there are several flagged patern with Serial_number1, and I would like to extract the whole pattern and write it in a new file ...

Can someone help me with this

Thanks in advance for your help

Best Regards

J├йr├┤me

J@Y
15 REPLIES 15
Doug O'Leary
Honored Contributor

Re: extracting data from string

Hey;

My understanding of your request is that you want everything from "Serial_number1" to flag_stop in a different file. Assuming that's the case, it's easy:

sed -n -e '/^Serial_number1/,/^flag_stop/p' ${in_file} > ${new_out_file}

HTH;

Doug

------
Senior UNIX Admin
O'Leary Computers Inc
linkedin: http://www.linkedin.com/dkoleary
Resume: http://www.olearycomputers.com/resume.html
Leif Halvarsson_2
Honored Contributor

Re: extracting data from string

Hi,
Not sure about exact what you want to do but, awk is a very useful tool for processing text files. Search the forum for "awk" and you will find a lot of examples how to use awk.
Chartier Jerome
Frequent Advisor

Re: extracting data from string

Hi all,

Thanks for your answers, in fact, In each file, the pattern is flagged beetween flag_start and flag_stop.
In each file, I recognize the pattern by Serial_number, and there are several per file.
What I would like is to extract all the patterns (beetween flag_start and flag_stop) that have the same Serial_number.


Thanks in advance for your help

J├Г┬йr├Г┬┤me C
J@Y
Simon Hargrave
Honored Contributor

Re: extracting data from string

Hmm, so you want your logic to say: -

If the "serial number" pattern within this particular flagged block matched X, then extract the whole block?

If so then try something like:

awk -F"\n" 'BEGIN { matched=0 ; block="" } \
$1 !~ "^flag_s*" { block = block$1"\n" } \
$1 == "Serial_number1" { matched = 1 } \
$1 == "flag_stop" { if ( matched == 1 ) { print block } ; block="" ; matched=0 }' infile

Basically it sets a flagged called "matched" to zero. Then reads each line in turn, and appends it to a variable. If it gets to a matching serial number, it sets the "matched" flag to 1. Then when it comes to "flag_stop" it knows it's finished a block, and if at this time "matched" is 1, it prints the block, otherwise it doesn't.

This suit?
Kent Ostby
Honored Contributor

Re: extracting data from string

Jerome --

Can you give us like 10 to 20 lines of a real file.

The problem I'm having is figuring out if Serial_numberN appears multiple times or what.

The following script will print ALL of the data between flag_start and flag_stop. I suspect that's not what you want, but I thought I would post that much while I was asking my questions.

Create a file called useme.awk with the following in it:

BEGIN{daflag=0;}
/flag_start/ {daflag=1;next}
/flag_stop/ {daflag=0;next}
daflag==1 {print $0}

Run:

awk -f useme.awk < inputfile > outputfile


"Well, actually, she is a rocket scientist" -- Steve Martin in "Roxanne"
Simon Hargrave
Honored Contributor

Re: extracting data from string

Oh and my example assumed you wanted to strip the flag_start and flag_stop entries. If you want to keep them, simply change the second line to be:

{ block = block$1"\n" } \

and thus removing the ^flag_s* ignorance.
Rajesh SB
Esteemed Contributor

Re: extracting data from string

Hi,

I got some idea to extract the flagged patern to share with you.
Using combination of commands.

grep -n filename

where -n option give you line no. of startflag.
Extract the pattern using the head or tail command option to -/+.

Hope this hint may give some more idea.

Regards,
Rajesh

Hein van den Heuvel
Honored Contributor

Re: extracting data from string


How do you recognize a serial number?
Does it always start with "Serial" in the begin of a line?
You wanted the lines BETWEEN start and stop right, not inclusive right?
If so ten I think the perl below is what you need.
You'll need to adapt the 'Serial' match to the actual pattern, and you probably want a different algoritme for the file name generation ($file = ...)

hth,
Hein.

while (<>) {
if (/^flag_start/) {
$save = 1;
next;
}
if (/^flag_stop/) {
$save = 0;
if ($file) {
open (FILE,">> $file") or die "could not append to $file";
print FILE $line while ($line = shift @lines);
close FILE;
}
}
next unless $save;
push @lines, $_;
if (/^Serial/) {
chop;
$file = $_ . ".tmp" ;
}
}
Chartier Jerome
Frequent Advisor

Re: extracting data from string

Thanks everyone ...

I would like to include the lines.
I am sorry, I am very bad in scripting so ..
Where do I put the input filename .. in the script?

Thanks in advance


Best Regards


J├Г┬йr├Г┬┤me
J@Y