# extracting integer from data

**URL:** <https://community.unix.com/t/extracting-integer-from-data/182228>\
**Category:** Shell Programming and Scripting\
**Created:** [January 18, 2008, 3:04am UTC](https://community.unix.com/t/extracting-integer-from-data/182228 "2008-01-18T03:04:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![grotesque](https://community.unix.com/letter_avatar/grotesque/32/5_5575768a8748004e209b776fc1b2916d.png) [@grotesque](https://community.unix.com/u/grotesque)\
**Post date:** [January 18, 2008, 3:04am UTC](https://community.unix.com/t/extracting-integer-from-data/182228/1 "2008-01-18T03:04:22Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ilan](https://community.unix.com/letter_avatar/ilan/32/5_5575768a8748004e209b776fc1b2916d.png) [@ilan](https://community.unix.com/u/ilan)\
**Post date:** [January 18, 2008, 3:12am UTC](https://community.unix.com/t/extracting-integer-from-data/182228/2 "2008-01-18T03:12:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![adderek](https://community.unix.com/letter_avatar/adderek/32/5_5575768a8748004e209b776fc1b2916d.png) [@adderek](https://community.unix.com/u/adderek)\
**Post date:** [January 18, 2008, 5:41am UTC](https://community.unix.com/t/extracting-integer-from-data/182228/3 "2008-01-18T05:41:30Z")

</div>

][_, [[]] ][_,

$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

---

<div class="post-metadata">

**Author:** ![fpmurphy](https://community.unix.com/user_avatar/community.unix.com/fpmurphy/32/110_2.png) [@fpmurphy](https://community.unix.com/u/fpmurphy)\
**Post date:** [January 18, 2008, 5:47am UTC](https://community.unix.com/t/extracting-integer-from-data/182228/4 "2008-01-18T05:47:29Z")

</div>

```nohighlight
sed -e 's/\(^.*:\$\)\(.*\)\(:.*$\)/\2/g' datafile

```

---

<div class="post-metadata">

**Author:** ![Franklin52](https://community.unix.com/user_avatar/community.unix.com/franklin52/32/500_2.png) [@Franklin52](https://community.unix.com/u/Franklin52)\
**Post date:** [January 18, 2008, 7:18am UTC](https://community.unix.com/t/extracting-integer-from-data/182228/5 "2008-01-18T07:18:11Z")

</div>

> [@fpmurphy](#):
>
> ```plaintext
> sed -e 's/\(^.*:\$\)\(.*\)\(:.*$\)/\2/g' datafile
> 
> ```

This suffice:

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

```

Regards
