LinuxQuestions.org
Register a domain and help support LQ
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-20-2011, 06:03 PM   #1
xeon123
Member
 
Registered: Sep 2006
Posts: 374

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


Hi,

I would like to return the last part of a string in an array of strings in bash.

The array contains in each position the content below:
Code:
a.b.c
a.d.f
a
a.d
a.b.c.h
and I would like to return only the last part of each string.
The correct result would be:

Code:
c
f
a
d
h
How can I do that in bash?
 
Old 03-20-2011, 06:29 PM   #2
Robhogg
Member
 
Registered: Sep 2004
Location: Old York, North Yorks.
Distribution: Debian 7 (mainly)
Posts: 653

Rep: Reputation: 88
This could be achieved using Bash's built-in Parameter Expansion feature:

Quote:
${parameter#word}
${parameter##word}
The word is expanded to produce a pattern just as in filename expansion (see Filename Expansion). If the pattern matches the beginning of the expanded value of parameter, then the result of the expansion is the expanded value of parameter with the shortest matching pattern (the # case) or the longest matching pattern (the ## case) deleted. If parameter is @ or *, the pattern removal operation is applied to each positional parameter in turn, and the expansion is the resultant list. If parameter is an array variable subscripted with @ or *, the pattern removal operation is applied to each member of the array in turn, and the expansion is the resultant list.
Hope this helps,
Rob
 
Old 03-20-2011, 08:47 PM   #3
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 45
Code:
$ ruby -F"\." -ane 'puts $F[-1]' file
c
f
a
d
h
 
Old 03-20-2011, 09:14 PM   #4
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi

In bash

Code:
#!/bin/bash
A=(a.b.c a.d.f a a.d a.b.c.h )
for w in ${A[@]}
do
    echo ${w##*.}
done
In bash using awk:

Code:
#!/bin/bash
A=(a.b.c a.d.f a a.d a.b.c.h )
for w in ${A[@]}
do
    echo $w
done | awk -F. '{print $NF}'
 
Old 03-20-2011, 09:40 PM   #5
anishkumarv
Member
 
Registered: Feb 2010
Location: chennai - India
Distribution: centos
Posts: 289

Rep: Reputation: 10
Hi

Try this also

awk -F. '{$0=$NF}1' filename
 
Old 03-20-2011, 10:08 PM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,255

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
As an addendum to post #4 using bash:
Code:
#!/bin/bash
A=(a.b.c a.d.f a a.d a.b.c.h )
for w in ${A[@]}
do
    echo ${w:(-1)}
    echo ${w##*.}
done
Both will print the last character
 
  


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] get part of a string in bash xeon123 Linux - Newbie 3 03-10-2011 11:04 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 09:26 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration