LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   bash script to preserve only first digits (https://www.linuxquestions.org/questions/linux-software-2/bash-script-to-preserve-only-first-digits-635266/)

alenD 04-14-2008 10:06 AM

bash script to preserve only first digits
 
Hello

I have a big number - i am looking for a bash script to preserve only first 3 digits of that number (basically, to have another number that consists only of first 3 digits of the original number)

thanks in advance

H_TeXMeX_H 04-14-2008 11:00 AM

for example:

Code:

echo '1234567890' | cut -c1-3

shadowsnipes 04-14-2008 11:08 AM

Try using cut -b-3

Example
Code:

echo "12345" | cut -b-3
123


alenD 04-14-2008 11:36 AM

great. thnx

unSpawn 04-14-2008 12:22 PM

s=1234567890; s=${s:0:3}

colucix 04-14-2008 12:24 PM

Merely in bash
Code:

number=1234567890
short_number=${number:0:3}

Edit: moderators are always faster... ;)

unSpawn 04-14-2008 12:51 PM

Yeah, but your answer is more complete in mentioning it only works in BaSH-like shells. BTW, how's the NuFW/Network auth project going?


All times are GMT -5. The time now is 09:46 AM.