Linux - NewbieThis 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
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.
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.
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.
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'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.
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.