I've ben trying to extract only part of string from a file looking like this:
str1=USER_NAME
str2=justAstring
str3=https://product.org/v-4.5-bin.zip
str4=USER_HOME
I need to extract ONLY the version - in this case: 4.5
I did it by grep and then sed but now the output is 4.5-bin.zip
-> grep str3 file.txt
str3=https://product.org/v-4.5-bin.zip
-> echo str3=https://product.org/v-4.5-bin.zip | sed -n "s/^.*v-\(\S*\)/\1/p"
4.5-bin.zip
What should I do in order to remove also the -bin.zip
at the end?
Thanks.
CodePudding user response:
1st solution: With your shown samples, please try following sed
code.
sed -n '/^str3=/s/.*-\([^-]*\)-.*/\1/p' Input_file
Explanation: Using sed
's -n
option which will STOP printing of values by default, to only print matched part. In main program checking condition if line starts from str3=
then perform substitution there. In substitution catching everything between 1st -
and next -
in a capturing group and substituting whole line with it by using \1
and printing the matched portion only by using p
option.
2nd solution: Using GNU grep
you could try following grep
program.
grep -oP '^str3=.*?-\K([^-]*)' Input_file
3rd solution: Using awk
program for getting expected output as per shown smaples.
awk -F'-' '/^str3=/{print $2}' Input_file
4th solution: Using awk
's match
function to get expected results with help of using RSTART
and RLENGTH
variables which get set once a TRUE match is found by match function.
awk 'match($0,/^str3=.*-/){split(substr($0,RSTART,RLENGTH),arr,"-");print arr[2]}' Input_file
CodePudding user response:
If you know the version contains just digits and dots, replace \S
by [0-9.]
. Also, match the remaining characters outside of the capture group to get it removed.
sed -n 's/^.*v-\([0-9.]*\).*/\1/p'