I am learning sed. Everything seemed to be going fine until I come across the N (multi-line next). I created this file (guide.txt) for practice/understanding/context purposes. Here is the contents of said file...
This guide is meant to walk you through a day as a Network
Administrator. By the end, hopefully you will be better
equipped to perform your duties as a Network Administrator
and maybe even enjoy being a Network Administrator that much more.
Network Administrator
Network Administrator
I'm a Network Administrator
So my goal is to substitute ALL instances of "Network Administrator" with "System User". Because the first instance of "Network Administrator" is separated by a newline (\n) I need the multi-line next operator (N) to append the line that starts with "Administrator" with the previous line ending with "Network\n". No problem. But I also want to catch all the other "Network Administrator" single-line instances.
From my research, I've learned that I will need two substitution commands; one for the newline separated string and one for the others. Also, there is some jive happening because of the last line containing the substitution match and the multi-line next. So I craft this...
$ sed '
> s/Network Administrator/System User/
> N
> s/Network\nAdministrator/System\nUser/
> ' guide.txt
This returns these results...
This guide is meant to walk you through a day as a System
User. By the end, hopefully you will be better
equipped to perform your duties as a System User
and maybe even enjoy being a Network Administrator that much more.
System User
Network Administrator
I'm a System User
I thought that the single-line substitution would catch all the "normal" instances of "Network Administrator" and swap it out for "System User", while the multi-line statement would work its magic on the newline separated instance, but as you can see it returned, what I consider, unexpected results.
After some fiddling, I landed on this...
$ sed '
> s/Network Administrator/System User/
> N
> s/Network\nAdministrator/System\nUser/
> s/Network Administrator/System User/
> ' guide.txt
And voilà, I get the desired output of...
This guide is meant to walk you through a day as a System
User. By the end, hopefully you will be better
equipped to perform your duties as a System User
and maybe even enjoy being a System User that much more.
System User
System User
I'm a System User
Why does this work and the original sed script doesn't? I really want to understand this.
Thanks in advance for any help.