LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-13-2007, 01:32 PM   #1
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Rep: Reputation: 15
simple reg ex question


Hi there - I am new to regular expressions, but i was wondering how i would do the following...

Say I have a file with this in it:

Quote:
# start 1
###########
something something

something
#
############
# end 1

# start 2
###########
something esle something new

sometimes another
#
############
# end 2

# start 3
###########
generic test
more text
#
############
# end 3
Now say I wanted to write a regular expression to say match and delete (using sed) all of the #2 entry, how would I do that?

It is the regular expressoin part I am confused about.

My file would end up looking like this:

Quote:
# start 1
###########
something something

something
#
############
# end 1

# start 3
###########
generic test
more text
#
############
# end 3
Thanks in advance.

PS - I have been googling, but my results (when I test what I read) are always all messed up.
 
Old 02-13-2007, 01:39 PM   #2
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
I'd say the easiest way would be to get the line number of "start 2" and "end 2" and then simply use sed to delete that range of line numbers.
 
Old 02-13-2007, 01:46 PM   #3
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by trickykid
I'd say the easiest way would be to get the line number of "start 2" and "end 2" and then simply use sed to delete that range of line numbers.
Yeah I figured as much, but I was looking for the reg ex example, it seems like it should be something simple, but for some reason my tests have come back with undesirable results.

Thanks!
 
Old 02-13-2007, 01:53 PM   #4
xflow7
Member
 
Registered: May 2004
Distribution: Slackware
Posts: 215

Rep: Reputation: 45
The 'd' command in sed accepts an address range, so you want to address the range from 'start 2' to 'end 2'.

The regexp's would be:

Code:
^# *start 2
and

Code:
^# *end 2
I added the * after the space following # because I can't tell for sure if you have a space there or not. The * allows for there to be zero or more space characters there.

The ^ character specifies that the match happen beginnin at the first character in the line.

So the entire sed command would look something like:

Code:
sed -e '/^# *start 2/,/^# *end 2/d' <filename>

Last edited by xflow7; 02-13-2007 at 01:55 PM.
 
Old 02-13-2007, 02:49 PM   #5
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by xflow7
The 'd' command in sed accepts an address range, so you want to address the range from 'start 2' to 'end 2'.

So the entire sed command would look something like:

Code:
sed -e '/^# *start 2/,/^# *end 2/d' <filename>
Hmmm I just gave that a go. But for some reason the file is not changed at all.

I am writing a conceptual script to add and delete apache vhost entries. Just more of a "practice" for myself. So maybe if i post actual code it might make more sense as to why it is not working...

I have a "fake" vhost file with this in it:

Quote:
# Start dustin.com
#####################################
#

<virtual *:80>
ServerName dustin.com
ServerAlias www.dustin.com
Log /home/logs/www.dustin.com/access.log
HomeDir /home/sites/www.dustin.com
</virtual>

#
#-------------------------------------
# End dustin.com

# Start dustin.ca
#####################################
#

<virtual *:80>
ServerName dustin.ca
ServerAlias www.dustin.ca
Log /home/logs/www.dustin.ca/access.log
HomeDir /home/sites/www.dustin.ca
</virtual>

#
#-------------------------------------
# End dustin.ca

# Start dustin.org
#####################################
#

<virtual *:80>
ServerName dustin.org
ServerAlias www.dustin.org
Log /home/logs/www.dustin.org/access.log
HomeDir /home/sites/www.dustin.org
</virtual>

#
#-------------------------------------
# End dustin.org
And my actual script is simply this thus far (hard coded - the dustin.ca would be replaced with $1 later):

Code:
#!/bin/bash
declare -rx SCRIPT=${0##*/}             # SCRIPT is the name of this script
declare -rx vfile="/home/dustin/Desktop/vhost.conf"
sed -e '/^# *Start dustin.ca/,/^# *End dustin.ca/d' $vfile
cat $vfile
exit 0
Now I am sure I need to be careful with the quotes in the SED command, especially when I change those entries to the $1.
 
Old 02-13-2007, 02:52 PM   #6
trickykid
LQ Guru
 
Registered: Jan 2001
Posts: 24,149

Rep: Reputation: 269Reputation: 269Reputation: 269
With sed, you need to redirect it to a new file or it's just going to print the changes on the console or terminal.
 
Old 02-13-2007, 03:14 PM   #7
xflow7
Member
 
Registered: May 2004
Distribution: Slackware
Posts: 215

Rep: Reputation: 45
Yeah, that's an important point.

If you are looking to write the resulting data back to the same file it may be easier to use ed (as opposed to sed) which uses similar syntax but operates on the full file in a buffer rather than line-by-line.

Code:
echo -e "/^# *start 2/,/^# *end 2/d\nw" | ed <filename>
or

Code:
echo \
    "/^# *start 2/,/^# *end 2/d
    w" \
| ed <filename>
which may be slightly more readable by making the two-line command input to ed clear.

Or there may be a much better way still that I don't know.

Last edited by xflow7; 02-13-2007 at 03:15 PM.
 
Old 02-13-2007, 04:08 PM   #8
dkrysak
LQ Newbie
 
Registered: Sep 2004
Distribution: centos
Posts: 25

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by xflow7
Yeah, that's an important point.

If you are looking to write the resulting data back to the same file it may be easier to use ed (as opposed to sed) which uses similar syntax but operates on the full file in a buffer rather than line-by-line.

Code:
echo -e "/^# *start 2/,/^# *end 2/d\nw" | ed <filename>
or

Code:
echo \
    "/^# *start 2/,/^# *end 2/d
    w" \
| ed <filename>
which may be slightly more readable by making the two-line command input to ed clear.

Or there may be a much better way still that I don't know.

Worked great!

Now onto figuring out how to remove an empty line if there are 2 or more (thus just leaving a single blank line).

Thanks!
 
Old 02-13-2007, 04:38 PM   #9
xflow7
Member
 
Registered: May 2004
Distribution: Slackware
Posts: 215

Rep: Reputation: 45
Glad that worked. There is one caveat, though, which may or may not pose a problem for your application. That ed command is only going to delete the first section that matches.

If you had another section further down bounded by the same text it would be left intact. There may be a way to get ed to perform that action on all sections that match, but I'm not sure how.

sed would do it as it will process the whole file line-by-line but you would then have to do the redirection as noted above to achieve writing the new file.

However, this will probably be slightly more complicated than just:

Code:
sed -e 'command-string' filename > filename
because the '> filename' will open filename for writing and truncate it to zero, meaning all the data in the file will be lost before sed does anything.

Instead, you'll have to use a temporary file for the redirection and then mv or cp that file onto the original filename.

Code:
sed -e 'command-string' filename > sometempfile
mv sometempfile filename
There may be a way to tell the shell to use a temporary file descriptor or something and avoid creating a temp file but that's beyond the scope of my knowledge.
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl Reg Exp Question amytys Programming 1 12-09-2005 12:53 PM
Nice surprise Kaffeine plays Reg 1 and Reg 2 disks 1kyle SUSE / openSUSE 1 10-10-2005 04:47 PM
Reg Expression Question windisch Programming 6 10-04-2005 11:08 AM
question reg nss_* functions and winbind kcv Linux - Networking 1 08-12-2005 04:19 PM
Scripting question reg. filenames with spaces Awfki Linux - Newbie 3 05-16-2004 08:01 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

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