extracting integer from data

Hi people,

I've encountered a problem. I have a data file like:

asd:$123:2
zxc:$456:4

But when I want to extract "$123" and get the number with this command:

echo $123 | cut -c 1-10

Im returned with 23 instead of 123.

Please help me out, thanks.

not sure what you are doing!!

echo $123 | cut -c 1-10
the above will give ouput you the characters from 1 to 10 from the data stored in the variable.

is that what you are trying?

-ilan

][, [[]] ][,

$1 = first parameter from the command line
echo $123
is equal to
echo -n "$1"
echo -n "23"
and if the parameter $1 is empty then it is equal to
echo "23"

In other words you are EXECUTING the string.

Hints how to solve this problem:

  1. You have probably hardcoded this. Modify the line to:
    echo '$123' | cut -c 1-10
  2. You should probably keep this '$123' in some variable - then usage will be different
sed -e 's/\(^.*:\$\)\(.*\)\(:.*$\)/\2/g' datafile

This suffice:

sed 's/.*\$\(.*\):.*/\1/' file

Regards