所以我有这样的 .txt 文件
TAGS aws:cloudformation:stack-name yanka-cloudformer
TAGS aws:cloudformation:logical-id WebServerSecurityGroup
SECURITYGROUPS launch-wizard-3 created 2017-04-11T15:51:41.918+09:00 sg-77aaaa10 an-dx-trainning vpc-878311e3
SECURITYGROUPS This security group was generated by AWS Marketplace and is based on recommended settin s for CentOS 6 (x86_64) - with Updates HVM version 6 2014-09-29 provided by Centos.org sg-7842031d CentOS 6 -x86_64- - with Updates HVM-6 2014-09-29-AutogenByAWSMP- 270062507952 vpc-11d10f74
SECURITYGROUPS from other cloud sg-796d1b1e rancher-demo-sg 270062507952 vpc-b4ef99d1
SECURITYGROUPS default VPC security group sg-79a4861d Cfn-Vpc-Sg-temp-SecurityGroup2DefaultSG-JLBXQ8YG4RN5 270062507952 vpc-ded6c7bb
USERIDGROUPPAIRS sg-79a4861d 270062507952
我只想 grep 所有以“sg”开头的值SG-XXXXXX。我怎样才能做到这一点?
我尝试过这个,但是得到了一长串 sg.
cat hello.txt | grep -o "sg*"
sg
sg
sg
sg
我想让每个值都以“sg”开头
像这样:
sg-77aaaa10
sg-796d1b1e
sg-79a4861d
答案1
尝试这个:
$ grep -o 'sg-[^ ]*' ip.txt
sg-77aaaa10
sg-7842031d
sg-796d1b1e
sg-79a4861d
sg-79a4861d
[^ ]*
表示除空格字符以外的字符。[0-9a-f]
如果您知道它们仅包含十六进制字符,您也可以使用- 通过管道将其传递给
sort -u
orsort | uniq
或awk '!seen[$0]++'
来删除重复项
sg*
表示匹配s
后跟g
零次或多次