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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
06-16-2005, 01:13 AM
|
#1
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Rep: 
|
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 01:52 AM.
|
|
|
06-16-2005, 02:05 AM
|
#2
|
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
|
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
|
|
|
06-16-2005, 02:58 AM
|
#3
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Original Poster
Rep: 
|
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 03:01 AM.
|
|
|
06-16-2005, 03:16 AM
|
#4
|
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
|
How about this ?
Code:
sed 's/\*.[^\*]*\*//g' data
Hope this solves your problem.
|
|
|
06-16-2005, 03:22 AM
|
#5
|
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
|
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.
|
|
|
06-16-2005, 04:20 AM
|
#6
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Original Poster
Rep: 
|
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
|
|
|
06-17-2005, 01:19 AM
|
#7
|
Member
Registered: May 2003
Location: Bangalore ,Karnataka, India, Asia, Earth, Solar system, milky way galaxy, black hole
Distribution: murugesan openssl
Posts: 181
Rep:
|
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.
|
|
|
06-20-2005, 12:33 AM
|
#8
|
Senior Member
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078
Original Poster
Rep: 
|
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
|
|
|
All times are GMT -5. The time now is 08:08 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|