我有过
[root@centos64 ~]# cat /tmp/out
[
"i-b7a82af5",
"i-9d78f4df",
"i-92ea58d0",
"i-fa4acab8"
]我想用管道(虽然sed或grep )来匹配格式"x-xxxxxxxx“,即a-z 0-9的混合,总是以1-8个字符长度,而省略其他所有内容。
[root@centos64 ~]# cat /tmp/out| sed s/x-xxxxxxxx/
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8我知道这是基本的,但我只能找到文本替代的例子。
发布于 2014-07-31 15:57:00
通过GNU,
$ sed -nr 's/.*([a-z0-9]-[a-z0-9]{8}).*/\1/p' file
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8发布于 2014-07-31 15:58:49
grep -Eo '[a-z0-9]-[a-z0-9]{8}' file-E选项使它能够识别扩展正则表达式,因此它可以使用{8}来匹配8次重复。
-o选项使它只打印行中与regexp匹配的部分。
发布于 2014-07-31 21:07:40
为什么不把引号之间的任何内容打印出来:
$ sed -n 's/[^"]*"\([^"]*\).*/\1/p' file
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8
$ awk -F\" 'NF>1{print $2}' file
i-b7a82af5
i-9d78f4df
i-92ea58d0
i-fa4acab8https://stackoverflow.com/questions/25063564
复制相似问题