LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-17-2010, 01:58 AM   #1
guessity
Member
 
Registered: Dec 2009
Posts: 41

Rep: Reputation: 15
Question Need help with SED command


Hi,
I have been trying to remove few lines of text in a file -



I basically want to remove the following -

Code:
<?xml version="1.0" encoding = "iso-8859-1"?>
<!--
All Rights Reserved.
-->
<!--
This feed is made by using XML Feed Maker v2.2.
-->

I used this command

Code:
sed '/<?xml\/version"/,/-->/d' article1.txt > u.txt
any idea where I went wrong?
 
Old 01-17-2010, 02:51 AM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
sed '/<\?xml\/version"/,/-->/d' article1.txt > u.txt
 
Old 01-17-2010, 03:06 AM   #3
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by konsolebox View Post
Code:
sed '/<\?xml\/version"/,/-->/d' article1.txt > u.txt
Thats what I had typed. the problem is that the text still remains in the new file.
 
Old 01-17-2010, 03:12 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
If you want to edit the file in place, use the -i option. Be careful with that: if you are not really sure that the result is exactly what you're trying to do, the loss of the original file is at risk. Anyway, you can use the -i option in the following way to do a safe backup copy:
Code:
sed -i.bck 'blah blah blah' file
this will create a copy of th original file, called file.bck, where .bck is just an example. Actually, you can choose the suffix at your pleasure.
 
Old 01-17-2010, 03:33 AM   #5
guessity
Member
 
Registered: Dec 2009
Posts: 41

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by colucix View Post
If you want to edit the file in place, use the -i option. Be careful with that: if you are not really sure that the result is exactly what you're trying to do, the loss of the original file is at risk. Anyway, you can use the -i option in the following way to do a safe backup copy:
Code:
sed -i.bck 'blah blah blah' file
this will create a copy of th original file, called file.bck, where .bck is just an example. Actually, you can choose the suffix at your pleasure.
I did this

Code:
sed -i '/<\?xml\/version"/,/-->/d' article1.txt > u.txt
I ended with a blank file u.txt and nothing in article1.txt changed (

What could be wrong?
 
Old 01-17-2010, 03:48 AM   #6
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
What's with all the escaping? I just used the following:
Code:
sed '/<?xml version/,/-->/d'  file
Of course, this only removes the first commented section. To remove the second one, you'd need to modify it. Probably the easiest way to do it is to simply address the number of lines following the first match (assuming gnu sed).
Code:
sed '/<?xml version/,+6d'  file
A useful way to debug a sed expression is to first set it to print only the lines you want to match first. Then once you get it working correctly, reverse it.
Code:
sed -n '/<?xml version/,+6p'  file
PS: I often use the sed faq to help me with my matching needs.
 
Old 01-17-2010, 04:44 AM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by guessity View Post
I did this

Code:
sed -i '/<\?xml\/version"/,/-->/d' article1.txt > u.txt
I ended with a blank file u.txt and nothing in article1.txt changed (

What could be wrong?
">u.text" redirects standard output to the new filename. Since you used -i to edit the original file in place, there's no output to redirect. And since the expression failed to match, there was nothing to edit in the original file either.
 
Old 01-17-2010, 07:44 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
As David said!
 
Old 01-19-2010, 06:03 AM   #9
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Quote:
Originally Posted by David the H. View Post
What's with all the escaping? I just used the following:
Code:
sed '/<?xml version/,/-->/d' file
My mistake. I didn't notice that it was space. Anyway shouldn't '<?' mean an optional '<' and not '<?' ? I haven't tested it though.
 
Old 01-19-2010, 07:13 AM   #10
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Quote:
Originally Posted by konsolebox View Post
Anyway shouldn't '<?' mean an optional '<' and not '<?' ? I haven't tested it though.
In extended regexp mode, yes. But without the -r flag, sed uses only a simplified "basic" regex syntax and "?" loses its special meaning (along with some other characters).

In fact, in basic mode, escaping the question mark actually enables the extended regex functionality!

The grep manpage goes into more detail about the differences between basic and extended regex, since it works the same way.
 
Old 01-19-2010, 07:31 AM   #11
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
i see thanks for the info.
 
Old 01-19-2010, 07:37 AM   #12
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Anyone who can remember all the differences between regular and extended Regexes---AND how those differences vary among programs---is worthy of sainthood.

I have developed a simple approach: Trial and Error
 
Old 01-19-2010, 05:37 PM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I highly recommend the OWL book http://regex.info/
 
Old 01-19-2010, 07:21 PM   #14
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Indeed ... Friedl is a genius :}
 
  


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
sed command Fond_of_Opensource Linux - Newbie 2 11-17-2006 08:11 AM
sed command shyamdey Programming 2 08-29-2006 11:06 PM
sed command ancys Programming 2 08-05-2006 10:50 AM
SED Command racingdynamics Linux - General 4 07-25-2006 02:09 PM
sed command kwigibo Linux - General 3 04-21-2002 04:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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