LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   bash string manipulation (https://www.linuxquestions.org/questions/programming-9/bash-string-manipulation-4175444502/)

Garrett85 01-07-2013 08:25 AM

bash string manipulation
 
echo ${NUM:0:2}

The above is just an example of the kind of code I'M using. I need a way to represent the end of the string. Is there a way to start from the right side of a line of text instead of the left? What I'M trying to do here is grab the last three digits on end, then the next three and so on. NUM in this case will be a user input so there is no way to hard code the end position. Thanks.

Guttorm 01-07-2013 08:44 AM

Code:

NUM=123456789
DIGITS=3
echo ${NUM: -$DIGITS}


Garrett85 01-07-2013 11:13 AM

Thanks, one more thing
 
Thanks. Now I need help on the following.

OLDSTRING=$(( ${NUM}-${NEWSTRING} )) # I need to set OLSTRING to NUM - NEWSTRING

My syntax is all wrong here if someone could give me a hand. Thanks.

Quote:

Originally Posted by Guttorm (Post 4864618)
Code:

NUM=123456789
DIGITS=3
echo ${NUM: -$DIGITS}



colucix 01-07-2013 11:15 AM

Quote:

Originally Posted by Garrett85 (Post 4864734)
OLDSTRING=$(( ${NUM}-${NEWSTRING} )) # I need to set OLSTRING to NUM - NEWSTRING

My syntax is all wrong here if someone could give me a hand. Thanks.

Why do you think the syntax is wrong? It looks correct to me. In the arithmetic operator you can omit the $ sign to evaluate variables, but it's not mandatory. What the problem is exactly?

Guttorm 01-07-2013 11:20 AM

Not sure if I understand, but

Code:

echo ${NUM: 0: -$DIGITS}
will give you the string without the last 3 characters.

Garrett85 01-07-2013 12:51 PM

the problem
 
Nevermind I got it.


NEWSTRING=${NUM: -$DIGITS}
OLDSTRING=${NUM:0:${NEWSTRING}}

Okay, now I see that it is working but with unintended results. I was hoping to set OLDSTRING minues the digits in new string but instead I'M getting OLDSTING - 1. If I were to input 1000 I would want NEWSTRING to hold 000 and old string to hold everything that is left over to the left, in this case, 1. Any ideas?

Code:

for n in ${PROCESSGROUPS};
        do
            DIGITS=3
            echo ${NUM: -$DIGITS}
            NEWSTRING=${NUM: -$DIGITS}
            OLDSTRING=$(( ${NUM}-${NEWSTRING} )) # I need to set OLSTRING to NUM - NEWSTRING
            echo ${OLDSTRING}
        done


Quote:

Originally Posted by colucix (Post 4864736)
Why do you think the syntax is wrong? It looks correct to me. In the arithmetic operator you can omit the $ sign to evaluate variables, but it's not mandatory. What the problem is exactly?


danielbmartin 01-07-2013 02:33 PM

[QUOTE=Garrett85;4864801]
Quote:

If I were to input 1000 I would want NEWSTRING to hold 000 and old string to hold everything that is left over to the left, in this case, 1.
This may help...
Code:
Code:

num=1234
echo; echo "num=" $num
let remainder=($num/1000)
echo; echo "remainder=" $remainder
let modulus=($num%1000)
echo; echo "modulus=" $modulus

Test results:
Code:

num= 1234

remainder= 1

modulus= 234

Daniel B. Martin

konsolebox 01-07-2013 09:34 PM

Quote:

Originally Posted by Garrett85 (Post 4864801)
If I were to input 1000 I would want NEWSTRING to hold 000 and old string to hold everything that is left over to the left, in this case, 1. Any ideas?

I think Guttorm already gave the idea:
Code:

NEWSTRING=${NUM:(-3)}

OLDSTRING=${NUM:0:(-3)}
# or
OLDSTRING=${NUM:0:(-${#NEWSTRING})}



All times are GMT -5. The time now is 11:28 PM.