LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-15-2017, 02:29 PM   #1
vincix
Senior Member
 
Registered: Feb 2011
Distribution: Ubuntu, Centos
Posts: 1,240

Rep: Reputation: 103Reputation: 103
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?
 
Old 03-15-2017, 04:47 PM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,564

Rep: Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121Reputation: 1121
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).

Last edited by MadeInGermany; 03-15-2017 at 04:51 PM.
 
1 members found this post helpful.
Old 03-15-2017, 04:55 PM   #3
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,740

Rep: Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198Reputation: 2198
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed command for matching the end character of a line and replacing the whole line. mamunm Linux - Newbie 4 04-12-2014 12:19 AM
[SOLVED] Multipal line edited using sed, how to make sed specific coolpraz Programming 4 01-05-2013 01:14 PM
[SOLVED] Insert line using sed or awk at line using line number as variable sunilsagar Programming 11 02-03-2012 10:48 AM
filter source line based on results line in log using awk and sed samanp Programming 5 04-06-2011 09:42 AM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration