Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to use sed or awk to replace 5 lines in a smb file but I just don't have any idea how to deal with the newlines.

share|improve this question
f course, sed and awk can do this, but I'm having a little trouble understaning exactly what your question is. What exactly is causing you trouble? Inserting new lines? Replacing them? –  frabjous Aug 25 '10 at 4:46

1 Answer 1

Sed is quite bad at this, because it operates one line at a time. The only decent technique I've ever seen to do this is this one, which involves storing the entire file in sed's hold buffer and then operating on it all at once:

sed -n '1h;1!H;${g;s/search/replace/;p;}'

If you can, it's much easier to use perl to accomplish this:

perl -0pe 's/search/replace/'

search can contain \ns to represent newlines

share|improve this answer
+1 for the sed multiliner. –  Lazer Aug 29 '10 at 6:36

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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