Operating System - HP-UX
1834628 Members
3349 Online
110069 Solutions
New Discussion

Re: Cut the last character(s) from a word

 
SOLVED
Go to solution
Sajeesh O.K
Advisor

Cut the last character(s) from a word

Hi

Anybody know how to cut last few character(s)
from a word.

I have sclap1,scl_ap2,sclk_ap3...sclok_ap20. and want to cut 1,2...20. or atleast ap1,ap2,....ap20

Thanks
Sajeesh
9 REPLIES 9
Slawomir Gora
Honored Contributor

Re: Cut the last character(s) from a word

Hi,

you can use tr utility:
ex:
echo sclk_ap3 | tr -d [:digit:]
Victor Fridyev
Honored Contributor

Re: Cut the last character(s) from a word

Hi,

You can use awk:
awk '{printt substr($0,1,length($0)-N)}' N=3 file

You can chnge N as you need
HTH
Entities are not to be multiplied beyond necessity - RTFM
James R. Ferguson
Acclaimed Contributor

Re: Cut the last character(s) from a word

Hi Sajeesh:

# echo sclok_ap20|sed 's/[0-9]\{1,\}//g'

This will remove any number of digits. If you want to specify a range of digits, as for example only 2-3 digits, but not one or more than four:

# sclok_ap20|sed 's/[0-9]\{2,3\}//g'

In general, {m,n} specifies the minimum number and the maximum number. If 'n' is omitted, as in the first example, then the maximum is unlimited.

Regards!

...JRF...
Sajeesh O.K
Advisor

Re: Cut the last character(s) from a word

Hi All

Actually I want to capture the digit from these words, not to exclude. It will be either 1 digit or 2 digit.

I have to use these captured number within a for loop


Regards
Sajeesh
James R. Ferguson
Acclaimed Contributor

Re: Cut the last character(s) from a word

Hi (again) Sajeesh:

OK, then let's use perl:

# echo sclok_ap20|perl -nle 'm/(\d+)/;print $1'

...prints 20

This extracts one or more digits.

Regards!

...JRF...
Slawomir Gora
Honored Contributor
Solution

Re: Cut the last character(s) from a word

Hi,
simple script to get digits

#!/bin/sh

NAMES="sclap1 scl_ap2 sclk_ap3 sclok_ap20"

for N in `echo $NAMES`
do
NR=`echo ${N} | tr -d [:alpha:] | tr -d '_'`
echo ${NR}
done
Sajeesh O.K
Advisor

Re: Cut the last character(s) from a word

Hi

Thanks to you all. Not familer with the perl, so used the last one.

Thanks
Sajeesh
Arturo Galbiati
Esteemed Contributor

Re: Cut the last character(s) from a word

Hi,
to obtain only teh difit:
echo "sclap1 scl_ap2 sclk_ap3 sclok_ap20"|tr -d [:alpha:][:punct:]

It ill retrun aonly the difit in teh strings.
HTH,
Art
Muthukumar_5
Honored Contributor

Re: Cut the last character(s) from a word

Same thread in linux too :). See the replies there.

http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=984492

Do you want to get digits only then,

simpy then,

# cat file
sclap1
scl_ap2
sclk_ap3
sclok_ap20
# sed 's/^[^0-9]*//' file
1
2
3
20

# for i in `sed 's/^[^0-9]*//' file`
> do
> echo $i
> done

-Muthu
Easy to suggest when don't know about the problem!