- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- substring builtin in ksh
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
Discussions
Discussions
Discussions
Forums
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
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
тАО09-03-2002 12:21 AM
тАО09-03-2002 12:21 AM
substring builtin in ksh
Do you know if there is builtin substring funtion under ksh (Solaris version this time)?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-03-2002 12:50 AM
тАО09-03-2002 12:50 AM
Re: substring builtin in ksh
i know no substring function in ksh but you can use awk for this ie:
var="fullstring"
sub=$(echo $var|awk '{print substr($0,5,3)}')
will set sub to "str"
Regards
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-03-2002 01:20 AM
тАО09-03-2002 01:20 AM
Re: substring builtin in ksh
(From man sh-posix)
${parameter#pattern}
${parameter##pattern}
If the shell pattern matches the beginning of the value of parameter, the value of this substitution is the value of the parameter with the matched portion deleted; otherwise, the value of this parameter is substituted. In the former case, the smallest matching pattern is deleted; in the latter case, the largest matching pattern is deleted. These characters # or % should be escaped by a backslash (\) or quotes ('').
${parameter%pattern}
${parameter%%pattern}
If the shell pattern matches the end of the value of parameter, the value of parameter with the matched part is deleted; otherwise, substitute the value of parameter. In the former, the smallest matching pattern is deleted; in the latter, the largest matching pattern is deleted. These characters # or % should be escaped by a backslash (\) or quotes ('').
Regards,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-03-2002 01:23 AM
тАО09-03-2002 01:23 AM
Re: substring builtin in ksh
#!/bin/sh
# substr
if [ $# -ne 3 ]; then
exit 2
fi
lg=$(( $2 + $3 - 1 ))
echo $1 | cut -c$2-$lg
~~~~~~~~~~~~~~~~~~
oudartj:/home/oudartj/k1 $ substr abcdef 3 2
cd
Jean-Luc
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-03-2002 01:36 AM
тАО09-03-2002 01:36 AM
Re: substring builtin in ksh
I know that in linux you can do with variables just:
> a=example
> echo ${a:0:2}
exa
I didn't want to use awk and cut, just to optimize and do it faster, but I can see there is no other way.
Thank you so much.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2002 02:09 AM
тАО09-04-2002 02:09 AM
Re: substring builtin in ksh
This functionality appears in ksh version newer than 11/16/88.
Unfortunately, the standard ksh version in HP-UX is 11/16/88 and so does include the latest ksh enhancement.
However sh-posix include a few , but not all of these.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2002 02:41 AM
тАО09-04-2002 02:41 AM
Re: substring builtin in ksh
While certainly not as conservative as a shell built-in you could use:
# expr substr expr1 expr2 expr3
This takes the substring expr1, starting at expr2 for the length given by expr3.
See the man pages for 'expr'.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО09-04-2002 03:44 AM
тАО09-04-2002 03:44 AM
Re: substring builtin in ksh
You said:
**********************************
I didn't want to use awk and cut, just to optimize and do it faster, but I can see there is no other way.
**********************************
Are you kidding me??
awk is a thousand (maybe a little exaggeration) times faster than ksh could ever imagine being.
And if you are serious about doing any kind of string parsing you should use either awk or better yet: perl!!!
live free or die
harry
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-25-2003 02:53 PM
тАО03-25-2003 02:53 PM
Re: substring builtin in ksh
I'm trying to do a similar thing. I have the string:
" N SAS Shared Components" Notice the spaces. I need to substring from 0-14 15-26 27-48. I tried the command above, but it starts with the first non-blank character (N). Any suggestions?
Thanks,
MOnica
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО03-25-2003 04:03 PM
тАО03-25-2003 04:03 PM
Re: substring builtin in ksh
but, quite a few of the 93 enhancements are included in /usr/dt/bin/dtksh. using this shell you will be able to do a substring using an offset and length.
monica, your probably better off using the cut command.