0

I have multiple lines that look like this:

##1##
text
##z##
##2##
text
##z##
...etc

Basically, while i already know how to get the text between the ##digit## line, and the first occurrence of ##z##, I'm not sure on how to overwrite what is in-between (the actual text that is).

I'm not trying to replace every text part of the said example/file, but only whatever is between what i specify as starting (eg: ##99##) and ending (##z##) point.

I can't/prefer not to replace the text part by the text i want to overwrite it with (eg: sed 's/oldtext/newtext/g') mainly because:

  • the text part could be part of other in-between part of the said file, and changing specifically the text, rather than what is between two specific line/pattern wouldn't.

How could i do this using Unix/local linux tools? (dd, sed, etc)

1 Answer 1

2

Given a file with the name file.txt and the contents

##1##
text
##z##
##2##
text
asdf
more text
##z##
...etc

It is not exactly clear what you want, I assume it's the following:

The command

cat file.txt | sed -n '/##2##/!{p;d};p;:cycle n;/##z##/!{H;bcycle};{x;s/.*/my awesome replacement/p;x;p}'

produces the output:

##1##
text
##z##
##2##
my awesome replacement
##z##
...etc

Is that what you want?

1
  • Yep, it is! pretty much what i imagined :) Sep 7, 2020 at 22:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .