'N' 命令在 sed 中有什么作用? [英] What does the 'N' command do in sed?

查看:39
本文介绍了'N' 命令在 sed 中有什么作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看起来'N'命令每隔一行都有效:

It looks like the 'N' command works on every other line:

$ cat in.txt 
 a
 b
 c
 d
$ sed '=;N' in.txt 
1
 a
 b
3
 c
 d

也许这很自然,因为命令 'N' 连接下一行并更改当前行号.但是(我看到了这个 此处):

Maybe that would be natural because command 'N' joins the next line and changes the current line number. But (I saw this here):

$ sed 'N;$!P;$!D;$d' thegeekstuff.txt

上面的例子删除了文件的最后两行.这不仅适用于偶数行编号的文件,也适用于奇数行编号的文件.在这个例子中,'N' 命令在每一行上运行.有什么区别?

The above example deletes the last two lines of a file. This works not only for even-line-numbered files but also for odd-line-numbered files. In this example 'N' command runs on every line. What's the difference?

你能告诉我为什么当我像这样运行 sed 时我看不到最后一行:

And could you tell me why I cannot see the last line when I run sed like this:

# sed N odd-lined-file.txt

推荐答案

摘自 info sed:

`sed' operates by performing the following cycle on each lines of
input: first, `sed' reads one line from the input stream, removes any
trailing newline, and places it in the pattern space.  Then commands
are executed; each command can have an address associated to it:
addresses are a kind of condition code, and a command is only executed
if the condition is verified before the command is to be executed.
...
When the end of the script is reached, unless the `-n' option is in
use, the contents of pattern space are printed out to the output
stream,
...
Unless special commands (like 'D') are used, the pattern space is
deleted between two cycles

...

`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.

...

`D'
     Delete text in the pattern space up to the first newline.  If any
     text is left, restart cycle with the resultant pattern space
     (without reading a new line of input), otherwise start a normal
     new cycle.

这应该可以解决您的查询.但我仍然会尝试解释您的三种不同情况:

This should pretty much resolve your query. But still I will try to explain your three different cases:

案例 1:

  1. sed 从输入中读取一行.[现在模式空间中有 1 行.]
  2. = 打印当前行号.
  3. N 将下一行读入模式空间.[现在模式空间中有 2 行.]
    • 如果没有下一行要读取,则 sed 将在此处退出.[即:在奇数行的情况下, sed 在这里退出 - 因此最后一行被吞下而不打印.]
  1. sed reads a line from input. [Now there is 1 line in pattern space.]
  2. = Prints the current line no.
  3. N reads the next line into pattern space.[Now there are 2 lines in pattern space.]
    • If there is no next line to read then sed exits here. [ie: In case of odd lines, sed exits here - and hence the last line is swallowed without printing.]

总结:在这种情况下,sed 一次读取 2 行并打印 2 行.最后一行被吞下,有奇数行(见步骤 3).

Summary: In this case sed reads 2 lines and prints 2 lines at a time. Last line is swallowed it there are odd lines (see step 3).

情况 2:

  1. sed 从输入中读取一行.[现在模式空间中有 1 行.]
  2. N 将下一行读入模式空间.[现在模式空间中有 2 行.]
    • 如果失败退出这里.仅当有 1 行时才会发生这种情况.
  1. sed reads a line from input. [Now there is 1 line in pattern space.]
  2. N reads the next line into pattern space. [Now there are 2 lines in pattern space.]
    • If it fails exit here. This occurs only if there is 1 line.

总结:在这种情况下:

  • sed 首先读取 2 行.
  • 如果有下一行可供阅读,请打印第一行并阅读下一行.
  • else 从缓存中删除这两行.这样它总是删除最后两行.
  • sed reads 2 lines first.
  • if there is next line available to read, print the first line and read the next line.
  • else delete both lines from cache. This way it always deletes the last 2 line.

案例 3:
它与CASE:1的情况相同,只需从中删除第2步即可.

CASE 3:
Its the same case as CASE:1, just remove the Step 2 from it.

这篇关于'N' 命令在 sed 中有什么作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆