2

So, I'm writing a script and I have a text file like this:

blahblahblahdeleteme
<!-- post --> 
This is the text I want to keep! Pick me!!
<!-- post navigation --> 
more text please delete me I am not needed....

I would like to delete the first and last parts (and the marker, if easily done) and keep the text in the middle.

Now, I know bash isn't usually the best for parsing text like this, but since it's simple I thought I might as well stick with using bash. Is this as easy as I think it should be?

I found this post: split text file in two using bash script

I could split it into two text files then into two more and just keep the middle one. Is that my best bet? Please let me know!

Community
  • 1
  • 1
Gary O
  • 21
  • 2
  • can't you write a simple java program that uses regular expressions? If you're very specific with the markers it should be quite straightforward to write – Bartvbl Sep 06 '11 at 22:34
  • Related: http://stackoverflow.com/questions/1212799/how-do-i-extract-lines-between-two-line-delimiters-in-perl – a3nm Sep 06 '11 at 23:03

3 Answers3

6
sed '1,/<!-- post -->/d;/<!-- post navigation -->/,$d' file
  • from 1st line to first mark: delete
  • from second mark to end of file ($) delete
user unknown
  • 35,537
  • 11
  • 75
  • 121
2
awk '/<\!-- post --/,/<\!-- post navigation/' file
ghostdog74
  • 327,991
  • 56
  • 259
  • 343
1

It would be really easy in awk:

/^<!-- post -->/ { if (start != 1)
    { start=1; firstline=1;}
}

/^<!-- post navigation -->/ {start=0;}

{ if (start == 1 && firstline != 1)
  { print $0; }
  firstline=0;
}
Diego Sevilla
  • 28,636
  • 4
  • 59
  • 87