bash script- text file manip- delete everything before/after string
I need a bash script to eliminate junk from the beginning and end of a text file.
can i search a file for 'good stuff begins', deleting each line until that string, then again, deleting each line after 'good stuff ends' or- perhaps it is possible to copy everything from 'good stuff begins' to 'good stuff ends' to a new file? tia |
You can use sed to do that. There's a tutorial at http://www.grymoire.com/Unix/Sed.html which you might find useful. The following example removes comments (lines that start with #) between the 2 lines containing 'start' and 'stop':
Code:
sed '/start/,/stop/ s/#.*//' |
so close
thanks for the help- i'm very close now, but still no cigar.
sed '1,/<START>/ d' temp.html > temp1.html this works great, removing everything from line 1 until <START> but i'm stuck on removing everything after <END> |
got it
turns out the ! is what i needed
this deletes everything but the selected region sed '/<START>/,/<END>/ !d' temp.html > temp1.html why did i waste my time googling- shoulda come here first (- ; thanks again! |
I'm glad you got it working. Thanks for posting the code you ended up using too... :)
|
similar issu with sed and the bash
My problem however that I want to remove/delete all character before the START variable and also all character following the END variable.
However START and END are not on the beginning of a line they are in the middle and furthermore in the same line similar to that example: t6gd68g d9d8j5%9j30j 0jf087*(&&^*2hd920STARTid8 =e72920 2d9nf9END93nf300j90 How to I get the output between START and END, resulting in id8 =e72920 2d9nf9 for this example. I can't find anything on the net so far. Happy for every idea. Cheers. |
If you just have one line (or a variable you want on every line) then:
Code:
sed -e 's/.*START//' -e 's/END.*//' Forrest |
Thanks
Quote:
Forrest, that's great it works! It's so easy that I can't believe it, I should have come straight to this forum, instead I was wasting my time with google for 24h :( Many thanks again!!! |
Quote:
|
Hello,
hope it is OK to re-open this (very useful, imho) thread. I need to clean some html pages and the "region" i have to keep is between the <article></article> tag. I tried using Code:
sed '/<article>/,/</article>/ !d' a.htm > b.htm Thank you. |
All times are GMT -5. The time now is 04:20 AM. |