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 06-16-2005, 02:13 AM   #1
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
regex problem with sed


I am working on a configuration file parser, written in a bash script. I am parsing a file where each line is in the form of

[comment][option title][comment]: [option value]

where [comment] is either " *[anything]* " or "". The problem is that egrep will find the lines I need, but sed will not remove the option tag for me.

Here is a simplified version of my script (sorry, no Linux here so I can't test it...):
Code:
#!/bin/bash

beg="^"
end=":"
mandspc="[ 	]*"
cmt="($mandspc\*.*\*$mandspc|)"
error="config_error"

function extract()
{
  tag="$beg$cmt$1$cmt$end$mandspc"
  if [ `egrep -c "$tag" "$file"` -ne 1 ]; then
    echo "$error"
  else
    egrep -h "$tag" "$file" | sed "s/$tag//"
  fi
}

echo "`extract \"extport_fs\"`"
(In case you can't tell, that is a space and a tab in mandspc.)

Here is the part of my config file applicable (file named ext-config):
Code:
* file system for extport *	extport_fs:     ext2
egrep can find the line just fine, however sed will not remove the "file system for extport * extport_fs: " from the line. Can you see anything I'm doing wrong as far as the regex? Is there something besides sed that I can use? It's still in "working" status, therefore I am going to change the style of the lines once I get it working. Thanks.
ta0kira

[EDIT] Made it a little more easy to read...

Last edited by ta0kira; 06-16-2005 at 02:52 AM.
 
Old 06-16-2005, 03:05 AM   #2
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
Are you looking for something like this ?

Code:
$> cat > data
* file system for extport * extport_fs * file system for extport * :     ext2
* file system for extport *     extport_fs:     ext2
* file system for extport * extport_fs * file system for extport * :     ext2

* file system for extport *     extport_fs:     ext2

$> sed 's/\*[a-zA-Z0-9 ]*\*//g' data
 extport_fs  :     ext2
     extport_fs:     ext2
 extport_fs  :     ext2

     extport_fs:     ext2
 
Old 06-16-2005, 03:58 AM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Close, but the problem seems to be with either the .* or the spaces. [a-zA-Z0-9 ]* doesn't do the entire job because I want it to be a true comment; it can contain anything except the comment end delimeter. The big question is is there a difference in how I need to denote space, tab, and "anything besides nothing" between egrep and sed? I tried using /t in place of tab, but that did not work (actually that was with grep, so I'll try with egrep). Maybe I could use [\w\W]* instead of .*? Thanks.
ta0kira

Last edited by ta0kira; 06-16-2005 at 04:01 AM.
 
Old 06-16-2005, 04:16 AM   #4
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
How about this ?

Code:
sed 's/\*.[^\*]*\*//g' data
Hope this solves your problem.
 
Old 06-16-2005, 04:22 AM   #5
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
How about this ?

Code:
sed 's/\*.[^\*]*\*//g' data
If you want to remove the tabs or spaces you can use something like

Code:
sed -e 's/\*.[^\*]*\*//g' -e 's/[        ]//g' data
Here -e 's/[ ]//g' the quare bracket [] consists of space followed by a tab.

Hope this solves your problem.
 
Old 06-16-2005, 05:20 AM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
What does .[^\*]* do? Looks like [single char][anything besides * repeated indefinitely]. That will not work for a delimeter larger than one char though. Can I use [:space:] in place of space tab?
ta0kira
 
Old 06-17-2005, 02:19 AM   #7
murugesan
Member
 
Registered: May 2003
Posts: 149

Rep: Reputation: 28
Here we are replacing any string which starts with star [ \* ] and followed by any character [ . ] which is not a star [ ^\* ] and also ends with star [ \* ]. You can use space (key) for replacing.
 
Old 06-20-2005, 01:33 AM   #8
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Original Poster
Rep: Reputation: Disabled
Got it figured out; I needed the -r option with sed. Turns out my expression was fine for what I needed. Thanks for your help.
ta0kira
 
  


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
sed / regex question whysyn Linux - General 3 06-28-2005 03:11 PM
Quick regex problem, can't find solution R00ts Programming 3 05-25-2005 03:55 PM
Help with Sed and regex cmfarley19 Programming 6 11-18-2004 02:09 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 07:12 AM
mod_rewrite & regex problem webmeister Programming 2 12-30-2003 12:19 PM


All times are GMT -5. The time now is 11:49 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration