0

I have a .config file, In that file there is one variable MinNumber which contains some strings like JAVA3.5, JAVA4.0, JAVA5. Now I want to find minimum Java number using shell script. MinNumber = "JAVA3.5, JAVA4.0, JAVA5";

Logic should be first it should find MinNumber in a.config file and then check minimum java number.

Kaan
  • 5,434
  • 3
  • 19
  • 41
james
  • 39
  • 2
  • 7
  • 1
    What did you try, show us your code. – Romeo Ninov Dec 27 '19 at 08:08
  • Actually I'm not getting logic for the same ..but i was trying to use awk command. – james Dec 27 '19 at 08:09
  • split string by comma, remove `JAVA` string and compare numbers. – Romeo Ninov Dec 27 '19 at 08:11
  • @james : Is every variable defined in a separate line in the config file? Then `grep` for the line, and take out the arguments, for instance using `cut`. – user1934428 Dec 27 '19 at 08:14
  • and how to find that variable 'MinNumber ' in large file a.config file, we want to do operations on value of 'MinNumber' only. – james Dec 27 '19 at 08:15
  • from your last comment @james it looks like you could benefit from https://stackoverflow.com/questions/893585/how-to-parse-xml-in-bash , that way you avoid problems with eventual multi-line MinNumer – malarres Dec 27 '19 at 08:33
  • Sorry i didn't find that link relatable to my question. Now my question is very simple How to get value of MinNumber variable from a file which contains below information: – james Dec 27 '19 at 08:37

2 Answers2

0
$ cat file
<Connector JAVAEnabled="true" URIEncoding="UTF-8" acceptCount="100" maxHttpHeaderSize="8192" maxSpareThreads="25" maxThreads="25" minSpareThreads="10" port="8444" protocol="HTTP/1.1" scheme="https" secure="true" server=" " MinNumber="CPP1.2, JAVA3.5, JAVA4.0, JAVA5"/>

One way:

$ grep -Po '(?<=MinNumber=")[^"]+' file | sed "s/,/\n/g; s/ //g"  | sed -r 's/([^0-9\.]+)(.*)/\1 \2/' | awk '$2<min{min=$2;x=$1$2;}END{print x}' min=999
CPP1.2

Using the positive look behind((?<=MinNumber=")) expression of Perl in grep, we extract the value of MinNumber. Using sed, we split the variable into multiple lines, and then using awk we got the minimum.

Guru
  • 16,456
  • 2
  • 33
  • 46
0

Here's an example showing how to extract the lowest string by just relying on standard text sorting.

First, set a shell variable to have a working example:

~ $ MinNumber="JAVA3.5, JAVA4.0, JAVA5"

Then use tr to replace single spaces with newlines, use sed to strip the commas, sort to order the output, then head to limit the line output to one line:

~ $ echo $MinNumber | tr ' ' '\n' | sed 's/,//' | sort | head -1
JAVA3.5

Here's another sample input with more "Java" values and is also out of sequence:

~ $  MinNumber="JAVA9, JAVA3.5, JAVA3, JAVA4.0, JAVA5"
~ $ echo $MinNumber | tr ' ' '\n' | sed 's/,//' | sort | head -1
JAVA3
Kaan
  • 5,434
  • 3
  • 19
  • 41