I need to split the string msu1_2
It should be generic for any string of the form msu<digits>_<digits>
so that i get $X =1 and $Y = 2
Please help
Thanks
I need to split the string msu1_2
It should be generic for any string of the form msu<digits>_<digits>
so that i get $X =1 and $Y = 2
Please help
Thanks
> str=msu12_34
> eval $(echo "$str" | sed -n 's/msu\([0-9]*\)_\([0-9]\)/X=\1;Y=\2/p')
> echo $X
12
> echo $Y
34
>
Jean-Pierre.
X=`echo msu1_2 | sed 's/[a-z]//g' | awk -F '' '{print $1}'`
Y=`echo msu1_2 | sed 's/[a-z]//g' | awk -F '' '{print $2}'`
bash
# s="msu1_2 "
l# echo ${s/msu/}
1_2
# IFS=_
# set -- ${s/msu/}
# echo $1
1
# echo $2
2
# s="msu100_22"
# set -- ${s/msu/}
# echo $1
100
# echo $2
22
#
> x=$(echo "msu1_2" | tr -d "[:alpha:]" | cut -d"_" -f1)
> y=$(echo "msu1_2" | tr -d "[:alpha:]" | cut -d"_" -f2)
> echo $x
1
> echo $y
2
And, of course, if you were to do this in perl and wanted the $x and $y available with a perl script:
#!/usr/bin/perl
open(FILE, "<$ARGV[0]") or die "Cannot open $ARGV[0] for read :$!";
@list = <FILE>;
close( FILE );
for (@list)
{
chomp;
($x,$y) = split /\_/,$_;
if (($x =~ /^-?\d/) && ($y =~ /^-?\d/)) #If $x and $y are both numbers, proceed
{
print "\$x = $x\t\$y = $y\n";
}