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) |