LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-13-2009, 02:29 AM   #1
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Rep: Reputation: 15
About trimming a variable


Hi,
When I studied variable or parameter expansions, I learned the following:

${variable%pattern} trim the shortest match from the end
${variable%%pattern} trim the longest match from the end
I couldn't figure out the difference between them.

Code:
#var1=1100
#echo ${var1%0}
110
#echo ${var1%%0}
110
Thanks in advance.
 
Old 07-13-2009, 02:59 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,036

Rep: Reputation: 96
Try:

echo ${var1%0*}

echo ${var1%%0*}

Last edited by Disillusionist; 07-13-2009 at 03:07 AM.
 
Old 07-13-2009, 06:08 AM   #3
ahmedb72
Member
 
Registered: Jan 2006
Location: Sydney
Distribution: RHEL
Posts: 72

Original Poster
Rep: Reputation: 15
Hi Disillusionist,
Your code example illustrated for me the difference but it violated the trimming purpose. I simply want to trim the leading zeros. eg: 1100 -> 11 , 210 -> 21

Code:
#var1=11001
#echo ${var1%0*}
110                     <--- no trimming
#echo ${var1%%0*}
11
 
Old 07-13-2009, 06:36 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
The wildcard * has the bash meaning, not the regexp one. In parameter substitution
Code:
${var%%0*}
it means any character from zero to the end of the string. In your first example, instead, the pattern matched just one zero, so that using either %% or % the result is the same. If you want to remove any number of trailing zeros you have to activate the extglob option:
Code:
$ var=1100
$ shopt -s extglob; echo ${var%%+(0)}; shopt -u extglob
See man bash under the section pathname expansion --> pattern matching for details.
 
  


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
trimming perl array elements homey Programming 7 02-17-2008 04:48 PM
Trimming a installed kernel fibster Slackware 3 11-01-2006 06:47 PM
QCad - trimming lines mjjzf Linux - Software 1 09-15-2006 04:16 AM
Trimming down a Suse 9.0 install Bradless Linux - Distributions 0 08-02-2004 05:36 PM
Trimming a file james_cwy Linux - Newbie 4 01-13-2004 11:08 PM


All times are GMT -5. The time now is 02:45 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration