Reputation: 105
Using bash script I need to search string like version = '1.8.1-SNAPSHOT'
from text file and get value '1.8.1-SNAPSHOT'
into variable.
I tryed to use the next code, but no result:
version=$(grep -P "version\s?=\s?'([a-zA-Z.\d-]){5,20}?'" file.txt)
regex="'([a-zA-Z.\d-]){5,30}?'"
value=`expr match "$version" '([a-zA-Z.\d-]){5,30}?'`
What is wrong and are there another way?
Some of text file:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath('org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE')
}
}
springBootVersion = '2.0.7.RELEASE'
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
// for Glassfish
//apply plugin: 'war'
jar {
baseName = 'work-space'
version = '1.8.1-SNAPSHOT'
}
Desired string need to be '1.8.1-SNAPSHOT'
into variable for the next manipulation.
Upvotes: 1
Views: 737
Reputation: 626689
You may use
version="$(grep -oP "version\s*=\s*'\K[^']+" file)"
See the online demo.
As you are using P
option, I assume you may use PCRE regex with your grep
. To output the match, you also need to add o
option.
The regex you need is version\s*=\s*'\K[^']+
:
version
- matches version
substring\s*=\s*
- =
enclosed with 0+ whitespaces'
- a '
char\K
- match reset operator discarding all text matched so far[^']+
- 1 or more chars other than '
Upvotes: 4