sed/awk help in removing the section of file if it contains regex
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.
i was doing it in vim , but there are lot of files and lot of regex expressions which was time consuming , there are multiple timing () lines in my file so i tried using tac filename | sed /} \/\* end of arc sbss_pclk_div4_5_SUBRATE_20checkpin1/timing \(\) {/d' | tac , which seems to be non-working
your assumptions are right , my basic problem is how to remove the section of text from a file delineated by some start and end regex only if a line inside that section of text matches another regex , so i want to delete below section only if this line matches related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ; , i was trying to find a generic solution for such thing
your assumptions are right , my basic problem is how to remove the section of text from a file delineated by some start and end regex only if a line inside that section of text matches another regex , so i want to delete below section only if this line matches related_pin : sbss_pclk_div4_1_SUBRATE_20checkpin1 ; , i was trying to find a generic solution for such thing
} /* end of arc sbss_pclk_div4_1_SUBRATE_20checkpin1
Ok. If the text looks EXACTLY like above, then my script obviously does the job.
I used grep to get the starting line number (hint: ((startline=startline-1))), and then sed to remove lines beginning from there until the end of the block.
i tried getting the line no using grep -n , but can sed delete can be used like this sed /<line no>/,/end line regex/ ? if not how to get the end line no assuming there are multiple matches for that particular regex as such line no is relative to start line ?
the usual way (at least for me) to set the delimiter as record separator and you only need to keep / drop the specified records. This can be easily implemented in awk:
i tried getting the line no using grep -n , but can sed delete can be used like this sed /<line no>/,/end line regex/ ? if not how to get the end line no assuming there are multiple matches for that particular regex as such line no is relative to start line ?
Regards
Bharath
I'm sorry, but I simply don't understand what you mean. It is probably the language barrier, but I cannot make head or tails out of this.
I'll try to break down my method for you anyway:
Find the line with the string you search for
Based on that line, set a starting point
From the starting point, remove the lines you want to remove
That's how I did it, and it works with your example. You still, however, have not showed the content of the actual file - only what you want to remove from said file - so my method may or may not work properly in real life.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.