LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Need help with SED command (https://www.linuxquestions.org/questions/linux-newbie-8/need-help-with-sed-command-782723/)

guessity 01-17-2010 01:58 AM

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?

konsolebox 01-17-2010 02:51 AM

Code:

sed '/<\?xml\/version"/,/-->/d' article1.txt > u.txt

guessity 01-17-2010 03:06 AM

Quote:

Originally Posted by konsolebox (Post 3829440)
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.

colucix 01-17-2010 03:12 AM

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.

guessity 01-17-2010 03:33 AM

Quote:

Originally Posted by colucix (Post 3829451)
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?

David the H. 01-17-2010 03:48 AM

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.

David the H. 01-17-2010 04:44 AM

Quote:

Originally Posted by guessity (Post 3829460)
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.

colucix 01-17-2010 07:44 AM

As David said! :)

konsolebox 01-19-2010 06:03 AM

Quote:

Originally Posted by David the H. (Post 3829470)
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.

David the H. 01-19-2010 07:13 AM

Quote:

Originally Posted by konsolebox (Post 3832008)
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.

konsolebox 01-19-2010 07:31 AM

i see thanks for the info.

pixellany 01-19-2010 07:37 AM

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

chrism01 01-19-2010 05:37 PM

I highly recommend the OWL book http://regex.info/

Tinkster 01-19-2010 07:21 PM

Indeed ... Friedl is a genius :}


All times are GMT -5. The time now is 12:18 PM.