英文:
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 grep
和Stack 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论