1823920 Members
3278 Online
109667 Solutions
New Discussion юеВ

Re: Reading a file

 
SOLVED
Go to solution
dude70_1
Advisor

Reading a file

Hi Guys,

I have a file where I have some properties. I want to read their value by property name like..

p1=value1
p2=value2

one time I want to read value of just p2. How do I do it?

Thanks in adv.
12 REPLIES 12
Karthik S S
Honored Contributor

Re: Reading a file

I am not quite understanding what you need. Can you attach a sample file and explain us again what exactly do you want to do with that file?

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
dude70_1
Advisor

Re: Reading a file

Thanks Karthik,

I have this property file.

key1=value
key2=my value 2
key3=my value 3


In my script I want to know the values of the peroperties. I can do that with
"fgrep -i key* abc.txt"
but it returns the whole line. what I want is to retrive just the value after "key1=" in my script and assign to another variable. is there some other power utility for that?


Michael Schulte zur Sur
Honored Contributor

Re: Reading a file

Hi Dude70,

look into your other thread,

Michael


her, what I wrote:
Hi Dude70,

try this:

grep "^p2=" file

do you want to set the variable in the script or just see, what is it?

the value you get by:
string=`grep "^p2=" file`
value=`expr "${string}" : ".*=\(.*\)"`

greetings,

Michael
Laurent Menase
Honored Contributor

Re: Reading a file

SAVIFS="$IFS"

while read a b
do
if [ "$a" = key1 ]
then
result=$b
break
done
IFS="$SAVIFS"


Or you can do:

res=$(awk -F= '/key1/{print $2 }')
Laurent Menase
Honored Contributor

Re: Reading a file


SAVIFS="$IFS"

while read a b
do
if [ "$a" = key1 ]
then
result=$b
break
done IFS="$SAVIFS"

Karthik S S
Honored Contributor

Re: Reading a file

grep key2 123 |awk -F"=" '{print $2}'

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
dude70_1
Advisor

Re: Reading a file

Thanks Guys,

I dont want any while loops. just read the value of key1 ie "value" and assign it to another var within script myscript.sh.

you have the file name(file.txt) that contains key1=value

some single line command will be helpful.

Thanks.
dude70_1
Advisor

Re: Reading a file

Hi Karthik,
Where do you provide the file name here.

grep key2 123 |awk -F"=" '{print $2}'

Thanks.
Karthik S S
Honored Contributor
Solution

Re: Reading a file

Sorry about that. It should read,

grep key2 your_file_name |awk -F"=" '{print $2}'


I tested with a file named "123" and pasted the command here :-))

-Karthik S S
For a list of all the ways technology has failed to improve the quality of life, please press three. - Alice Kahn
dude70_1
Advisor

Re: Reading a file

I got it thanks karthik!
Michael Schulte zur Sur
Honored Contributor

Re: Reading a file

Hi Dude70,

have you tried my commands?

greetings,

Michael
dude70_1
Advisor

Re: Reading a file

Hi Mike,

Thanks. It worked!