LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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-08-2009, 05:26 AM   #1
c_mitulescu
Member
 
Registered: Nov 2003
Location: London
Distribution: Ubuntu
Posts: 35

Rep: Reputation: 15
egrep with multiple regex conditions


Hi,

I am trying to write a bash script that reads through a lot of xml like files and tries to extract certain values.

I am currently stuck trying to pipe the output from the files through egrep where I have three regex conditions that I would like to check for.

For example I want egrep to output the following values if encountered

<OTDataID>15364515
<OTName>IMG_2350.JPG
<OTDataSize>1200026

The three regex conditions

regexp1='<OTDataID>'[0-9]*
regexp2='<OTName>'.*
regexp3='<OTDataSize>'[0-9]*

For some reason no matter how I try to fomulate multiple conditions in egrep it fails with an error or does not return anything.

This works
egrep $regexp1

This fails as it interprets the "|" as a pipe
egrep $regexp1|$regexp2|$regexp3

These do not return anything
egrep -e '($regexp1|$regexp2|$regexp3)'
egrep -E '($regexp1|$regexp2|$regexp3)'

And neither does this

egrep {$regexp1,$regexp2,$regexp2}

returns:
egrep: <OTName>.*: No such file or directory
egrep: <OTName>.*: No such file or directory


This does not return anything:
egrep -E '{$regexp1,$regexp2,$regexp2}'


Any help would be greatly appreciated.
 
Old 07-08-2009, 05:46 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware
Posts: 299

Rep: Reputation: 50
Quote:
Originally Posted by c_mitulescu View Post
Hi,

I am trying to write a bash script that reads through a lot of xml like files and tries to extract certain values.

I am currently stuck trying to pipe the output from the files through egrep where I have three regex conditions that I would like to check for.

For example I want egrep to output the following values if encountered

<OTDataID>15364515
<OTName>IMG_2350.JPG
<OTDataSize>1200026

The three regex conditions

regexp1='<OTDataID>'[0-9]*
regexp2='<OTName>'.*
regexp3='<OTDataSize>'[0-9]*

For some reason no matter how I try to fomulate multiple conditions in egrep it fails with an error or does not return anything.

This works
egrep $regexp1

This fails as it interprets the "|" as a pipe
egrep $regexp1|$regexp2|$regexp3
I don't know about egrep. But this should work with grep:

grep '<OTDataID>[0-9]\|<OTName>.\|<OTDataSize>[0-9]' your_xml_file
 
Old 07-08-2009, 05:53 AM   #3
c_mitulescu
Member
 
Registered: Nov 2003
Location: London
Distribution: Ubuntu
Posts: 35

Original Poster
Rep: Reputation: 15
That did it. Thank you! I needed to escape the pipe from "|" to "\|"
 
  


Reply

Tags
egrep, multiple, regex


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
Syntax error for multiple if conditions predatorz Programming 2 01-29-2009 02:44 PM
Multiple If conditions keysorsoze Programming 4 12-12-2007 02:07 AM
excel count with multiple conditions mfrick Programming 11 05-23-2006 09:21 PM
egrep/grep regex question buldir Programming 7 05-08-2006 08:17 PM
egrep multiple files smart_sagittari Linux - Newbie 2 06-27-2005 09:57 AM


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

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