LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using N with sed on last line (https://www.linuxquestions.org/questions/linux-newbie-8/using-n-with-sed-on-last-line-4175601851/)

vincix 03-15-2017 02:29 PM

using N with sed on last line
 
cat script4
Code:

/Owner/{
N
s/ *\n/ /
s/Owner and Operator Guide */Installation Guide\
/
}

cat operator2
Code:

Consult Section 3.1 in the Owner and Operator
Guide for a description of the tape drives
available on your system.

Look in the Owner and Operator Guide shipped with your system.

Two manuals are provided including the Owner and
Operator Guide and the User Guide.

The Owner and Operator Guide is shipped with your system.

According to this http://docstore.mik.ua/orelly/unix/sedawk/ch06_01.htm ("Do you see the two problems? The most obvious problem is that the last line did not print."), the last line shouldn't have been printed, because N cannot be applied to the last line (which contains the string "Owner"), and yet if I run sed -f script4 operator2, the last line is printed, even if the substitution (to "Installation Guide") isn't made.

Any ideas why this is happening?

MadeInGermany 03-15-2017 04:47 PM

A Unix sed and a Posix sed terminate without printing the last buffer.
GNU sed disagrees with it, and does print the last buffer (unless the default printing is suppressed with sed -n ...).
You can change that with the --posix option:
Code:

sed --posix -f script4 operator2
BTW if you want the GNU behavior with a Unix or Posix sed, the usual way is $!N instead of N (do N if not in the last line).

rknichols 03-15-2017 04:55 PM

The info page for sed (which is quite a bit more informative than the manpage, BTW) has this to say about the "N" command:
'N'
Add a newline to the pattern space, then append the next line of input to the pattern space. If there is no more input then `sed' exits without processing any more commands.
A very pedantic reading of that and the description of the execution cycle suggests that the printing of that final line can be attributed to the automatic action at the end of each processing cycle, and not, at least techically, the result of any further commands.


All times are GMT -5. The time now is 10:33 PM.