在具有以下内容的linux文件中,我希望摆脱以特定模式开头的所有内容,在这种特殊情况下为aws:
:
aws:cloudformation:logical-id="somestring" Name="abc" Product="xyz" Role="www" aws:autoscaling:groupName="strings-and-numbers-012345" aws:cloudformation:stack-id="strings-and-numbers-012345" aws:cloudformation:stack-name="strings-and-numbers-012345"
我需要的输出只是:Name="abc" Product="xyz" Role="www"
;我该怎么做?
答案 0 :(得分:1)
awk '{print $2,$3,$4}' file
Name="abc" Product="xyz" Role="www"
答案 1 :(得分:0)
怎么样
sed 's/aws:[^ ]*//g' filename
答案 2 :(得分:0)
在awk中:
$ awk '{for(i=1;i<=NF;i++)if($i!~/^aws:/)b=b(b==""?"":OFS)$i;print b;b=""}' file
Name="abc" Product="xyz" Role="www"
说明:
{
for(i=1;i<=NF;i++) # iterate over every field
if($i!~/^aws:/) # if it doesn't start start with aws:
b=b (b==""?"":OFS) $i # buffer it to b with OFS
print b # never stop iterating till the iterating is done and output
b="" # reset buffer
}
下行是,如果每个字段都以aws:
开头,则会打印一个空行。在if(b!="")
之前使用print b
避免使用此项。
答案 3 :(得分:0)
假设:
$ echo "$s"
aws:cloudformation:logical-id="somestring" Name="abc" Product="xyz" Role="www" aws:autoscaling:groupName="strings-and-numbers-012345" aws:cloudformation:stack-id="strings-and-numbers-012345" aws:cloudformation:stack-name="strings-and-numbers-012345"
使用GNU Grep和Perl正则表达式,您可以:
$ echo "$s" | grep -Po '(\bName=.*?)(?=aws)'
Name="abc" Product="xyz" Role="www"
如果您感兴趣的字段是严格定位的,则可以使用awk
或cut
打印这些位置字段:
$ echo "$s" | awk '{print $2, $3, $4}'
Name="abc" Product="xyz" Role="www"
$ echo "$s" | cut -d" " -f2-4
Name="abc" Product="xyz" Role="www"
要清除以aws
开头的所有条目,您可以使用perl
:
$ echo "$s" | perl -lane 'foreach (@F) {$s=$s."$_ " unless /^aws/} print "$s"'
Name="abc" Product="xyz" Role="www"
或
$ echo "$s" | perl -lane 'print join(" ", grep {!/^aws/} @F)'
Name="abc" Product="xyz" Role="www"