Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2043198
  • 博文数量: 470
  • 博客积分: 10206
  • 博客等级: 上将
  • 技术积分: 5620
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-03 12:50
文章分类

全部博文(470)

文章存档

2012年(1)

2011年(18)

2010年(47)

2009年(404)

分类:

2009-08-03 11:29:59

| | | |
[] [] [] [] []
  帅哥
天使
i'am falling



CU编号: 261249
注册:2005-4-28
最后登录: 2009-07-28
帖子:
精华:0







状态:...保密...

[] [] [博客]


[]     顶部
发表于 2007-10-31 10:20 

sed '=' text2|sed 'N;s/\n//'

man了下  还是不怎么懂
n N    Read/append the next line of input into the pattern space.

去掉n/N就 不能匹配正确 为啥
麻烦解释解释
或给个提示

[ 本帖最后由 nuclearxin 于 2007-10-31 11:03 编辑 ]



您对本贴的看法:

__________________________________

the world is a stage
鉴于好多人要.大头像


| | |
  帅哥 (Tim)
法师


CU奥运火炬传递手2008
CU编号: 465018
注册:2006-9-13
最后登录: 2009-08-03
帖子:
精华:0







来自:长春
状态:...在线...

[] [] [博客]


[]     顶部
发表于 2007-10-31 10:37 

echo "1\n2\n3\n4" | sed -n 'N;s/\n/ /;p'
echo "1\n2\n3\n4" | sed -n 'n;s/\n/ /;p'

对比下就知道了。



您对本贴的看法:
| | |
  帅哥 (黑哥)
法师



CU编号: 631768
注册:2007-10-22
最后登录: 2009-08-03
帖子:
精华:0







状态:...离线...

[] [] [博客]


[]     顶部
发表于 2007-10-31 10:48 

n不追加模式空间,N追加模式空间。

[ 本帖最后由 blackold 于 2007-11-2 14:31 编辑 ]



您对本贴的看法:
| | |
  帅哥
天使
i'am falling



CU编号: 261249
注册:2005-4-28
最后登录: 2009-07-28
帖子:
精华:0







状态:...保密...

[] [] [博客]


[]     顶部
发表于 2007-10-31 11:01 



QUOTE:
原帖由 ly5066113 于 2007-10-31 10:37 发表
echo "1\n2\n3\n4" | sed -n 'N;s/\n/ /;p'
echo "1\n2\n3\n4" | sed -n 'n;s/\n/ /;p'

对比下就知道了。

输出空
什么都看不到

呵呵



您对本贴的看法:

__________________________________

the world is a stage
鉴于好多人要.大头像


| | |
  帅哥 (黑哥)
法师



CU编号: 631768
注册:2007-10-22
最后登录: 2009-08-03
帖子:
精华:0







状态:...离线...

[] [] [博客]


[]     顶部
发表于 2007-10-31 11:04 

echo "1\n2\n3\n4" | sed -n 'N;s/\n/ /;p'
echo "1\n2\n3\n4" | sed -n 'n;s/\n/ /;p'

应该有输出啊,p命令。

[ 本帖最后由 blackold 于 2007-10-31 11:07 编辑 ]



您对本贴的看法:
| | |
  帅哥
风云使者



CU编号: 374607
注册:2006-2-20
最后登录: 2009-07-09
帖子:
精华:0







来自:杭州
状态:...离线...

[] [] [博客]


[]     顶部
发表于 2007-10-31 11:16 



QUOTE:
原帖由 nuclearxin 于 2007-10-31 11:01 发表

输出空
什么都看不到

呵呵

try

echo -e "1\n2\n3\n4" | sed -n 'N;s/\n/ /;p'



您对本贴的看法:
| | |
  帅哥
天使
i'am falling



CU编号: 261249
注册:2005-4-28
最后登录: 2009-07-28
帖子:
精华:0







状态:...保密...

[] [] [博客]


[]     顶部
发表于 2007-10-31 11:22 

不懂~~~~~~~~~能不能 通俗的说下  n /N 事干什么的
burning:~/nn# echo -e "1\n2\n3\n4" | sed -n 'N;s/\n/ /;p'
1 2
3 4
burning:~/nn# echo -e "1\n2\n3\n4" | sed -n 'n;s/\n/ /;p'
2
4
输出这个代表什么啊
分号前面的n/N到底做了什么????



您对本贴的看法:

__________________________________

the world is a stage
鉴于好多人要.大头像


| | |
  帅哥 (Tim)
法师


CU奥运火炬传递手2008
CU编号: 465018
注册:2006-9-13
最后登录: 2009-08-03
帖子:
精华:0







来自:长春
状态:...在线...

[] [] [博客]


[]     顶部
发表于 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 space
这时pattern space里面就是1\n2,然后执行s/\n/ /,将换行符替换成空格,最后打印。
echo -e "1\n2\n3\n4" | sed -n 'n;s/\n/ /;p'
sed先读入第一行到pattern space,然后执行n命令,用第二行覆盖pattern space
这时pattern space里面就是2,然后执行s/\n/ /,因为pattern space里没有\n,所以不做任何替换,直接打印。



您对本贴的看法:
| | |
  帅哥
精灵



CU编号: 259402
注册:2005-4-24
最后登录: 2009-08-03
帖子:
精华:0







来自:泉州
状态:...离线...

[] [] [博客]


[]     顶部
发表于 2007-10-31 11:43 



QUOTE:
原帖由 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 ...

第2行不是2\n 吗?怎么会没有\n了.(新手)



您对本贴的看法:

__________________________________

机会并非偶然
| | |
  帅哥
天使
i'am falling



CU编号: 261249
注册:2005-4-28
最后登录: 2009-07-28
帖子:
精华:0







状态:...保密...

[] [] [博客]


[]     顶部
发表于 2007-10-31 12:03 



QUOTE:
原帖由 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 编辑 ]



您对本贴的看法:

__________________________________

the world is a stage
鉴于好多人要.大头像


| | |

» »
| | | |
[] [] [] [] []
  帅哥
天使
i'am falling



CU编号: 261249
注册:2005-4-28
最后登录: 2009-07-28
帖子:
精华:0







状态:...保密...

[] [] [博客]


[]     顶部
发表于 2007-10-31 12:04 

另:
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



您对本贴的看法:

__________________________________

the world is a stage
鉴于好多人要.大头像


| | |
  帅哥 (黑哥)
法师



CU编号: 631768
注册:2007-10-22
最后登录: 2009-08-03
帖子:
精华:0







状态:...离线...

[] [] [博客]


[]     顶部
发表于 2007-10-31 12:22 



QUOTE:
原帖由 canjie 于 2007-10-31 11:43 发表

第2行不是2\n 吗?怎么会没有\n了.(新手)

\n只出现于多行模式空间,而在单行模式空间中\n被“去掉了”,输出时又添加\n。

用心体会!!!!!!!



您对本贴的看法:
| | |
  帅哥
精灵




CU编号: 306659
注册:2005-8-26
最后登录: 2009-01-26
帖子:
精华:0







状态:...离线...

[] [] [博客]


[]     顶部
发表于 2008-11-21 15:45 

学到知识了~~



您对本贴的看法:
| | |

骑士
皇帝老儿



CU编号: 1196641
注册:2009-3-24
最后登录: 2009-08-03
帖子:
精华:0







状态:...在线...

[] [] [博客]


[]     顶部
发表于 2009-8-3 09:48 

偷师



您对本贴的看法:

__________________________________

陌上发花,可以缓缓醉矣; 忍把浮名,换了浅斟低唱
| | |
阅读(816) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~