LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-07-2013, 08:25 AM   #1
Garrett85
Member
 
Registered: Jan 2011
Posts: 87

Rep: Reputation: 0
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.
 
Old 01-07-2013, 08:44 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,135

Rep: Reputation: 230Reputation: 230Reputation: 230
Code:
NUM=123456789
DIGITS=3
echo ${NUM: -$DIGITS}
 
3 members found this post helpful.
Old 01-07-2013, 11:13 AM   #3
Garrett85
Member
 
Registered: Jan 2011
Posts: 87

Original Poster
Rep: Reputation: 0
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 View Post
Code:
NUM=123456789
DIGITS=3
echo ${NUM: -$DIGITS}
 
Old 01-07-2013, 11:15 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,492

Rep: Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956Reputation: 1956
Quote:
Originally Posted by Garrett85 View Post
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?
 
Old 01-07-2013, 11:20 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,135

Rep: Reputation: 230Reputation: 230Reputation: 230
Not sure if I understand, but

Code:
echo ${NUM: 0: -$DIGITS}
will give you the string without the last 3 characters.
 
1 members found this post helpful.
Old 01-07-2013, 12:51 PM   #6
Garrett85
Member
 
Registered: Jan 2011
Posts: 87

Original Poster
Rep: Reputation: 0
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 View Post
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?

Last edited by Garrett85; 01-07-2013 at 12:59 PM. Reason: Solved myself
 
Old 01-07-2013, 02:33 PM   #7
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Ubuntu
Posts: 1,084

Rep: Reputation: 287Reputation: 287Reputation: 287
[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
 
1 members found this post helpful.
Old 01-07-2013, 09:34 PM   #8
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by Garrett85 View Post
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})}
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH: string manipulation rhuhawk Programming 9 09-21-2011 11:05 AM
BASH string manipulation help ptml Linux - Newbie 8 08-26-2010 06:38 PM
String manipulation in bash dalmat Linux - Software 4 12-03-2007 05:50 PM
string manipulation in BASH ovince Programming 4 04-16-2007 07:15 PM
bash + string manipulation dave bean Programming 7 02-16-2005 11:16 AM


All times are GMT -5. The time now is 03:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration