ProgrammingThis 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.
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.
Hey, I'm looking for a little help here, and I'm having a little trouble finding the information. Basically, I need to edit a bunch of files in a directory, and all of its subdirectories. While I have done this before using sed, here is my current problem.
All of my pages have a row in a table that I would like to remove (in html). Here is what it looks like:
Now I only want to remove the row containing foo2, and leave all other rows in the table alone. Therein lies the problem I have using sed, because it works on a per line basis. I have no problem removing the line containing foo2, but I also need to delete everything in that table row from <tr>...</tr>. I can't have sed remove all lines with <tr>, because this would also affect the table row containing foo1. Any help would be much appreciated here, or maybe some advice on how web maintainers update the links that they have throughout their websites.
The following code will remove paragraphs containing an expression (foo2 in the above example), all other paragraphs will be printed.
Called it rem-par.sh
Usage: rem-par.sh infile expression
Code:
#!/bin/sh
sed -n '
# if an empty line, check the paragraph
/^$/ b para
# else add it to the hold buffer
H
# at end of file, check paragraph
$ b para
# now branch to end of script
b
# this is where a paragraph is checked for the pattern
: para
# return the entire paragraph
# into the pattern space
x
# look for the pattern, if there - delete, print all other paragraphs
/'$2'/{d}
p
' $1
I'm not quite sure if I follow your code. How exactly would I put the entire three lines in that code, including the returns? And I'm not quite following this buffer you are referring to, both how I'm supposed to pipe what's not in the paragraph into this? Could you maybe use the code I have above in your example? Thanks so much for the help!
line 1: not empty so added to hold buffer:
H='<tr>'
line 2: not empty so added to hold buffer:
H='<tr>
<td>left</td>'
and so on until H='<tr>
<td>left</td>
<td>cell 1>foo1</td>
<td>right</td>
</tr>'
line 6: empty so branch to para, where H is put back (x) into the pattern space.
If pattern space contains "$2", it is deleted (d), else it is printed (p).
All this is done for all of the "$1" file. So usage: put all your HTML lines into a file, and run rem-par.sh file.html "foo2".
This will only work if your <tr>...</tr> blocks don't contain empty lines, and <tr> is first on line and not after a non-empty line, and </tr> is last on line and is not before a non-empty line.
I see what you're talking about now, and it's a great idea. Now I just need to implement it. I want to make sure to keep all the regular blank lines in the file, but that's just some tweaking. Thanks so much for your help you two, you're lifesavers.
New problem, I'm not on a linux machine but on a Sun machine at work. The version of gsed is just too old, and it doesn't work. This code worked beautifully on my linux machine, but the files are all located on the servers at work. It looks like I'll have to figure out perl to get this done.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.