我有 300 多行包含来自 Jenkins 插件的文本,格式如下:
Server Sent Events (SSE) Gateway Plugin (sse-gateway): 1.24
Common API for Blue Ocean (blueocean-commons): 1.24.4
Handy Uri Templates 2.x API Plugin (handy-uri-templates-2-api): 2.1.8-1.0
Durable Task Plugin (durable-task): 1.35
Git Pipeline for Blue Ocean (blueocean-git-pipeline): 1.24.0
REST API for Blue Ocean (blueocean-rest): 1.24.4
Terraform Plugin (terraform): 1.0.10
GIT server Plugin (git-server): 1.9
Web for Blue Ocean (blueocean-web): 1.24.0
Bitbucket Pipeline for Blue Ocean (blueocean-bitbucket-pipeline): 1.24.0
我正在寻找一种方法来修剪不必要的文本,使用类似sed
或awk
例如的工具,并得到如下结果:
- plugin-util-api:1.7.0
- blueocean-pipeline-api-impl:1.24.0
- credentials-binding:1.24
- Pipelineworkflow-aggregator:2.6
- hashicorp-vault-plugin:3.6.1
- matrix-project:1.18
- blueocean-display-url:2.4.1
- structs:1.21
答案1
你可以试试这个:
$ sed 's/^.*(\([^()]*\)): \(.*\)$/- \1:\2/' file
- sse-gateway:1.24
- blueocean-commons:1.24.4
- handy-uri-templates-2-api:2.1.8-1.0
- durable-task:1.35
- blueocean-git-pipeline:1.24.0
- blueocean-rest:1.24.4
- terraform:1.0.10
- git-server:1.9
- blueocean-web:1.24.0
- blueocean-bitbucket-pipeline:1.24.0
答案2
$ awk -F'[(): ]+' '{print "-", $(NF-1)":"$NF}' file
- sse-gateway:1.24
- blueocean-commons:1.24.4
- handy-uri-templates-2-api:2.1.8-1.0
- durable-task:1.35
- blueocean-git-pipeline:1.24.0
- blueocean-rest:1.24.4
- terraform:1.0.10
- git-server:1.9
- blueocean-web:1.24.0
- blueocean-bitbucket-pipeline:1.24.0
答案3
解决方案gawk
:
awk -F'): ' -v OFS=':' '{ sub(/.*\(/, "", $1); print " - "$1,$2 }' file
答案4
awk -F ')' '
{
sub(/.*\(/, " - ", $(NF-1))
print $(NF-1) $(NF)
}
' file