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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cutting our part of text from string

Status
Not open for further replies.

MCubitt

Programmer
Mar 14, 2002
1,081
GB
In UNIX, how can I set a variable to be the value less the 1st 5 chars?

Eg QWERTYUIOP
returns
YUIOP

thanks


Applications Support
UK
 
Sorry, I actually want to retain the value

so PART1 = QWERT
and PART2 = YUIOP


Applications Support
UK
 
var="QWERTYUIOP"
PART1=`expr "$var" : '\(.....\).*'`
PART2=`expr "$var" : '.....\(.*\)'`

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 

or you can use " expr subst "


9. To return the string starting at position 11, for a length of 6 of the
string "Goodnight Ladies", enter:

expr substr "Goodnight Ladies" 11 6

The following is displayed:
Ladies



 
In the Korn Shell, you can define your variable to only take the left or right five characters. That way you don't have to do any chopping, they will just always accept what you want.
Code:
$ typeset -L5 LEFT5="1234567890"
$ print ${LEFT5}
12345
$
$ LEFT5=ABCDEFGHIJK
$ print ${LEFT5}
ABCDE
$
$ typeset -R5 RIGHT5
$ RIGHT5="1234567890"
$ print ${RIGHT5}
67890
Just change the [tt]5[/tt] in the [tt]-L5[/tt] or [tt]-R5[/tt] to another number to get a different size.

Hope this helps.
 
Oops, I think I read the question wrong. If you want to chop the first five and also keep the rest, for any length variable, you could do this...
Code:
$ VAR="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$ PART2=${VAR#?????}
$ PART1=${VAR%${PART2}}
$ print ${PART1}
ABCDE
$ print ${PART2}
FGHIJKLMNOPQRSTUVWXYZ
And again, this is Korn Shell.

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top