1

I am trying to get how Noption works in a Sed editor. My goal is to change "System Administrator" to "Desktop User" in the "file01", while braking line and even in the last line. Sed won't catch up with the last line because there would not be a next line. Another modification is necessary, adding for instance:

sed 's/System Administrator/Desktop User/', but this and:

sed 'System\nAdministrator/Desktop\nUser/' toggles in an unexpected way (for me), such that one of the command stops working for the last line, or last two lines. This happened either using N between the two or befor both of them. I am using GNU Sed, version 4.4 .

#cat file01
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
Another line
And here we have: System Administrator's Group as well.
1.System Administrator's group.
2.System Administrators Group.
3.System Administrators Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
System Administrators Group.

Case 1, both commands after N

# sed '
>N
>s/System\nAdministrator/Desktop\nUser/
>s/System Administrator/Desktop User/
> ' file01
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: Desktop User's Group as well.
1.Desktop User's group.
2.System Administrators Group.
3.Desktop Users Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
Desktop Users Group.

Case 2, sed 's/System Administrator/Desktop User/' before N.

# sed '
> s/System Adminitrator/Desktop User/
> N
> s/System\nAdministrator/Desktop\nUser/
> ' file01
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: System Administrator's Group as well.
1.Desktop User's group.
2.System Administrators Group.
3.Desktop Users Group.
The first meeting of the Linux System
Administrator's group will be held on Tuesday.
System Administrators Group.

This seemed weird to me, and could not figure out what was wrong. [Edit]: further detail.

I am looking for replacing "System Administrator" with "Desktop User". Also if a line ends with "System" and the next line starts with "Administrator", i would have them replaced accordingly with "Desktop" and "User". All of this has been taken from a book, but the output did not match what the book was stating. I ended up not knowing what went wrong. The only world i found to describe my problem was the precedence, i apologize, it seems that i was wrong.

don_crissti
  • 82,805
pigeon
  • 107

2 Answers2

2

This doesn't really have anything to do with precedence (which is usually something that relates to operators), but to the order in which the commands are being issued.


Taking a look at the first example in the question:

N
s/System\nAdministrator/Desktop\nUser/
s/System Administrator/Desktop User/

This will read in the lines in pairs and apply the two substitutions on them. If the second line in the pair ends with System (having Administrator on the following, third, line), then it will fail to detect that. This means that the string, when straddling an odd and an even line, will not be replaced.

Taking a look at the second example in the question (with spelling corrected):

s/System Administrator/Desktop User/
N
s/System\nAdministrator/Desktop\nUser/

This will change the string on the current line, read the next line and change the string with a newline in the middle. This will not change odd lines with a complete copy of the string on it (or odd lines with only System on it).


Using GNU sed:

:top
N
$s/System\(.\)Administrator/Desktop\1User/g
b top

This script loops and reads all lines of the file into the pattern space. Once it reaches the last line of input it performs the substitution globally while allowing for any character between the two words (could also use \([ \n]\)) instead of \(.\)).

The result will be

The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Another line
And here we have: Desktop User's Group as well.
1.Desktop User's group.
2.Desktop Users Group.
3.Desktop Users Group.
The first meeting of the Linux Desktop
User's group will be held on Tuesday.
Desktop Users Group.
Kusalananda
  • 333,661
  • I am working with what is at hand, either i have the issue with my system, and still not know where, or else the the book was not right. In case 1 , all combinations of adding /g to one of or both of "s/Syst...." has been tried before. Always something was left unchanged in the output. – pigeon Feb 24 '18 at 10:24
  • 1
    @pigeon Let us know what sed you are using on what type of Unix system. The example in the book assumes GNU sed since it inserts a newline using \n. I will later expand on why the two example commands that you posted won't work (I have a busy day). – Kusalananda Feb 24 '18 at 11:25
  • FWIW this approach only works with gnu sed which doesn't terminate if there's no Next input line (use --posix to emulate a posix sed) – don_crissti Feb 24 '18 at 13:50
  • @don_crissti Yes, this is why I explicitly mentioned that sed variant. Whatever text the OP is reading likewise assumes GNU as it inserts a newline. – Kusalananda Feb 24 '18 at 13:52
  • Alright, my only concern was to find out what went wring. The parity of lines explained it all. Thanks a lot for your time you all!, Thanks @Kusalananda . – pigeon Feb 24 '18 at 14:06
0

When searching for patterns that may span over two lines N is used together with P and D - aka a N;P;D cycle, so as to always have two lines in the pattern space1:

sed '$!N
s/System\nAdministrator/Desktop\nUser/
s/System Administrator/Desktop User/g
P
D' <infile

Note that s/System\nAdministrator/Desktop\nUser/ is gnu sed syntax. Portably you'd do

s/System\nAdministrator/Desktop\
User/

1: it starts with lines 1-2, after the P;D it processes lines 2-3, then lines 3-4 and so on...

don_crissti
  • 82,805