Removing lines from file
I have a list of files and want to remove the lines between
the "Cookies Consent Notice" lines. Quote:
|
Welcome to LQ!
The common way to do that would be using sed, although there are many ways to accomplish this task. Using sed, something like this would do the trick (other scripting languages would use similar regular expressions)... Code:
/Cookies[^>]* start/,/Cookies[^>]* end/{/Cookies/!d;} LQ does not operate like a help desk where you get complete solutions, so it is better if you try to solve a problem yourself then ask for help when you get stuck, showing us what you have done first. This also tells others what you language preferences are and how you have framed the task in your own mind, which can help produce better answers for your case. Please review the Site FAQ for guidance in posting your questions and general forum usage. Good luck! |
I have tried getting the line numbers whre I get a match for "Cookies Consent Notice",
then using awk to get the two line numbers where the match occurs, then remove lines between the two line numbers. Quote:
Quote:
Quote:
|
Ok, got it when I use
Quote:
|
You need to be very specific (and very correct) when asking for help. astrogeek gave an answer to what you asked, but probably not what you meant.
As for your own solution, it appears you are confusing regex pattern with address specifiers. Why are you using regex at all in that awk ? - just print $1. :doh: - too slow typing again ... |
He asked what I had done, so I put some more things I had tried. Thanks.
|
Thanks for the response, but I am now confused about what your ultimate goal is.
Per your original question I think that you are trying to strip the 'Consent Notice' script lines from an HTML document. If that is the case I would not use the line numbers at all as it is at least an unnecessary step and confusing as it may obscure what you are really trying to accomplish. That said, if the line numbers are important for some other reason then you should tell us what that is. It is also unclear in your original question whether you want to remove the 'Consent Notice' lines themselves, or strictly the lines between them. My earlier example removes only the lines between, but can be easily modified to remove those lines as well. One effective way to ask text processing questions is to provide an example input file, which you have done, and show what you would expect the actual result to look like. Again, using your original question as the guide, this would be a possible example... Code:
Source file looks like this... Code:
Result output should look like this... |
use the correct tool for the job.
sed & awk & co. are not so suited for HTML and XML. i would use something like xmlstarlet to correctly identify the script element (maybe through "src=*cdn.cookielaw.org*" or some such) and remove it. but i don't know what the subsequent OptanonWrapper() is about. i know nothing about javascript. PS: what happened to "show us what you tried"? ;) |
Quote:
|
If astrogeek accurately described what you're trying to do, this little script does it:
Code:
#!/usr/bin/awk -f Notice that I used ".+" as a wildcard to match multiple characters. If you need to be more precise, you could replace them with actual text. I'm using "del" as a variable. All variables in awk are auto-initialized to zero. If you want to delete the "Cookie Consent" comments also, you can swap the two long lines. |
I have tried the following bash code but the file is not being changed
The idea is to run the sed command Quote:
Quote:
Quote:
|
Before we begin to debug your bash code, please take the time to again read through the suggestions offered, and questions asked to this point.
If your questions are to be based around a certain input file, abaca.html for example, then please post the relevant contents of that file along with an example of what you expect the result to be. Your bash code is incomplete and mostly irrelevant to the questions asked, so let's not go there until we know what it is you are trying to accomplish. This is also the first indication that you want to write this into an interactive script which takes arguments to be used in the replacement operation. Please tell us clearly, and precisely what you are trying to accomplish so that we can try to resolve one problem at a time - help us help you! Please review the Site FAQ for guidance in asking well formed questions. |
I have got it working now.
I wanted to change the following file Code:
<script src="https://cdn.cookielaw.org/consent/bcd12cae-aa3c-4ffb-ac51-8d41462cdcb4.js" type="text/javascript" charset="UTF-8"></script> Code:
<script src="https://cdn.cookielaw.org/consent/bcd12cae-aa3c-4ffb-ac51-8d41462cdcb4.js" type="text/javascript" charset="UTF-8"></script> |
Very good!
It is also helpful if you can post your solution here so that others who are looking for a solution to similar problems can benefit from the experience. When you are satisfied that the problem is solved please use the Thread Tools list at top of the first post to mark the thread as SOLVED. Thanks, and good luck! |
Quote:
Quote:
Anyway, a very good starting point has been given in #2 as you only need to change a few characters to make it work for your case. |
All times are GMT -5. The time now is 01:50 AM. |