return part of a string in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting return part of a string in bash
# 1  
Old 03-20-2011
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:

Quote:
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:

Quote:
c
f
a
d
h
How can I do that in bash using AWK?

Thanks and Regards
Anish Kumar.V
# 2  
Old 03-20-2011
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}'

# 3  
Old 03-20-2011
Code:
awk -F \. '{print $NF}' infile

This User Gave Thanks to rdcwayx For This Post:
# 4  
Old 03-20-2011
Code:
while read line;do echo ${line##*.};done < file

or using awk
Code:
awk -F. '{$0=$NF}1' file

This User Gave Thanks to danmero For This Post:
# 5  
Old 03-20-2011
Hi dude!!

Thanks for your reply, Is it possible to get the output from file instead of directly declaring

Code:
A=(a.b.c a.d.f a a.d a.b.c.h )

like this

A= /root/filename

With Regards
Anish Kumar.V
# 6  
Old 03-20-2011
Quote:
Originally Posted by anishkumarv
Hi dude!!

Thanks for your reply, Is it possible to get the output from file instead of directly declaring
Code:
#!/bin/bash
A=( $(<file) )
for w in ${A[@]}
do
    echo ${w##*.}
done

This User Gave Thanks to danmero For This Post:
# 7  
Old 03-20-2011
Hi all

Thanks for all your replies It works like charm!!! :-)

With Regards
Anish Kumar.V
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

bash - Validating return code 0

Hi All, I am trying a script out that will startup on one of my servers. i wanted to check for RC 0 and if it didnt check out, exit. Typo- This is BASH (Redhat) This isnt working, and this is the best way to do error checking I feel. Heres my erorr ./start: line 25: syntax error near... (2 Replies)
Discussion started by: jeffs42885
2 Replies

2. Shell Programming and Scripting

Expect in bash to get the return value

cat test.sh #!/bin/sh expect <<- EOF set timeout 5 spawn ssh -o StrictHostKeyChecking=no lyang0@128.224.178.245 -C mkdir -p /tmp expect { "Password:" {send "root\r"} } spawn scp -o StrictHostKeyChecking=no /tmp/1 lyang0@128.224.178.245:/tmp/ ... (1 Reply)
Discussion started by: yanglei_fage
1 Replies

3. Shell Programming and Scripting

Use double quotes as part of the string in a Bash array

So I need to create an array that has " in the string of the text: string = ( "value 1" "value2" where the actual string is "value1" with the quotations included would this work? string = ( \"value1\" \"value\") and if the strings contain spaces as well: string = ("\"this... (4 Replies)
Discussion started by: os2mac
4 Replies

4. Shell Programming and Scripting

bash: need to have egrep to return a text string if the search pattern has NOT been found

Hello all, after spending hours of searching the web I decided to create an account here. This is my first post and I hope one of the experts can help. I need to resolve a grep / sed / xargs / awk problem. My input file is just like this: ----------------------------------... (6 Replies)
Discussion started by: bash4ever
6 Replies

5. Shell Programming and Scripting

Deleting part of a string : string manipulation

i have something like this... echo "teCertificateId" | awk -F'Id' '{ print $1 }' | awk -F'te' '{ print $2 }' Certifica the awk should remove 'te' only if it is present at the start of the string.. anywhere else it should ignore it. expected output is Certificate (7 Replies)
Discussion started by: vivek d r
7 Replies

6. Shell Programming and Scripting

Bash - multiple line carriage return

Hello! I have one strange question - let's say I have a long, multiple-line string displayed on the terminal using echo, and I would like to make a carriage return to the beginning of this string, no to the beginning of the last line - is something like that possible? I would like to be able to... (1 Reply)
Discussion started by: xqwzts
1 Replies

7. Shell Programming and Scripting

Return part of string after X numbers of a character

My input strings look something like this: /dev/vs/dsk/group/vol I want to print just "group" out of the line... which is always found between the 4th and 5th "/" in the string. I figure I have to use awk, sed, or some combination to do this, but I've searched the forums and can't find... (2 Replies)
Discussion started by: ltricarico
2 Replies

8. Shell Programming and Scripting

How to return a string?

function blah { return "string" } it keeps saying string: not found How can i do this guys? Because I'm trying to do something like this function print_daemon_options { echo "Start Daemons - Please enter one or a combination of the following:" if isDatasubEnabled &&... (11 Replies)
Discussion started by: pyscho
11 Replies

9. Shell Programming and Scripting

How to enter a return key in bash script?

Hi, I'm porting an install script from AIX to Red Hat (2.6.18-164.el5 #1 SMP) I have this script working in both AIX and HP-UX. The script is a wrapper for a Micro Focus Server Express install program. It responds to the install program questions with a here-now list. Responses includes... (14 Replies)
Discussion started by: duker61
14 Replies

10. Shell Programming and Scripting

Return name of running WM in bash?

While I am by habit comfortable with bash as a shell, from time to time, I find tiny reasons to believe it's a little dense. One of the things that proves it is: to date, I have not been able to get it to identify what WM it's running inside of or alongside of in an X11 kind of environment... (1 Reply)
Discussion started by: SilversleevesX
1 Replies
Login or Register to Ask a Question