2

There is a file post_check.ini I need the value set for:

Max_value=2

How would I get the value 2 from Max_value?

Kent
  • 189,393
  • 32
  • 233
  • 301
JumpOffBox
  • 703
  • 3
  • 10
  • 19
  • 1
    Possible duplicate of [bash get string after character](http://stackoverflow.com/questions/15148796/bash-get-string-after-character) – tripleee Feb 18 '16 at 08:17

4 Answers4

4

try

grep -Po '(?<=Max_value=).*' post_check.ini
Kent
  • 189,393
  • 32
  • 233
  • 301
  • This one works with one change of, I added the awk '{print $1}' to get first part else it would return whole line. But what if I want to set back the value I got value=$(grep -Po '(?<=Max_value=).*' /usr/post_check.ini|awk '{print $1}') sed -i -r 's/Max_value=[0-9]+/Max_value=0/g' /usr/master.ini echo "$value" sed -i -r 's/Max_value=[0-9]+/Max_value=$value/g' /usr/master.ini value=$(grep -Po '(?<=Max_value=).*' /usr/master.ini|awk '{print $1}') echo "$value" If I reset it to zero and try to set it back to old value How would I do that ? – JumpOffBox Apr 03 '13 at 17:21
  • @JumpOffBox grep can only extract data. cannot set data. you may need awk/sed to do that. But that would be another new question. You could open a new question. Please don't write big block code in comment, it is hard to read. – Kent Apr 03 '13 at 19:30
2
Max_value=$(sed -n '/^Max_value=\([0-9]*\)$/s//\1/p' post_check.ini)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    Points for the use of an empty pattern after `s` but maybe simply `'s/^Max_value=//p'` would be easier to type and comprehend. – tripleee Feb 18 '16 at 08:18
1

I recommend using crudini which is a dedicated tool to manipulate ini files from shell

value=$(crudini --get /usr/post_check.ini section Max_value)

Details on usage and download at: http://www.pixelbeat.org/programs/crudini/

pixelbeat
  • 30,615
  • 9
  • 51
  • 60
0

You might find it useful to use proper config file parser. Given the following .ini file:

$ cat post_check.ini
[section 1]
Max_value=123
[section 2]
Max_value=456

The following python script will print 123:

import ConfigParser, os
config = ConfigParser.ConfigParser()
config.read('post_check.ini')
print config.get('section 1','Max_value')

This is most reliable and modifiable way to go if you need to work with config files.

Chris Seymour
  • 83,387
  • 30
  • 160
  • 202