2

I have == Changelog == in a textfile and I want to create a new file where this line and everything after it will be deleted.

I am sure the answer is here somewhere, I have tried to search, now my head is spinning. sed, awk, csplit, ... ?

4
  • 2
    awk '/Changelog/{exit}1' file > newfile – jasonwryan Mar 15 '15 at 6:34
  • Thanks. I better use '/== Changelog/{exit}1' or I get the wrong line by accident or not? – redanimalwar Mar 15 '15 at 7:23
  • That all depends on what else is in your input file... Does Changelog appear on another line? – jasonwryan Mar 15 '15 at 7:25
  • Maybe, maybe later so I will be specific in the first place. – redanimalwar Mar 15 '15 at 8:07
1

I would use sed:

sed '/== Changelog ==/,$d' file > newfile

This syntax is less efficient than the one proposed above by jasonwryan, but it is more readable by most people. There are two famous use for sed: the basic search and replace s/// and range based action /beginpattern/,/endpattern/X where X is a sed action like delete, search-replace... Any construction with a semicolon (;) is more programmatic and less understood.

1
  • Accepted for putting the == in there. And because sed ,$d is quite easy to remember for me. Its actually almost impossible that there is another capital case Changelog in there but still I like it this way more. But for to include just the == before the word so I can forget the == after the words. I did that once. Its about wordpress.org/plugins/about/readme.txt btw. In this annoying light markdown hears closing are actually not optional like in real markdown. – redanimalwar Mar 18 '15 at 10:04
2

With awk:

awk '/Changelog/{exit}1' file > newfile

or with sed:

sed -n '/Changelog/q;p' file > newfile
2
  • Doesn't this answer ignore the == signs? – o0'. Mar 15 '15 at 11:09
  • @Lohoris Yes, see the comments under the question... – jasonwryan Mar 15 '15 at 16:11
0
csplit -f newfile -n1 infile /==\ Changelog\ ==/ 

That will create ./newfile0 and ./newfile1 (w/ GNU csplit, or POSIXly ./newfile[12]). The first will contain all lines up to the first occurring line that matches == Changelog == and the second will contain all lines following that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.