- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- manipulate variable string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:09 AM
02-25-2004 03:09 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:12 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:12 AM
02-25-2004 03:12 AM
Re: manipulate variable string
str=`echo "$string" | sed 's/......$//'` should do it
(substiture any 6 chars at end of line by nothing.
JP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:15 AM
02-25-2004 03:15 AM
Re: manipulate variable string
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:16 AM
02-25-2004 03:16 AM
Re: manipulate variable string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:19 AM
02-25-2004 03:19 AM
Re: manipulate variable string
x=${string%??????}
s=${string#$x}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:22 AM
02-25-2004 03:22 AM
Re: manipulate variable string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:23 AM
02-25-2004 03:23 AM
Re: manipulate variable string
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-25-2004 03:27 AM
02-25-2004 03:27 AM