LinuxQuestions.org
Review your favorite Linux distribution.
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 05-25-2005, 02:21 PM   #1
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Rep: Reputation: 30
Quick regex problem, can't find solution


I've been playing around with a few regular expressions for half an hour now and I'm stumped on what to do. Here's the string I want to apply the regex to:

./crafty_base.gnu3 < crafty.in > crafty.out 2> crafty.err

What I want to do is extract everything between the <, > and 2>. I've managed to grab everything but crafty.out now. Here's my bash script:

Code:
# exec_target contains the initial string I want to perform the regex on
exec_in=$echo $(echo exec_target | sed 's/.*<//g') | sed 's/>.*//g')
exec_out=$(echo exec_target | sed 's/.*?^2>//g')
exec_err=$(echo exec_target | sed 's/.*2>//g')
echo "TARGET: $exec_target"
echo "INPUT:    $exec_input"
echo "OUTPUT: $exec_output"
echo "ERROR: $exec_err"
And the output of this script:

Code:
TARGET: ./crafty_base.gnu3
INPUT: crafty.in
OUTPUT: ./crafty_base.gnu3 < crafty.in > crafty.out 2> crafty.err
ERROR: crafty.err

What I want to tell sed is "eat up everything that occurs before the first >", but no matter what I've tried it always eats up everything before the last ">". I tried to fix this by saying that the 2 character can not preceed the > but that doesn't seem to be working. And setting .* to be ungreedy causes nothing to be eaten at all. I've tried googling, reading the sed manpage, and I just can't find a solution. Does anyone know how I can do this?


Oh and FYI: I can't do something like use the < for solving this problem, because I'm using these regexes for multiple strings, all of which <, > and 2> are completely optional.
 
Old 05-25-2005, 02:35 PM   #2
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
Don't really know sed, but isn't this how you eliminate
a particular char (so it can't precede)?

Code:
[~2]

So something like:
Code:
s/.?*[~2]>//g


EDIT:
I'm used to perl compatible re's so if that doesn't work
with sed, you could maybe just switch it to a perl line
in your script instead of sed.

Last edited by towlie; 05-25-2005 at 02:41 PM.
 
Old 05-25-2005, 02:51 PM   #3
R00ts
Member
 
Registered: Mar 2004
Location: Austin TX, USA
Distribution: Ubuntu 11.10, Fedora 16
Posts: 547

Original Poster
Rep: Reputation: 30
Yes, sed is perl compatible. I've never seen ~ used as the negation operator though, I always thought it was ^


Well good news: that helped me figure out what was wrong. I needed [ ] around ^2. I think ^ is also the match beginning of line operator so maybe the interpreters needs it to be in square brackets for it to be interpreted as negation. This is the fix for output:

Code:
exec_out=$(echo $(echo $exec_target | sed 's/.*[^2]>//g') | sed 's/2>.*//g')
 
Old 05-25-2005, 02:55 PM   #4
towlie
Member
 
Registered: Apr 2004
Location: U.S.
Distribution: slackware 10.0
Posts: 110

Rep: Reputation: 15
What the, hmmm, yeah you're so right. The ^ is
the negation op.

What was I thinking with ~?

Been a while with me for regexes
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
another mplayer sound problem. cant find pertinent solution. roller24 Linux - General 3 11-02-2005 03:07 PM
Solution to Quick Fix MadnessASAP Linux - Software 1 06-03-2005 08:08 PM
Quick backup solution... new@linux Linux - Newbie 1 12-08-2004 04:54 PM
Please help me find a solution...I've tried for days. (network problem) RoaCh Of DisCor Linux - Newbie 12 11-19-2004 06:54 AM
quick fstab solution louie055 Linux - Newbie 1 09-01-2003 02:00 PM

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

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