LinuxQuestions.org
Review your favorite Linux distribution.
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 03-10-2011, 10:51 AM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Rep: Reputation: 16
get part of a string in bash


Hi,

I've a string "this.is.a.name", and I would like to return "is.a.name"

How can I do that in bash?
 
Old 03-10-2011, 11:00 AM   #2
xeon123
Member
 
Registered: Sep 2006
Posts: 374

Original Poster
Rep: Reputation: 16
I think I've got it:

Code:
echo `expr index "stringZ.abc.dfr" .
 
Old 03-10-2011, 11:03 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387Reputation: 2387
Hi,

Using bash internals:
Code:
echo $(varname#*.}
And the other way around (this.is.a):
Code:
echo ${varname%.*}
This uses Parameter extensions (3.5.3 Shell Parameter Expansion)

Hope this helps.
 
Old 03-10-2011, 11:04 AM   #4
sycamorex
LQ Veteran
 
Registered: Nov 2005
Location: London
Distribution: Slackware64-current
Posts: 5,811
Blog Entries: 1

Rep: Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191Reputation: 1191
You could do:

Based on length:
Code:
var="this.is.a.name"

echo ${var:5}
 
  


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
[SOLVED] extract part of string himu3118 Programming 4 05-07-2010 08:13 AM
[SOLVED] C - How to put a specific arbitrary part of a string into it's own string? golmschenk Programming 9 04-19-2010 09:27 PM
extracting part of a string (sed, bash, ...) rama76 Programming 3 01-11-2008 09:14 AM
remove part of string in bash script crewblunts Programming 2 03-16-2006 06:54 PM
Simple bash script help, grabbing part of a string colabus Linux - Newbie 3 04-25-2005 10:42 AM


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