Hash
Hash

Reputation: 8040

Read a property file and get value in .sh

I know how to read a file and get all text but i need to know how to get only the value i need.

ex:-

.properties file has,

runId=A667B

I just need to get A667B value.

Upvotes: 0

Views: 1092

Answers (3)

Armali
Armali

Reputation: 19395

. .properties

Thereafter, the value can be obtained through $runId.

Upvotes: 1

John1024
John1024

Reputation: 113924

Here is a pure-shell method:

IFS="=" read -r ignore var <file.properties

After executing the above the variable var has the value A667B.

The above works by reading in to variables from the file. Normally, the shell expects variables to be separated by whitespace. However, in this case, we tell it that the variables will be separated by an equal sign. We do this by assigning the IFS variable to have the value of =.

The -r option to read tells it to treat any backslashes in the input file as literal backslashes, not as characters with special meaning. If you want the special meanings, like \t being tab, then remove that option.

The above also sets the shell variable ignore to have the value runId. Since you said you didn't want that, just ignore the variable named ignore.

The double quotes in the code above are actually unnecessary. I put them there to try to make the code more clear. The following line may look strange but it works just as well:

IFS== read -r ignore var <file.properties

Upvotes: 0

Guru
Guru

Reputation: 17004

One way:

awk -F= '$1=="runId"{print $2}' file

Upvotes: 1

Related Questions