Operating System - HP-UX
1847194 Members
4245 Online
110263 Solutions
New Discussion

manipulate variable string

 
SOLVED
Go to solution
Patrick Stevens
Occasional Contributor

manipulate variable string

I am trying to do something I thought was simple, but has turned out to be challenge.

I have a variable length string eg xxxxHHMMSS.
I need to drop the HHMMSS. (last 6 char)

My method was to get the length of the string:
slen=`echo $string |awk '{print length($0)}'`

Then calculate a new length:
newslen=`expr $slen - 6`

Then get the string minus the last 6 char
s=`echo $string |awk '{print substr($0,0,$newslen)'`

But awk does not like the $newslen var.

Any suggestions would be welcome.


As I lay on my bed looking at the stars, I wandered where the hell is the ceiling?
8 REPLIES 8
MarkSyder
Honored Contributor
Solution

Re: manipulate variable string

Have you declared slen and newslen as integers?

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
Jeroen Peereboom
Honored Contributor

Re: manipulate variable string

Something like
str=`echo "$string" | sed 's/......$//'` should do it
(substiture any 6 chars at end of line by nothing.

JP
curt larson_1
Honored Contributor

Re: manipulate variable string

try this

s=$(echo $string |
awk '{
x=length($0);
b=substr($0,1,x-6);
printf("%s",b);
}')

or to make it short

printf("%s",substr($0,1,length($0)-6));
Sridhar Bhaskarla
Honored Contributor

Re: manipulate variable string

Hi,

I would do something like this.

VAR=xxxHHMMSS
TOTAL=$(echo $VAR|wc -c)
(( REST = $TOTAL - 7 ))
newVAR=$(echo $VAR|cut -c 1-${REST})
echo $newVAR

You may want to put some checks like if VAR doesn't have more than 6 char etc., etc., wc -c returns
n+1 for n chars. Hence 7 in the REST calculations.

-Sri

You may be disappointed if you fail, but you are doomed if you don't try
curt larson_1
Honored Contributor

Re: manipulate variable string

or just using the shell

x=${string%??????}
s=${string#$x}
Ralph Grothe
Honored Contributor

Re: manipulate variable string

Patrick,

if you're using a POSIX shell it will evaluate the string length for you

e.g.

# var=blablaHHMMSS

# echo ${#var}
12

so you could use the cut command

# echo $var|cut -c$((${#var}-5))-
HHMMSS
Madness, thy name is system administration
curt larson_1
Honored Contributor

Re: manipulate variable string

the reason why what you were doing wasn't workings is that the variable newslen is defined in the shell but not to awk.

one to get the variable value into awk would be to use the -v switch to pass the value

awk -v newslen=$newslen .....

or being this is a short awk script you could play with the quotes to get the shell to do the substitution ( not recomended)

awk "{substr($0,0,$newslen);"

use double quotes instead of single quotes
Patrick Stevens
Occasional Contributor

Re: manipulate variable string

Thanks all, so easy but yet so hard.
As I lay on my bed looking at the stars, I wandered where the hell is the ceiling?