LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 04-25-2011, 11:25 PM   #1
Wolfjurgen
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Rep: Reputation: 0
Bash script question, searching in a variable, displaying search results...


Hey Guys,

I'm trying to figure out how I can search within a variable and assign the results to a new variable. I'll use the following as an example -

cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=`echo ${cars} | egrep -o '\<A?+|\<C+'`

with the echo command I get the following output assigned to list -
A
C
C

What I'd like to get for output is -
Audi
Cadillac
Chevy

Anyone have an idea how I can accomplish this? Also, any tips on how I could do this regardless of upper/lower case letters?
 
Old 04-26-2011, 12:51 AM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Your bash syntax is just fine, although $(..) is recommended over `..`.

The problem is with your grep regex. It's not matching what you want it to. "C+", for example, matches a variable-length string of C's, and only C's.

Code:
cars="Audi BMW Cadillac Chevy Dodge Ferrari Ford Mercedes"
list=$( echo ${cars} | egrep -io '\<[AC][^[:space:]]+[[:space:]]' )
Notice first of all how you can combine everything into a single expression. This one matches a word-beginning space, followed by an A or C, followed by one or more non-space characters, followed by a space.

[^x]+x (match anything not-x until you match an x) is a common pattern used for getting around regex greediness, as * and + will keep going until they find the longest possible match of the expression they effect. If you simply use .* or .+, it will match everything to the end of the text.

I also used the [:space:] character class, which matches space, tab, newline, and a few other rarer characters (read info grep), but if you know what the input will have, you can use those directly instead.

For case insensitivity, use grep's -i option.

Last edited by David the H.; 04-26-2011 at 12:59 AM. Reason: minor addition
 
1 members found this post helpful.
Old 04-26-2011, 09:22 AM   #3
Wolfjurgen
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
David the H. that is exactly what I was looking for!

I've got my script doing exactly what I want now. You sir are a scholar and a gentleman. Also, thanks for the BashFAQ link, good stuff.
 
Old 04-26-2011, 12:04 PM   #4
Wolfjurgen
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
For anyone else who may find this useful, I had to tweak the egrep command to find the last matching item in a variable.

Consider the following -

~$ cars="Acura Audi BMW Buick Cadillac Chevrolet Chrysler Dodge Saab Saturn Subaru Suzuki"

I'll search for anything that starts with the letters "a", "d" or "s" in $cars-
~$ echo ${cars} | egrep -io '\<[ads][^[:space:]]+[[:space:]]'
Acura
Audi
Dodge
Saab
Saturn
Subaru

NOTICE how "Suzuki" is not listed in the output. In order to get the last matching item in the variable, do -

~$ echo ${cars} | egrep -io '\<[ads][^[:space:]]+[[:space:]]*'
Acura
Audi
Dodge
Saab
Saturn
Subaru
Suzuki

All I had to do was add an "*" at the end of the egrep command. Many thanks to David the H. once again!
 
Old 04-26-2011, 12:11 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Actually, what's the point of the last "[[:space:]]"? The "[^[:space:]]+" part will not match beyond the end of the word anyway.
 
Old 04-26-2011, 12:29 PM   #6
Wolfjurgen
LQ Newbie
 
Registered: Apr 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Oh, cool. Thanks for pointing that out MTK358.
 
  


Reply

Tags
regular expression



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
bash search for a pattern within a string variable nutthick Programming 8 03-17-2015 07:26 PM
question regarding bash script variable mrmnemo Linux - Desktop 4 03-22-2010 12:55 PM
Simple variable bash script help/question lilrazzzzz Programming 4 04-29-2009 05:50 PM
Grab results of GRUB's find command into a shell script variable kushalkoolwal Programming 6 02-03-2009 05:04 PM
I did a search bash function but it get less results then find nadavvin Programming 1 09-14-2006 01:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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