Link to home
Start Free TrialLog in
Avatar of mxgong
mxgongFlag for Australia

asked on

Help Shell script

Hi All

I would like to write a shell script to retrieve a value.

for example: there is a string: "no of customer": 3

I need a shell script to retrieve "3".

Can anyone help me ? I am new in shell.

thanks
Avatar of Papertrip
Papertrip
Flag of United States of America image

Depending on all the other lines in the file, you can do the following
awk -F: '{print $2}' filename

Open in new window

That will print the 2nd field using : as a delimiter of each line in filename.

Really depends on what else is in the file you are parsing.
Avatar of mxgong

ASKER

Hi Papertrip

Thanks for your reply, but this is not file.

It's the result of the command.

eg: /usr/lib/result.sh, then I got the "no of customer": 3

But I need a shell script for retrieving "3" from the result.

Thanks
Avatar of mxgong

ASKER

The result from command will be:

{
 "no of customer": 3
}



Avatar of omarfarid
try this

grep "no of customer" filename | awk -F: '{ print $2 }'

#!/bin/bash

/usr/lib/result.sh | grep '^\"' | awk -F ': ' '{print $2}'

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Papertrip
Papertrip
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mxgong

ASKER

thanks Papertrip

the output is: [space]3  

there is a whitespace before 3, is it able to remove the space?

Thanks
Yep use my last syntax at http:#37074470

I overlooked the space in my first answer.
[root@broken ~]# cat asdf
"blah": a
"blah": b
[root@broken ~]# grep '^"' asdf |awk -F': ' '{print $2}'
a
b
[root@broken ~]#

Open in new window

Avatar of mxgong

ASKER

Thanks it works fine now.

Regards
Avatar of mxgong

ASKER

perfect, thanks