String operations

Can you give me some suggestions to split below string into three parts using shell scripts.. Script has to print all alphabets before the number, then number and then all alphabets after the number..
input: chris martin 200173 845747 mech engineer
output: chris martin 200173 845747 mech engineer

Thanks in advance.

???
Check your example of input/output.

Script has to split the string in such a way that all alphabets before the number, then number and then all alphabets after the number..

Input: chris martin 200173 845747 mech engineer
output:
var1=chris martin
var2=200173 845747
var3=mech engineer

echo 'chris martin 200173 845747 mech engineerchris' | 
sed -r 's/([^0-9]*)([0-9 ]*)([^0-9]*)/var1=\1\nvar2=\2\nvar3=\3/'

GNU sed. Change "(" and ")" to "\(" and "\)" for another one (and remove -r option).

1 Like

Thank you very much, Yazu. I appreciate your quick help!
It worked!

help needed.. when i tried echo $var1 i was not getting output as "chris martin"
what i suppose to do to print values explicitly using

echo $var1 
echo $var2
echo $var3

Command:

echo "chris martin 200173 845747 mech engineerchris" | sed  's/\([^0-9]*\)\([0-9 ]*\)\([^0-9]*\)/var1=\1\nvar2=\2\nvar3=\3/'

current output:

var1=chris martin nvar2=200173 845747 nvar3=mech engineerchris

Try this:

#!/bin/ksh

eval $(print "chris o'neil 200173 845747 mech engineer" | sed 's/\([^0-9]*\)\([0-9 ]*\)\([^0-9]*\)/var1="\1";var2="\2";var
3="\3"/')

## Kludgy way of getting rid of trailing space.  Should change
## the above sed command somehow.
var1=$(print $var1|sed 's/ $//')
var2=$(print $var2|sed 's/ $//')

print "[$var1]"
print "[$var2]"
print "[$var3]"

Output:

$ xyz
[chris o'neil]
[200173 845747]
[mech engineer]
$