原帖由
ly5066113 于 2007-10-31 11:34 发表
3楼不都解释的很清楚了么?
正常的echo -e "1\n2\n3\n4"的结果是:
1
2
3
4
echo -e "1\n2\n3\n4" | sed -n 'N;s/\n/ /;p'
sed先读入第一行到pattern space,然后执行N命令,将第二行追加进pattern spac ...
正解~~~~~~~三颗油
另:
following explaination could be small but concise , can treat it as a guideline when you study all the sed commands.
Where SED buffers data
SED maintains two data buffers: the active pattern space, and the
auxiliary hold space. In "normal" operation, SED reads in one line from
the input stream and places it in the pattern space. This pattern space
is where text manipulations occur. The hold space is initially empty,
but there are commands for moving data between the pattern and hold
spaces.
right, a small practice here for SED command "x" :
'x' - Exchange the contents of the hold and pattern spaces.
[Copy to clipboard] [ - ]CODE:
say a file contains following 3 lines ,
#cat file
line1
line2
line3
#
by applying 'x' command, the output is as following :
#sed 'x' file
line1
line2
#
explain :
<-- first line is empty , because hold space and pattern
space exchange the contents , do remember initially the hold space is
empty , now hold space contains line1 after first line data
manipulation .
line1 <- second output is line1, now hold space contains line2 , and so on a so forth . ^_^
line2
[
本帖最后由 nuclearxin 于 2007-10-31 12:04 编辑 ]