Computing.Net > Forums > Unix > How to use substr function in sh

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

How to use substr function in sh

Reply to Message Icon

Name: ravichap
Date: December 27, 2004 at 11:24:07 Pacific
OS: Unix
CPU/Ram: No idea
Comment:

Hi,

I want to use substr function, but it is not working in sh. As per help following command works with bash.

stringZ=123456
echo `expr substr $stringZ 1 2`

Any idea what is the problem.
If it won't work, then is there any other alternative of substr ( get few characters from a string ) which can be use in sh, ksh, csh.

Thanks
Ravi




Sponsored Link
Ads by Google

Response Number 1
Name: dk0871
Date: December 27, 2004 at 14:08:53 Pacific
Reply:

Try using somting like this :-

stringZ=123456
expr "$stringZ" : '\(..\)....'

This will return 12 out of 123456.

dkr


0
Reply to Message Icon

Related Posts

See More


search part of the word, ... zFS and sero size file



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: How to use substr function in sh

used-defined functions in C SHell ? www.computing.net/answers/unix/useddefined-functions-in-c-shell-/5786.html

How to use sed editor in a shell script www.computing.net/answers/unix/how-to-use-sed-editor-in-a-shell-script/8509.html

how to read binary files in Unix www.computing.net/answers/unix/how-to-read-binary-files-in-unix/4566.html