Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

substr in c shell scripting

Status
Not open for further replies.

sjh7899

Programmer
Jun 11, 2002
20
0
0
CA
I am very new to unix scripting and I have some up against a problem and i cant find any documentation on it. I simply want to substr out part of a string and assign it to another var. Something like this....

set var = substr($another_var,1,3)

If anyone could help me out with this i would really appreciate it.
Steve.
 
another="some text"

var2=`echo $another | awk '{ print substr($0,1,3)} '`

echo var2

result:
som

Cheers.
 
Another solution using 'expr'
[tt]
set another_var="some text"
set var = `expr substr "$another_var" 1 3`
[/tt]

Jean Pierre.
 

set another_var="some text"
set var = `echo $another_var | cut -c 1-3`

 
Thanks everyone! I actually ended up using the cut command. Thanks to all for your input.
Steve.
 
it's quite funny :eek:)

that's why I like unix/linux, for the same task you have a lots of posibilities!! and you can create new ones..

Just thinking...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top