“N”命令如何在sed中工作?
没有序言,
$cat in.txt a b c d $sed '=;N' in.txt 1 a b 3 c d 看起来像“N”命令在每隔一行工作.也许这是自然的,因为命令’N’加入下一行并更改当前行号.但, $sed 'N;$!P;$!D;$d' thegeekstuff.txt (我看到这个here) # sed N odd-lined-file.txt 解决方法
摘自信息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. 这应该几乎解决您的查询.但是我仍然会尝试解释你们三种不同的情况: 情况1: sed从输入中读取一行. [现在模式空间有一行] >如果没有下一行读取,则在这里退出sed. [即:在奇数行的情况下,sed出现在这里 – 因此最后一行被吞没,无需打印.] > sed打印图案空间并清理它. [图案空间为空.] 总结:在这种情况下,sed读取2行,并一次打印2行.最后一行被吞下,它有奇数行(见步骤3). 案例2: sed从输入中读取一行. [现在模式空间有一行] >如果它失败退出这里.这只有在有1行时才会发生. >如果它的最后一行($!)从模式空间打印第一行(P). [图案空间的第一行被打印出来.但是模式空间中仍然有2行.] 总结:在这种情况下: sed首先读取2行. 案例3:它与CASE:1相同,只需从其中删除步骤2. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |