I run the command grep to get certain output and store it into a variable. The output of grep command is:
5.3.1-6.2011171513.ASMOS6
but I need to only parse out 5.3.1-6 and store that version in a variable. How can I do that?
What about this?
TEXT=5.3.1-6.2011171513.ASMOS6
RES=$(echo $TEXT | cut -d. -f-3)
We cut the string on . and then get the first 3 pieces.
With awk
RES=$(echo $TEXT | awk -F. 'OFS=FS {print $1,$2,$3}')
OFS=FS meaning we get the delimiter to print that was defined with -F.
cut or awk are easier to handle and understand. If you are done, you can mark the question as answered. Cheers!sed with regex (\\d)...?As you tag says linux, I'll assume that you have a modern grep that supports the -o option.
You can do
var=$(grep -o 'regex' file)
To capture just the regex to a variable.
Unfortunately, I don't understand the problem you're having getting just 5.3.1-6, you have to edit your question to show us the regex youare currently using.
IHTH