bash - How to retrieve digits including the separator "." -


i using grep string this: ans_length=266.50 use sed digits: 266.50

this full command: grep --text 'ans_length=' log.txt | sed -e 's/[^[[:digit:]]]*//g'

the result : 26650

how can line changed result still shows separator: 266.50

you don't need grep if going use sed. use sed' // match lines need print.

sed -n '/ans_length/s/[^=]*=\(.*\)/\1/p' log.txt 
  • -n suppress printing of lines not match /ans_length/
  • using captured group print value next = sign.
  • p flag @ end allows print lines matches our //.

if grep happens support -p option can do:

grep -op '(?<=ans_length=).*' log.txt 
  • (?<=...) look-behind construct allows match lines need. requires -p option
  • -o allows print value part.

Comments

Popular posts from this blog

java - WrongTypeOfReturnValue exception thrown when unit testing using mockito -

php - Magento - Deleted Base url key -

android - How to disable Button if EditText is empty ? -