Skip to main content
added 95 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
$ sed ':a; /Network$/{$!{N;ba}}; s/Network\nAdministrator/System\nUser/g; s/Network Administrator/System User/g' test1
System
User System
User
$ sed ':a; /Network$/{$!{N;ba}}; s/Network\nAdministrator/System\nUser/g; s/Network Administrator/System User/g' guide.txt 
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

Compatibility Note: All the above use \n in the replacement text. This requires GNU sed. It will not work on BSD/OSX sed. [Hat

[Hat tip: to Philippos.]

$ sed ':a
    /Network$/{
       $!{
           N
           ba
       }
    }
    s/Network\nAdministrator/System\nUser/g
    s/Network Administrator/System User/g
    ' filename
  1. :a

    This creates a label a.

  2. /Network$/{ $!{N;ba} }

    If this line ends with Network, then, if this is not the last line ($!) read and append the next line (N) and branch back to label a (ba).

  3. s/Network\nAdministrator/System\nUser/g

    Make the substitution with the intermediate newline.

  4. s/Network Administrator/System User/g

    Make the substitution with the intermediate blank.

$ sed ':a; /Network$/{N;ba}; s/Network\nAdministrator/System\nUser/g; s/Network Administrator/System User/g' test1
System
User System
User
$ sed ':a; /Network$/{N;ba}; s/Network\nAdministrator/System\nUser/g; s/Network Administrator/System User/g' guide.txt 
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

Compatibility Note: All the above use \n in the replacement text. This requires GNU sed. It will not work on BSD/OSX sed. [Hat tip: Philippos.]

$ sed ':a
    /Network$/{
       N
       ba
    }
    s/Network\nAdministrator/System\nUser/g
    s/Network Administrator/System User/g
    ' filename
  1. :a

    This creates a label a.

  2. /Network$/{N;ba}

    If this line ends with Network, then read and append the next line (N) and branch back to label a (ba).

  3. s/Network\nAdministrator/System\nUser/g

    Make the substitution with the intermediate newline.

  4. s/Network Administrator/System User/g

    Make the substitution with the intermediate blank.

$ sed ':a; /Network$/{$!{N;ba}}; s/Network\nAdministrator/System\nUser/g; s/Network Administrator/System User/g' test1
System
User System
User
$ sed ':a; /Network$/{$!{N;ba}}; s/Network\nAdministrator/System\nUser/g; s/Network Administrator/System User/g' guide.txt 
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

Compatibility Note: All the above use \n in the replacement text. This requires GNU sed. It will not work on BSD/OSX sed.

[Hat tip to Philippos.]

$ sed ':a
    /Network$/{
       $!{
           N
           ba
       }
    }
    s/Network\nAdministrator/System\nUser/g
    s/Network Administrator/System User/g
    ' filename
  1. :a

    This creates a label a.

  2. /Network$/{ $!{N;ba} }

    If this line ends with Network, then, if this is not the last line ($!) read and append the next line (N) and branch back to label a (ba).

  3. s/Network\nAdministrator/System\nUser/g

    Make the substitution with the intermediate newline.

  4. s/Network Administrator/System User/g

    Make the substitution with the intermediate blank.

added 470 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

Compatibility Note: All the above use \n in the replacement text. This requires GNU sed. It will not work on BSD/OSX sed. [Hat tip: Philippos.]

This method is not good if the file is huge (usually meaning gigabytes). If it is that large, then reading it all in at once might strain the system RAM.

Solution that works on both GNU and BSD sed

As suggested by Phillipos, the following is a portable solution:

sed 'H;1h;$!d;x;s/Network\([[:space:]]\)Administrator/System\1Us‌​er/g'

This method is not good if the file is huge (usually meaning gigabytes). If it is that large, then reading it all in at once might strain the system RAM.

Compatibility Note: All the above use \n in the replacement text. This requires GNU sed. It will not work on BSD/OSX sed. [Hat tip: Philippos.]

This method is not good if the file is huge (usually meaning gigabytes). If it is that large, then reading it all in at once might strain the system RAM.

Solution that works on both GNU and BSD sed

As suggested by Phillipos, the following is a portable solution:

sed 'H;1h;$!d;x;s/Network\([[:space:]]\)Administrator/System\1Us‌​er/g'
added 94 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165

The problem is that the code does not substitute in for the last Network\nAdministrator.

This solution does work:

This solution does work:

The problem is that the code does not substitute in for the last Network\nAdministrator.

This solution does work:

added 32 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading
added 32 characters in body
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading
Source Link
John1024
  • 76.4k
  • 12
  • 176
  • 165
Loading