Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

closed as unclear what you're asking by jasonwryan, Jenny D, Braiam, maxschlepzig, Networker Oct 20 '14 at 10:21

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the 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

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
Can you explain what the command line args to perl do? – Michael Jan 2 '15 at 1:11
@Michael -0 sets the record separator to null, so the whole file will be read at once instead of line-by-line. -p makes it print the result after the substitution. -e has it take the next argument as the expression to run, and s/search/replace/ is that expression – Michael Mrozek Jan 2 '15 at 6:49

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