Reputation: 630
The input file I have looks like this
Topic1
Some text
Topic2
Some text
Topic3
Some text
I need to go to Topic2 and then replace "Some text" with "Actual input"
The following script works
/Topic2/,/Topic/ {
/Some text/c
Actual input
}
Is there a simpler way to do this. I was hoping the following will work but it does not.
/Topic2/ {
/Some text/c
Actual input
}
Update : Should have made it clearer. Am only looking for sed based solutions.
Upvotes: 0
Views: 76
Reputation: 58473
This might work for you (GNU sed):
sed -e '/Topic2/,/Topic/{//!d;/Topic2/a\bla\nbla\nbla' -e '}' file
or:
sed -e '/Topic2/,/Topic/{//!d;/Topic2/r fileb' -e '}' filea
or using Bash:
sed $'/Topic2/,/Topic/{//!d;/Topic2/a\\bla\\nbla\\nbla\n}' file
EDIT:
sed $'/Topic2/{:a;n;/Some text/!ba;c\\Actual input\n}' file
Upvotes: 1
Reputation: 41460
Here is a gnu awk
solution:
awk -v RS="Topic[0-9]*" 'f=="Topic2" {$0="\nActual input\n"} NR>1{print f,$0} {f=RT}' file
Topic1
Some text
Topic2
Actual input
Topic3
Some text
Upvotes: 0
Reputation: 195179
how about with awk?
awk 'p&&/Topic/{p=0}/Topic2/{p=1}p{gsub(/Some text/,"foo\nbar\nbla\nline")}7' file
above I used a multiple line replacement as example, the output with your input would be:
Topic1
Some text
Topic2
foo
bar
bla
line
Topic3
you can pass the replacement string to awk as shell variable, so that the awk one-liner would be flexible. like:
kent$ r="a
b
c"
kent$ awk -v rep="$r" 'p&&/Topic/{p=0}/Topic2/{p=1}p{gsub(/Some text/,rep)}7' f
Topic1
Some text
Topic2
a
b
c
Topic3
Some text
Upvotes: 1