How do I replace the first blank line with two lines of content? I did see a question on replacing multiple blank lines with a single blank line in vim sed but don't quite see how to adapt that. So, for example, if my input file is:

% abd
% def

% jkl

% mno

I would like to have a sed command that replaces just the first blank line with these two lines (one containing ghi and the other containing %):

% abd
% def
% ghi
%
% jkl

% mno
link|improve this question
Is the last line supposed to stay jkl? – Michael Mrozek May 24 '11 at 5:41
I don't understand the requirements either. What is jkl supposed to become? Could you express the requirements in English as well, for example “replace all blank lines by a fixed multi-line text” or “replace all blank lines by a blank line preceded by a copy of the next line”? – Gilles May 24 '11 at 19:30
Oppss. Sorry, I have corrected the example. ALL lines (except the first blank line), stay as they are.. The first blank line gets replaced with %ghi\n% where the \n represents a linefeed to get me to the next line so that there is a line with just a % on it. Hope that removes the confusion. – Peter Grill May 24 '11 at 22:29
feedback

2 Answers

Sed matches entire lines but doesn't include the newline, so a blank line will just be an empty string. You can use ^ to match the beginning of a line and $ to match the end, so ^$ matches a blank line. Just replace that with % ghi\n%:

sed 's/^$/% ghi\n%/'

The newline that already existed will remain, so you'll end up with % ghi on one line and % on the next


Edit: If it needs to only match once then the expression is a bit more complicated. The easiest way I know to do it in sed is:

sed '0,/^$/ s/^$/% ghi\n%/'

The replacement is wrapped in an address range 0,/^$/, which means "only apply the following to the lines between 0 and the first line that matches ^$". Since the replacement expression checks for ^$ as well, the only line that's actually going to change is the first that matches ^$ -- the first blank line

link|improve this answer
That works great, but I forgot to mention that i ONLY want it to replace the first blank line. I should be able to add "1" somewhere at the end , bu everywhere I try I seem to get an error. – Peter Grill May 24 '11 at 5:52
I am getting a syntax error "bad flag in substitute command: '}'" on a Mac running 10.6.7? – Peter Grill May 24 '11 at 6:16
If I know the line number, which I do, I can then use the following: s/^$/% ghi\\\n%/' Only problem left is that that \n shows up two characters "\" and "n" near the end of the line as opposed to inserting a carriage return. – Peter Grill May 24 '11 at 6:44
@Peter Is the mac version of sed different? I don't really know anything about macs. If you know the line number this is way easier though: sed '3 i ghi' (to insert ghi above line 3) – Michael Mrozek May 24 '11 at 14:01
@Michael: I do not know of any specific differences between mac sed and other. But OS X is certified as UNIX as per this question. I have posted a follow up to find if there are differences between sed on mac osx and other standard sed – Peter Grill May 24 '11 at 18:18
show 1 more comment
feedback
sed '/^$/{i\
% ghi\
%
d
}'

When sed finds a blank line, this inserts the two lines '% ghi' and '%', then deletes the blank line.


From the comments

This produces a syntax error.

Use a real shell instead of a sea-shell; it will save everyone grief in the long run. Place the commands on 5 lines of a simple file and use sed -f sed.script. There are probably other ways to achieve this in C shell - this works, though:

echo '/^$/{\'  > sed.script
echo '% ghi\' >> sed.script
echo '%'      >> sed.script
echo 'd'      >> sed.script
echo '}'      >> sed.script
sed -f sed.script data.file
rm -f sed.script
link|improve this answer
This also produces a syntax errror, even if I attempt to put it on one line. – Peter Grill May 24 '11 at 6:45
I have seen quite a few posts about giving up on csh and after your comment I will switch to sh for my script writing needs. Thanks for the frank feedback. – Peter Grill May 24 '11 at 18:21
1  
@Peter: the definitive demolition of C shell for programming is Csh Programming Considered Harmful. I think you just made a correct decision. – Jonathan Leffler May 24 '11 at 18:24
feedback

Your Answer

 
or
required, but never shown

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