LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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
 
LinkBack Search this Thread
Old 03-22-2011, 05:29 PM   #1
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,242

Rep: Reputation: 48
Bash parameter substitution question


Let's say I have a following string in a variable called LONG_NAME:
Code:
ata-WDC_WD3200AAJB-00J3A0_WD-WCAV2Y020369 wwn-0x50014ee103971beb
Now I want to simply take out everything in the red color above.

I have been trying the following command but it does not work:
Code:
echo "${LONG_NAME%%_[0-9]*}"
Basically, I am trying to remove anything after the second underscore (including).

Last edited by kushalkoolwal; 03-22-2011 at 05:35 PM.
 
Old 03-22-2011, 06:25 PM   #2
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,485

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
Code:
jlinkels@donald_pc:~$ echo ${LONG_STRING%_[A-Z0-9]*}
ata-WDC_WD3200AAJB-00J3A0
Provided there are no more underscores in the part that you want to remove. The trick is to match the shortest string (not the longest) so you can be more specific.

jlinkels

Last edited by jlinkels; 03-22-2011 at 06:28 PM.
 
1 members found this post helpful.
Old 03-22-2011, 06:31 PM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian
Posts: 1,421

Rep: Reputation: 360Reputation: 360Reputation: 360Reputation: 360
It looks like maybe you thought ${var%%pat} expansion uses regex, but actually the pattern is a glob, see Shell Parameter Expansion

Code:
${LONG_NAME%_*}
would remove everything after the last underscore.
 
1 members found this post helpful.
Old 03-22-2011, 07:33 PM   #4
kurumi
Member
 
Registered: Apr 2010
Posts: 217

Rep: Reputation: 45
Code:
$ s="ata-WDC_WD3200AAJB-00J3A0_WD-WCAV2Y020369 wwn-0x50014ee103971beb"
$ echo $s| ruby -e 'puts gets.split(/_/)[0,2].join("_")'
ata-WDC_WD3200AAJB-00J3A0
 
Old 03-29-2011, 12:16 PM   #5
kushalkoolwal
Senior Member
 
Registered: Feb 2004
Location: Middle of nowhere
Distribution: Debian Squeeze
Posts: 1,242

Original Poster
Rep: Reputation: 48
Thank you all. All the suggestions were good.
 
Old 03-29-2011, 08:36 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,402

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
Don't forget to mark as SOLVED once you have a solution
 
  


Reply

Tags
bash, expansion


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Requested with bash Parameter Substitution haleba Linux - Software 3 08-23-2010 01:37 PM
[SOLVED] bash variable substitution Jerry Mcguire Programming 6 04-29-2010 09:33 AM
Question on Bash parameter extension DaneelGiskard Programming 6 01-14-2008 06:15 AM
linux bash - how to use a dynamic parameter in shell parameter expansion expression nickleus Linux - General 2 08-21-2006 04:54 AM
Bash variable substitution daYz Programming 3 04-14-2006 01:16 PM


All times are GMT -5. The time now is 03:04 PM.

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