是否有选项可以将行的输出转换为Linux中的新列并删除行中的重复条目

是否有选项可以将行的输出转换为Linux中的新列并删除行中的重复条目

我的 cli 输出如下,

GROUP DEFINITIONS   SERVICE DIMENSION
RESULTSBYTIME   False
KEYS    AWS CloudShell
BLENDEDCOST 0.0000026589    USD
KEYS    AWS CloudTrail
BLENDEDCOST 0.000001    USD
KEYS    AWS Config
BLENDEDCOST 46.921  USD
KEYS    AWS Glue
BLENDEDCOST 355.70735552    USD
KEYS    AWS Key Management Service
BLENDEDCOST 0.10418545  USD
KEYS    AWS Lambda
BLENDEDCOST 0.0000002605    USD
KEYS    AWS Migration Hub Refactor Spaces
BLENDEDCOST 0   USD
KEYS    AWS Secrets Manager
BLENDEDCOST 2.0496788951    USD
KEYS    AWS Security Hub
BLENDEDCOST 0.028892556 USD
KEYS    AWS Service Catalog
BLENDEDCOST 0   USD
KEYS    AWS Step Functions
BLENDEDCOST 0.0000000031    USD
KEYS    AWS Support (Business)
BLENDEDCOST 246.2376324993  USD
KEYS    AWS Systems Manager
BLENDEDCOST 0.000351    USD
KEYS    AWS Transfer Family
BLENDEDCOST 208.8   USD
KEYS    Amazon EC2 Container Registry (ECR)
BLENDEDCOST 0.2636971622    USD
KEYS    EC2 - Other
BLENDEDCOST 325.4630384796  USD
KEYS    Amazon Elastic Compute Cloud - Compute
BLENDEDCOST 694.4962624953  USD
KEYS    Amazon Elastic Container Service for Kubernetes
BLENDEDCOST 69.890509083    USD
KEYS    Amazon Elastic File System
BLENDEDCOST 0.0000002652    USD
KEYS    Amazon Elastic Load Balancing
BLENDEDCOST 73.2040001769   USD

我期望输出如下,BLENDEDCOST 重复项应该被删除,并且 KEYS 应该出现在新行中。将行转换为列并删除重复项?

AWS CloudShell  0.0000026589    USD
AWS CloudTrail  0.000001    USD
AWS Config  46.921  USD
AWS Glue    355.7073555 USD
AWS Key Management Service  0.10418545  USD
AWS Lambda  0.0000002605    USD
AWS Migration Hub Refactor Spaces   0   USD
AWS Secrets Manager 2.049678895 USD
AWS Security Hub    0.028892556 USD
AWS Service Catalog 0   USD
AWS Step Functions  0.0000000031    USD
AWS Support (Business)  246.2376325 USD
AWS Systems Manager 0.000351    USD
AWS Transfer Family 208.8   USD
Amazon EC2 Container Registry (ECR) 0.2636971622    USD
EC2 - Other 325.4630385 USD
Amazon Elastic Compute Cloud - Compute  694.4962625 USD
Amazon Elastic Container Service for Kubernetes 69.89050908 USD
Amazon Elastic File System  0.0000002652    USD
Amazon Elastic Load Balancing   73.20400018 USD
Amazon Elastic MapReduce    2.28898852  USD
Amazon Glacier  0.0000000025    USD
Amazon GuardDuty    7.367077065 USD
Amazon Inspector    1   USD
Amazon Location Service 0   USD
Amazon Relational Database Service  388.1651428 USD
Amazon Route 53 0.508976    USD
Amazon Simple Notification Service  0.2022960904    USD
Amazon Simple Queue Service 0   USD
Amazon Simple Storage Service   3.338266835 USD
Amazon Simple Workflow Service  0.0000000015    USD
Amazon Virtual Private Cloud    147.4110373 USD
AmazonCloudWatch    60.12971039 USD
CloudWatch Events   0.000048    USD
CodeBuild   29.71339194 USD

答案1

使用 GNU sed

sed -z 's/\nBLENDEDCOST//g'

或者perl

perl -0777pe 's/\nBLENDEDCOST//g'

为了匹配您的输出,您需要将“混合成本”添加到标题并删除第二行,然后删除USD例如管道输出到:

sed '1s/$/\BLENDEDCOST/; 2d; s/[[:blank:]]\+USD$//'

总共:

some_command | perl -0777pe 's/\nBLENDEDCOST//g;' | sed '1s/$/\tBLENDEDCOST/; 2d; s/[[:blank:]]\+USD$//'

输出:

GROUP DEFINITIONS   SERVICE DIMENSION   BLENDEDCOST
KEYS    AWS CloudShell 0.0000026589
KEYS    AWS CloudTrail 0.000001
KEYS    AWS Config 46.921
KEYS    AWS Glue 355.70735552
KEYS    AWS Key Management Service 0.10418545
KEYS    AWS Lambda 0.0000002605
KEYS    AWS Migration Hub Refactor Spaces 0
KEYS    AWS Secrets Manager 2.0496788951
KEYS    AWS Security Hub 0.028892556
KEYS    AWS Service Catalog 0
KEYS    AWS Step Functions 0.0000000031
KEYS    AWS Support (Business) 246.2376324993
KEYS    AWS Systems Manager 0.000351
KEYS    AWS Transfer Family 208.8
KEYS    Amazon EC2 Container Registry (ECR) 0.2636971622
KEYS    EC2 - Other 325.4630384796
KEYS    Amazon Elastic Compute Cloud - Compute 694.4962624953
KEYS    Amazon Elastic Container Service for Kubernetes 69.890509083
KEYS    Amazon Elastic File System 0.0000002652
KEYS    Amazon Elastic Load Balancing 73.2040001769

非常简单的替代方案,其输出应该相当容易修复:

paste - -

答案2

pcregrep

pcregrep -M -o1 -o2 '^KEYS\h+(.*)\nBLENDEDCOST(\h.*)' your-file

或者使用标准sed

sed -n '
  /^KEYS/ {
    N
    s/^KEYS[[:blank:]]\{1,\}\(.*\)\nBLENDEDCOST\([[:blank:]]\)/\1\2/p
    t
    D
  }' your-file

(不照顾重复项因为从你的描述中不清楚它们会是什么样子)。

相关内容