Operating System - Linux
1827889 Members
1539 Online
109969 Solutions
New Discussion

extract data from a "," delimitted string

 
SOLVED
Go to solution
lawrenzo_1
Super Advisor

extract data from a "," delimitted string

Hi everyone,

I run a command and the output looks like this:

name=SERV-xadr-99-59-SN11D0FFF,type_model=9119-590,serial_num=51D0F5F,ipaddr=1.1.1.252,ipaddr_secondary=1 ,etc,etc

I am interested in extracting different fields from this output however for this thread I want to display:

SERV-xadr-99-59-SN11D0FFF from name=SERV-xadr-99-59-SN11D0FFF

I have attempted this by using awk and match with no results:

`$sshcmd` |awk -F ',' '{match($1, "name=[^\,]*")}{name = substr($0, RSTART, RLENGHT)};{print name}'

any ideas or a better method?

thanks

Chris.
hello
6 REPLIES 6
James R. Ferguson
Acclaimed Contributor
Solution

Re: extract data from a "," delimitted string

Hi Chris:

# `$sshcmd` | awk -F ',' '{split($1,a,/=/);print a[2]}'

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: extract data from a "," delimitted string

Thanks James,

It didnt work so I read a bit and found out how to use split more effectively and:

`$sshcmd` |awk -F ',' '{split($1,a,"=");print a[2]}'

much appreciated.

Chris.
hello
Steven Schweda
Honored Contributor

Re: extract data from a "," delimitted string

There are probably too many ways to do things
like this to count them all. Which program
would you like to use?

> any ideas [...]

"sed"?

td192> echo $name | sed -e 's/\,.*//'
SERV-xadr-99-59-SN11D0FFF

echo $name | sed -e 's/[^,]*\,\([^,]*\)\,.*/\1/'
type_model=9119-590

echo $name | sed -e 's/[^,]*\,[^,]*\,\([^,]*\)\,.*/\1/'
serial_num=51D0F5F

td192> echo $name | sed -e 's/[^,]*\,[^,]*\,[^,]*\,\([^,]*\)\,.*/\1/'
ipaddr=1.1.1.252

> [...] or a better method?

Better? Define good.
lawrenzo_1
Super Advisor

Re: extract data from a "," delimitted string

Thanks Steve I get your point,

both methods work in different ways and I can make use off all the examples given ...

Chris.
hello
James R. Ferguson
Acclaimed Contributor

Re: extract data from a "," delimitted string

Hi (again) Chris:

> It didnt work ...

Oh, you are running AIX :-)

Yes, HP-UX will allow either :

split($1,a,"=")

OR:

split($1,a,/=/);

...whereas on AIX (which I have too) only the first form is correct.

Regards!

...JRF...
lawrenzo_1
Super Advisor

Re: extract data from a "," delimitted string

Thanks James,

That will be good to know as I also operate on HPUX and Linux and have noticed some suttle differences with that.

Chris.
hello