如何在使用bash脚本时正确分割

huangapple go评论55阅读模式
英文:

How to split correctly when using bash script

问题

sql-apolo
sql-sxdf
sql-pokf

英文:

I have a bash command which the result is something like this in my file:

sm-apolo: 2.1-835
sql-apolo: 1.0-2
srmq-processor: 1.0-214

I want to get a result of all items that have sql at the start and only the first part before ":" so need the second part to be removed. Then I want to use the result in choiceParam in Jenkins pipeline to be shown as a drop-down list.
If I run something like this:

names = sh (script: "A command here | grep sql", returnStdout: true).trim()
names_list = names.trim().tokenize("\n")

In the drop-down I get
sql-apolo: 1.0-2

How I can get rid of everything after the name including ": 1.0-2" to show up in the drop down list? something like:

sql-apolo
sql-sxdf
sql-pokf

答案1

得分: 2

使用GNU grep和Perl兼容正则表达式(-P):

grep -Po '^sql.*(?=:)'

或者:

grep -o '^sql[^:]*'

参见:man grepStack Overflow正则表达式FAQ

英文:

With GNU grep and a Perl-compatible regular expression (-P):

grep -Po '^sql.*(?=:)'

or:

grep -o '^sql[^:]*'

See: man grep and The Stack Overflow Regular Expressions FAQ

huangapple
  • 本文由 发表于 2023年2月14日 04:51:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/75441062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定