Home > Net >  Regex: How to get the release value?
Regex: How to get the release value?

Time:10-27

I've a file with below name formats:

rzp-QAQ_SA2-5.12.0.38-quality.zip
rzp-TEST-5.12.0.38-quality.zip
rzp-ASQ_TFC-5.12.0.38-quality.zip

I want the value as : 5.12.0.38-quality.zip from the above file names.

I'm trying as below, but not getting the correct value though:

echo "$fl_name" | sed 's#^[-[:alpha:]_[:digit:]]*##'

fl_name is the variable containing file name.

Thanks a lot in advance!

CodePudding user response:

You are matching too much with all the alpha, digit - and _ in the same character class.

You can match alpha and - and optionally _ and alphanumerics

sed -E 's#^[-[:alpha:]] (_[[:alnum:]]*-)?##' file

Or you can shorten the first character class, and match a - at the end:

sed -E 's#^[-[:alnum:]_]*-##' file

Output of both examples

5.12.0.38-quality.zip
5.12.0.38-quality.zip
5.12.0.38-quality.zip

CodePudding user response:

With GNU grep you could try following code. Written and tested with shown samples.

grep -oP '(.*?-){2}\K.*'  Input_file

OR as an alternative use(with a non-capturing group solution, as per the fourth bird's nice suggestion):

grep -oP '(?:[^-]*-){2}\K.*' Input_file

Explanation: using GNU grep here. in grep program using -oP option which is for matching exact matched values and to enable PCRE flavor respectively in program. Then in main program, using regex (.*?-){2} means, using lazy match till - 2 times here(to get first 2 matches of - here) then using \K option which is to make sure that till now matched value is forgotten and only next mentioned regex matched value will be printed, which will print rest of the values here.

CodePudding user response:

It is much easier to use cut here:

cut -d- -f3- file

5.12.0.38-quality.zip
5.12.0.38-quality.zip
5.12.0.38-quality.zip

If you want sed then use:

sed -E 's/^([^-]*-){2}//' file

5.12.0.38-quality.zip
5.12.0.38-quality.zip
5.12.0.38-quality.zip

CodePudding user response:

Assumptions:

  • all filenames contain 3 hyphens (-)
  • the desired result always consists of stripping off the 1st two hyphen-delimited strings
  • OP wants to perform this operation on a variable

We can eliminate the overhead of sub-process calls (eg, grep, cut and sed) by using parameter substitution:

$ f1_name='rzp-ASQ_TFC-5.12.0.38-quality.zip'

$ new_f1_name="${f1_name#*-}"                     # strip off first hyphen-delimited string
$ echo "${new_f1_name}"
ASQ_TFC-5.12.0.38-quality.zip

$ new_f1_name="${new_f1_name#*-}"                 # strip off next hyphen-delimited string
$ echo "${new_f1_name}"
5.12.0.38-quality.zip

On the other hand if OP is feeding a list of file names to a looping construct, and the original file names are not needed, it may be easier to perform a bulk operation on the list of file names before processing by the loop, eg:

while read -r new_f1_name
do
    ... process "${new_f1_name)"

done < <( command-that-generates-list-of-file-names | cut -d- -f3-)

CodePudding user response:

In plain bash:

echo "${fl_name#*-*-}"
  • Related