For Programmers: Free Programming Magazines  


Home > Archive > Unix Shell Programming > March 2004 > extracting text









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author extracting text
Joe MacDonald

2004-03-27, 12:14 am

Hi All,

I'm trying to extract text from lines beginnnig with 145 (^145) from
the point beginning with $b and ahead 9 characters.

i.e.

145 randomtext$b092452569morerandomstuff
145 example$b008271451randomlengthlines

so I would want the digits 092452569 and 008271451.

A lot of the lines are the same length, so I can use cut to grab
those. Unfortunately some aren't..

Any awk/sed/perl/shell etc. solutions welcome.

Thanks

Joe.
Andre Majorel

2004-03-27, 12:14 am

In article <249f8ddc.0403220715.26127373@posting.google.com>, Joe
MacDonald wrote:

> I'm trying to extract text from lines beginnnig with 145 (^145) from
> the point beginning with $b and ahead 9 characters.
>
> i.e.
>
> 145 randomtext$b092452569morerandomstuff
> 145 example$b008271451randomlengthlines
>
> so I would want the digits 092452569 and 008271451.


awk '/^145/ {
ofs = index($0,"$b");
if (ofs != 0)
print substr($0, ofs + 2, 9);
}'

--
André Majorel <URL:http://www.teaser.fr/~amajorel/>
Depuis Malraux, on n'a malheureusement jamais su trouver de
liens entre le monde intellectuel et la droite.
-- Jacques Chirac, cité dans le Canard Enchaîné du 2004-02-25
Ed Morton

2004-03-27, 12:14 am



Joe MacDonald wrote:

> Hi All,
>
> I'm trying to extract text from lines beginnnig with 145 (^145) from
> the point beginning with $b and ahead 9 characters.
>
> i.e.
>
> 145 randomtext$b092452569morerandomstuff
> 145 example$b008271451randomlengthlines
>
> so I would want the digits 092452569 and 008271451.
>
> A lot of the lines are the same length, so I can use cut to grab
> those. Unfortunately some aren't..
>
> Any awk/sed/perl/shell etc. solutions welcome.


Try this:

gawk '/^145/{print gensub(/(.*\$b)(.........)(.*)/, "\\2", 0)}'

Regards,

Ed.

> Thanks
>
> Joe.


rakesh sharma

2004-03-27, 12:14 am

joe_macdonald25@yahoo.com (Joe MacDonald) wrote in message:

>
> I'm trying to extract text from lines beginnnig with 145 (^145) from
> the point beginning with $b and ahead 9 characters.
>
> i.e.
>
> 145 randomtext$b092452569morerandomstuff
> 145 example$b008271451randomlengthlines
>
> so I would want the digits 092452569 and 008271451.
>
> A lot of the lines are the same length, so I can use cut to grab
> those. Unfortunately some aren't..
>
> Any awk/sed/perl/shell etc. solutions welcome.
>
> Thanks
>
> Joe.


a 'sed' solution can be:

sed -e '
/^145/!d
s/\$b[0-9]\{9\}/\
&\
/;s/.*\n..\(.*\)\n.*/\1/
' yourfile
Daniele F.

2004-03-27, 12:14 am

Nell'articolo <249f8ddc.0403220715.26127373@posting.google.com>, Joe MacDonald ha scritto:

> I'm trying to extract text from lines beginnnig with 145 (^145) from
> the point beginning with $b and ahead 9 characters.
>
> i.e.
>
> 145 randomtext$b092452569morerandomstuff
> 145 example$b008271451randomlengthlines
>
> so I would want the digits 092452569 and 008271451.


sed -e '/^145/s/^.*\$b\(.........\).*$/\1/' FILE

--
Bye, Daniele F.
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com