awk question

Hi guys,

I need some advice on how to extract the number from variable which holds directory path.
Here is my example:

DIR="/ts/pod/px/px347/config/pxedbsvc.cfg"

I need to extract the number after 'px' (in the example above it is 347)

I have tried to use awk and "##*" but it does not seem to do what I need.

Thanks a lot for advice.
I am new on the forum and very good with 'awk/nawk' :frowning:

---------- Post updated at 07:37 AM ---------- Previous update was at 07:34 AM ----------

px* patters can be on any place in variable DIR. This variable changes dynamically all the time .

Hello desant,

Could you please try following and let us know if this helps you.

cat script1.ksh
DIR="/ts/pod/px/px347/config/pxedbsvc.cfg"
echo $DIR  | awk -F"/" '{for(i=1;i<=NF;i++){if($i ~ /px[0-9]+/){sub(/px/,X,$i);print $i}}}'

Output will be 347 , kindly do let us know if this helps you.

Thanks,
R. Singh

1 Like

This worked great. Thanks a lot for the advice.

awk is not necessary, nor is any other utility. Try also pure shell:

TMP=${DIR#${DIR%%px[0-9]*[^/]/*}px}
echo ${TMP%%/*}
347
1 Like

Thanks a lot RudiC.
That works great as well.