LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-10-2008, 10:17 AM   #1
DaneelGiskard
LQ Newbie
 
Registered: Apr 2007
Posts: 24

Rep: Reputation: 15
Question on Bash parameter extension


Hi all,

I'm trying to use the bash "parameter extension" mechanism to extract a substring which matches a certain pattern from a string.

So far I'm succesfully using the following in a bash script:

Code:
test='this is my example video with a resolution of 400x500 pixel.'
echo ${test//???x???/}
This will remove the '400x500' substring from the test string and output the rest.

What I want to achieve is exactly the opposite: I want to remove everything _except_ the '400x500' substring, thus using regular expressions to extract a specific substring from a string in a bash script.

Is such a "negation" possible in the above script fragment? Is there a better way to extract a substring which matches a specific regex pattern from a string in bash?

Many thanks,
Michael
 
Old 01-10-2008, 11:35 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Regular expressions are not supported that way in bash. It is probably possible to do it in pure bash, but it will become complex AFAICS.

Using sed it is quite easy, but not "pure bash" (if that is what you asking):
Code:
echo $test | sed -n 's/.* \([0-9]*x[0-9]*\) .*/\1/p'

Last edited by Hko; 01-10-2008 at 11:37 AM.
 
Old 01-10-2008, 11:48 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Anyway, doing it "pure bash" is possible. But a bit more work.
Code:
test='this is my example video with a resolution of 400x500 pixel.'
head=${test%%x[0-9]*}
tail=${test##*[0-9]x}
X_res=${head##* }
Y_res=${tail%% *}
res_string=${X_res}x${Y_res}
echo $res_string

Last edited by Hko; 01-10-2008 at 11:51 AM.
 
Old 01-10-2008, 12:09 PM   #4
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
Firstly, DaneelGiskard, your script doesn't work. It doesn't only strip out the '400x500' substring, it strips out any 'x' and the 3 characters either side of it. It should be something like:
Code:
echo ${test//[0-9][0-9][0-9]x[0-9][0-9][0-9]/}
I agree with Hko, string manipulation easily becomes over-complicated in bash, it's better to use tools that are designed for the job. Here's the simplest version I could come up with using pure bash:
Code:
x=${test//${test%%[0-9][0-9][0-9]x[0-9][0-9][0-9]*}/}; echo ${x:0:7}
 
Old 01-10-2008, 01:37 PM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Quote:
Originally Posted by jozyba View Post
Here's the simplest version I could come up with using pure bash:
Code:
x=${test//${test%%[0-9][0-9][0-9]x[0-9][0-9][0-9]*}/}; echo ${x:0:7}
This does not work correctly if the resolution has different length, e.g. 1024x768 or 50x40.

Neither does the original code from DaneelGiskard by the way. It will not remove, say, "1024x768" entirely.

Last edited by Hko; 01-10-2008 at 01:40 PM.
 
Old 01-10-2008, 02:06 PM   #6
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
Yes, you're right. This should work...
Code:
x=${test//${test%%[0-9]*x[0-9]*}/}; echo ${x// */}
 
Old 01-14-2008, 06:15 AM   #7
DaneelGiskard
LQ Newbie
 
Registered: Apr 2007
Posts: 24

Original Poster
Rep: Reputation: 15
Many thanks, the sed version works beautifully for me.

All the best,
Michael
 
  


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
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
bash parameter indirection possible? kornelix Programming 5 11-30-2005 08:35 AM
setting up scp alias under bash - parameter passing problem gjr Linux - Newbie 4 06-08-2005 06:38 AM
bash: pass a parameter to xview nimra Programming 2 06-28-2004 09:18 PM
Bash function parameter Misel Programming 2 05-17-2003 11:51 AM

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

All times are GMT -5. The time now is 06:51 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