Chinaunix首页 | 论坛 | 博客
  • 博客访问: 163759
  • 博文数量: 35
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 294
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-11 14:55
个人简介

努力奋斗的骚年!

文章分类

全部博文(35)

文章存档

2014年(19)

2013年(16)

分类: LINUX

2013-09-06 18:38:06

Today , when I learned about  sed,I found a question there.That is :
$ cat hello 
   hellohellohello 
 I want the output like this :
hello 
hello 
hello 

So the code is this :    sed 's/\(hello\)/@\1/g' hello | tr "@" "\n" 
So what's the meaning of this?
 让hello与hello之间用特殊字符(在这里我用的是@)进行连接,然后在把该特殊字符(@)用tr命令转化为"\n"(换行符).
this is the explain.

It's a little hard to understand .So I search google ,found this :

Here's a simple example:

$ echo 'abcabcabc' | sed 's/\(ab\)c/\1/'
ababcabc
$ echo 'abcabcabc' | sed 's/\(ab\)c/\1/g'
ababab
$ echo 'abcabcabc' | sed 's/\(ab\)\(c\)/\1d\2/g'
abdcabdcabdc
So ,the meaning of \1 is the first part of s/ /,
in  sed 's/\(hello\)/@\1/g' hello | tr "@" "\n"  ,\1 refers to \(hello\)
in 
echo 'abcabcabc' | sed 's/\(ab\)\(c\)/\1d\2/g'  ,
\1d refers to \(ab\)   ,  \2 refers to \(c\)

\(...\) would capture the characters specified inside of the parens and \1 would be used to reference the first match, this is a part of regex.



I think it's helpful, so I note it here.thanks 




阅读(1530) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~