0

I have lines in text.txt as follows

start:xfedfgrrg
vfefvrgvr
efefrg
end: abc
end : xyz

start:ewef
feefvef
frrfer
end:ccbf


start:e3frrf
f2erv
frf
end: ece
end: d32d2d
end: fff

I want to add few lines (let's say hello world) everytime patten "end:" matches. But ignore following occurrence of pattern "end:" untill a "start:" pattern matches again.

Required Output

start:xfedfgrrg
vfefvrgvr
efefrg
hello world
end: abc
end : xyz

start:ewef
feefvef
frrfer
hello world
end:ccbf


start:e3frrf
f2erv
frf
hello world
end: ece
end: d32d2d
end: fff
Kenney
  • 9,003
  • 15
  • 21
Rahul P
  • 1
  • 1

2 Answers2

3

Awk oneliner:

awk '/start:/{on=1} on&&/end:/{print "hello world"; on=0} {print}' file

which uses a variable "on" to mark when we're between the start/end blocks.

Brian
  • 2,172
  • 14
  • 24
0
$ sed '/start/{x;s/.*/./;x};/end/{x;/^.$/{x;s/^/hello world\n/;x};s/^/./;x}' filename
start:xfedfgrrg
vfefvrgvr
efefrg
hello world
end: abc
end : xyz

start:ewef
feefvef
frrfer
hello world
end:ccbf


start:e3frrf
f2erv
frf
hello world
end: ece
end: d32d2d
end: fff

Explanations:

like this

if (/start/)
    HoldSpace = "."

if (/end/)
    if (HoldSpace == ".")  # just one /end/
        add "hello world\n" to first

    HoldSpace = HoldSpace + "."  # if two /end/; HoldSpace is ".."
bian
  • 1,456
  • 8
  • 7