LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 07-11-2008, 02:19 AM   #1
djcham
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Rep: Reputation: 0
find word between string


Hello all,

I did a search before I posted but did not find what I needed. I am looking to write a sed/awk/cut/tr command that will parse and print out numbers between two unique strings. for instance

number=3243423 random text blah blah yes no number=323432432 fsd jklf sjl number=32432432

basically I have to grab the number after the = sign after every "number=" string without knowing how long the number will be.

Can somebody give some suggestions on how to do this?

I read that you can use

sed -n '/FOO/,/BAR/p' test.txt

but that only works if it is only one line.... and it is inclusive..any help would be great. thank you!
 
Old 07-11-2008, 02:25 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
string="number=3243423 random text blah blah yes no number=323432432 fsd jklf sjl number=32432432"
echo $string | egrep -o "number=[0-9]*" && echo "you do the rest"
 
Old 07-11-2008, 03:10 AM   #3
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Oops, be careful...

Code:
string="number=3243423 random text blah blah yes no number= fsd jklf sjl number=32432432"
$ echo $string | egrep -o "number=[0-9]*"
number=3243423
number=
number=32432432

# Maybe better to force a digit to match
echo $string | egrep -o "number=[0-9][0-9]*"

# Or, another way, removing number=
echo $string | perl -ne 'print "$1\n" while ($_=~/number=(\d+)/g)'
 
Old 07-11-2008, 03:37 AM   #4
djcham
LQ Newbie
 
Registered: Feb 2005
Posts: 17

Original Poster
Rep: Reputation: 0
great!! thanks for the help. i think this will solve my problem.
 
Old 07-11-2008, 03:44 AM   #5
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by Mr. C. View Post
# Maybe better to force a digit to match
echo $string | egrep -o "number=[0-9][0-9]*"
thanks, good catch
Code:
echo $string | egrep -o "number=[0-9]+"

Last edited by ghostdog74; 07-11-2008 at 03:46 AM.
 
Old 07-11-2008, 04:04 AM   #6
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
It is this both the silly length, and duplication requirement, of the [ ] character classes vs. pcres' \d+, etc. that encourages me to avoid sed/awk much of the time.
 
Old 07-11-2008, 04:23 AM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
later version of sed (-r switch) and gawk have options for extended regular expressions, but not sure about pcre though

Last edited by ghostdog74; 07-11-2008 at 04:27 AM.
 
Old 07-11-2008, 04:26 AM   #8
Mr. C.
Senior Member
 
Registered: Jun 2008
Posts: 2,529

Rep: Reputation: 59
Not mine on NetBSD. I'll have to update my /usr/local copy. Thanks, I wasn't aware of that.
 
Old 07-11-2008, 04:37 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
no problem.
 
  


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
bash scripting capitalizing first letter or each word in a string hypexr Programming 6 06-16-2011 09:58 AM
[SOLVED] find a word in a file, and change a word beneath it ?? vikas027 Programming 10 02-14-2008 09:46 PM
string functions in C to make word case independant? knobby67 Programming 1 03-19-2007 01:13 PM
find a string followed by any word character in bash bryan.out.there Programming 2 07-12-2006 06:36 AM
Read a word in a string with bash orgazmo Programming 6 06-07-2005 10:19 AM


All times are GMT -5. The time now is 11:33 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration