LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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: 332

Rep: Reputation: 6
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,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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: 332

Original Poster
Rep: Reputation: 6
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
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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,453

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
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: 332

Original Poster
Rep: Reputation: 6
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: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
[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,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:31 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration