The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com




Thread: sed command
View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 06-05-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Code:
set -- `echo "$string" | sed -e 's/[^0-9][^0-9]*/ /g'`
echo First value is $1
echo Second value is $2
The set -- `command` idiom is rather obscure, but it's nevertheless a standard technique for splitting whitespace-separated tokens into the shell's positional variables. After the set, the first token in the output from command will be in $1, the second in $2, etc, and $# will tell you how many there were, just like when a script is invoked with command-line parameters.

If this is too weird for you then try the following.

Code:
variable1=`echo "$string" | sed -e 's/^[^0-9]*\([0-9][0-9]*\).*/\1/'`
variable2=`echo "$string" | sed -e 's/.*\([0-9][0-9]*\)[^0-9]*$/\1/'`
The first will grab the first number in the string, and the second, the last. If there are more, they will be lost.