LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 08-04-2008, 06:15 AM   #1
Swapna173
LQ Newbie
 
Registered: Dec 2007
Posts: 25

Rep: Reputation: 15
String Substitution


HI Guys,

I get File Names in as I or U as Suffix at the End of a File Name and I wanted to retrieve that I or U from the File name and run the processes acordingly.

I got the solution to this as well.
For Ex:

a="US_SALES_08_2008_I"
b=`echo ${a##*_}`

the variable b displays I in this case. But i wanted to know the role that # and * plays in the command.

Can i get information on any site from whether i can learn the shortcuts and more information on the linux commands as well.

Thanks in advance,
Swapna
 
Old 08-04-2008, 06:22 AM   #2
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
This is an example of parameter substitution. The syntax here is
Code:
${var##Pattern}
which means remove from $var the longest part of $Pattern that matches the front end of $var.
In a pattern the asterisk means any sequence of characters (zero included). In your example all the characters before the last underscore and the underscore itself are removed, leaving I or U as required.

You can find a detailed description of parameter substitution, along with a lot of examples, in the Advanced Bash Scripting Guide, here. A pdf version of this document is also available.

Last edited by colucix; 08-04-2008 at 06:23 AM.
 
  


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
Trying to change String using sed with a string \/home\/user\/Desktop icecoolcorey Programming 10 06-12-2008 11:32 PM
Substitution of a long string horacioemilio Programming 5 04-07-2008 09:21 PM
Complicated string substitution horacioemilio Programming 5 02-13-2008 06:40 PM
Shell Script: Delete lines til string found or until particular string. bhargav_crd Linux - General 3 12-20-2007 11:14 PM
Rewrite rule with query string in the pattern string basahkuyup Linux - Newbie 2 10-17-2006 02:06 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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