Extract a string from a line

Hiee all

Can anyone tell me how to extract a string from a given line.

STAPISDK_RELEASE_32_BL012_2011_JAN_25.1597

I want to extract BL012 from above.

as 102 keeps on changing i want smthing like that it extract BL and 102 extrct by its own.

Thankx guyzz

Have you tried using cut ?

awk -F"_" '{for(i=1;i<=NF;i++) if($i~/BL[0-9]+/) print $i}' file
1 Like

Try this,

echo 'STAPISDK_RELEASE_32_BL012_2011_JAN_25.1597' | cut -d"_" -f4

thanx a lot
can u explain me how did u do it? It will be great if u spend your litl bit time to explain me ?

-F"_": using "_" as record separator variable
for (i=1;i<=NF;i++) :make a loop
if($i~/BL[0-9]+/) print $i : if $i contains patten "BL[0-9]+", which you want, then print it
1 Like
grep -o "BL[0-9]*" infile
sed 's/.*\(BL[0-9]*\).*/\1/' infile
$ echo STAPISDK_RELEASE_32_BL012_2011_JAN_25.1597 | grep -o BL[0-9][0-9][0-9]

The beauty here is that it evaluates only for "BL" followed by 3 digits.

Damn, I didn't saw the second page....

 $ echo STAPISDK_RELEASE_32_BL012_2011_JAN_25.1597 | ruby -e 'puts gets.scan(/(BL\d+)/)' 
BL012