Sunday, May 12, 2024
1
rated 0 times [  1] [ 0]  / answers: 1 / hits: 681  / 1 Year ago, sun, january 8, 2023, 1:19:39

I want just know how to extract the value from the variable in shell script.
following is the code:



var=Volume: 0: 100% 1: 100% 


or



var=Volume: 0: 35% 1: 35%  


I want to extract 100 or 35 depends value from the variable and store it into another variable.
so how can i do this?


More From » command-line

 Answers
7

I think this is what the command you looking for,



sed 's/.* (.*)%.*/1/g'


Example:



$ echo 'var=Volume: 0: 35% 1: 35%' | sed 's/.* (.*)%.*/1/g'
35

$ echo 'Volume: 0: 35% 1: 35%' | sed 's/.* (.*)%.*/1/g'
35

$ var="Volume: 0: 35% 1: 35%"
$ echo $var | sed 's/.* (.*)%.*/1/g'
35


This command will display the first number 35 or 100 before the first % mark.


[#25596] Sunday, January 8, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ersoggles

Total Points: 69
Total Questions: 111
Total Answers: 111

Location: Vanuatu
Member since Sun, Oct 2, 2022
2 Years ago
;