We help IT Professionals succeed at work.

Help Shell script

mxgong
mxgong asked
on
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
Comment
Watch Question

Top Expert 2011

Commented:
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.

Author

Commented:
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

Author

Commented:
The result from command will be:

{
 "no of customer": 3
}



BRONZE EXPERT
Top Expert 2007

Commented:
try this

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

Top Expert 2011

Commented:
#!/bin/bash

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

Open in new window

Top Expert 2011
Commented:
Actually you shouldn't need to escape the " within single quotes, but either way works.

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

Open in new window

Author

Commented:
thanks Papertrip

the output is: [space]3  

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

Thanks
Top Expert 2011

Commented:
Yep use my last syntax at http:#37074470

I overlooked the space in my first answer.
Top Expert 2011

Commented:
[root@broken ~]# cat asdf
"blah": a
"blah": b
[root@broken ~]# grep '^"' asdf |awk -F': ' '{print $2}'
a
b
[root@broken ~]#

Open in new window

Author

Commented:
Thanks it works fine now.

Regards

Author

Commented:
perfect, thanks

Explore More ContentExplore courses, solutions, and other research materials related to this topic.