LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 01-29-2007, 03:51 AM   #1
maxvonseibold
Member
 
Registered: Mar 2006
Location: Oxford, UK.
Distribution: Debian, Ubuntu, RHEL5, OSX
Posts: 41

Rep: Reputation: 15
Angry Matching values in a bash script grep, regex's ... ?


Hello,

Firstly yes I could do this in Perl. However I am trying to only use Bash...

Should be simple ...


I have a list of variables in an array which follow the format NUMBER_NAME (i.e. Number Underscore Name).

I am trying to get just the number and the underscore and assign it to a variable.


I have tried this:

NEWNAME=$(echo ${anarray[$a]} | grep [0-9]_ ${anarray[$a]})

Here ${anarray[$a]} holds the initial variable with number_name.




This:

#NEWNAME=$(echo grep '[0-9]_' "${anarray[$a]}")

and this:

grep '[0-9]_' "${anarray[$a]}" > NEWNAME


As you can see I am a bit confused.

In Perl I would simply do a little regex and catch what I wanted in a $N and then swap things around, SIMPLE! I can't figure this one out. I know what I want, just not the mechanics. Any ideas?
 
Old 01-29-2007, 04:18 AM   #2
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Try 'grep -o [0-9]_' or "sed -e 's/\([0-9]_\).*/\1/'"
 
Old 01-29-2007, 05:02 AM   #3
maxvonseibold
Member
 
Registered: Mar 2006
Location: Oxford, UK.
Distribution: Debian, Ubuntu, RHEL5, OSX
Posts: 41

Original Poster
Rep: Reputation: 15
Hmmmm, ok. I'm trying your grep suggestion.


NEWNAME=$(echo grep -o [0-9]_ ${anarray[$a]})


Any ideas why this does not work?

I have tried various combinations of "" and '' etc. but I do not quite understand. Basically I want the matched Number and Underscore assigned to NEWNAME...

When I use quotes it just outputs grep -o etc. If I leave them out it says Command Not Found....


Thankyou for your help so far.



Max.
 
Old 01-29-2007, 05:20 AM   #4
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
Why echo? You need only grep!
 
Old 01-29-2007, 05:54 AM   #5
maxvonseibold
Member
 
Registered: Mar 2006
Location: Oxford, UK.
Distribution: Debian, Ubuntu, RHEL5, OSX
Posts: 41

Original Poster
Rep: Reputation: 15
Sorry, I thought you needed echo.


Ok


NEWNAME=$(grep -o [0-9]_ ${anarray[$a]})


Still nothing. All I get is

"grep: 1_Valuename: No such file or directory"



Any further clues ...
 
Old 01-29-2007, 06:04 AM   #6
raskin
Senior Member
 
Registered: Sep 2005
Location: France
Distribution: approximately NixOS (http://nixos.org)
Posts: 1,900

Rep: Reputation: 69
You could use either
echo ${anarray[$a]} | grep -o '[0-9]_'
or
grep -o '[0-9]_' <<< ${anarray[$a]}
 
Old 01-29-2007, 06:07 AM   #7
maxvonseibold
Member
 
Registered: Mar 2006
Location: Oxford, UK.
Distribution: Debian, Ubuntu, RHEL5, OSX
Posts: 41

Original Poster
Rep: Reputation: 15



Worked!


NEWNAME=$(echo ${anarray[$a]} | grep -o '[0-9][0-9]_')



Thankyou! I really appreciate it.



Max.
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
bash script pattern matching thedude2010 Programming 9 06-02-2006 02:39 AM
BASH Script: variable values referencing for console arguments sadarax Programming 1 11-14-2005 05:23 PM
bash script and grep syros Programming 4 01-13-2005 03:04 PM
Pattern Matching Help in Bash script cmfarley19 Programming 1 04-07-2004 09:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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