Reputation: 59426
I'm trying to substitute the first empty line in my input file with a multiline block, i. e. out of
one
two
three
four
five
six
I want to create
one
two
foo
three
four
five
six
For this I tried this sed
script:
sed '/^$/i\
\
foo'
But it inserts at /each/ empty line.
How can I tweak this call to sed
so that it inserts just at the first occurrence of an empty line? Is there a way to tell sed
that now the rest of the input should just be copied from to the output?
I do not want to switch to awk
or other shell tools like read
in a loop or similar. I'm just interested in the use of sed
for this task.
Upvotes: 1
Views: 46
Reputation: 89557
You can loop and print lines until the end of the file:
sed '/^$/{i\
\
foo
:a;n;ba}' file
Upvotes: 3
Reputation: 59426
I found a way by replacing the i
with a s
command:
sed '0,/^$/s//\
foo\
/'
But I would prefer a solution using the i
command because not everything I could want to do after the search might be easily replaceable with an s
.
Upvotes: 1