extract from a long sentence

Hi,

There's a long sentence from which I need to extract only the part which is at the right side of the word LENGTH i.e. 15

 
long sentence :
 
<INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" LENGTH ="15" PHYSICALOFFSET ="29" PICTURETEXT ="" PRECISION ="15" SCALE ="0" USAGE_FLAGS =""/>
 

I could do the following

 
cat $file_with_the_long_sentence | awk '{ if ($0 ~ "LENGTH")  { print $33} }'

but the part that is worrying me is

{ print $33} }

as it might vary so not neccessarily I would have LENGTH at the 33rd field....also I can't use space (" ") as the field-separator as it is clear that there's space even between the variable (LENGTH) and the assignment operator (=).

Can someone please suggest a flexible way to do this?

-dips

Hi,

Try this:

$ cat infile
<INPUT VAR1 ="" DATATYPE ="number(p,s)" VAR2 ="" VAR3 ="3" VAR4="0" VAR5 ="ELEMITEM" VAR6 ="NO" VAR7 ="NOT A KEY" VAR8 ="17" LEVEL ="0" NAME ="UNIX" NULLABLE ="NOTNULL" OCCURS ="0" OFFSET ="19" LENGTH ="15" PHYSICALOFFSET ="29" PICTURETEXT ="" PRECISION ="15" SCALE ="0" USAGE_FLAGS =""/>
$ perl -ne 'm/length\s*="([[:digit:]]+)"/i; print "$1\n";' infile
15

Regards,
Birei

awk -F\" '/^ *=/{print $2}' RS=LENGTH file
1 Like

Thanks cabrao! Can you please explain the command?

Sorry birei, I haven't used perl at all and therefore don't understand it........and because this was part of a korn shell script I was looking for a solution in unix scripting only.

-dips