Avatar of deharvy
deharvy

asked on 

Parse Data to Get Value

Here is my data:

type=bike,color=red,price=100
type=car,style=sport,color=blue,price=30000
type=plane,color=silver,price,1000000

Need a simple script that grabs value of color. Output should be:

red
blue
silver

Thanks for your assistance.
Shell ScriptingLinuxLinux OS Dev

Avatar of undefined
Last Comment
woolmilkporc
Avatar of lexlythius
lexlythius
Flag of Argentina image

If fields were in a fixed order, this would suffice:
awk -F',' '{ print $2}' data.txt | awk -F'=' '{ print $2}'

Open in new window

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of a1j
a1j
Flag of United States of America image

Correction:

cat file | sed -n -e '/color=/s/.*color=\([^,]*\),\?.*/\1/p' > outfile

Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

If there's no fixed order:

awk -F"color=" '{print $2}' inputfile | sed 's/,.*//'

wmp
Avatar of lexlythius
lexlythius
Flag of Argentina image

With gawk only (no piping):

gawk '{ if (match($1, /color=(\w+)/, matches) != 0) print matches[1]; }' INPUTFILE

Open in new window

SOLUTION
Avatar of tel2
tel2
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of a1j
a1j
Flag of United States of America image

Well i might be wrong but if you have line with color= missing in it you will print the whole line in your solution and you will print nothing in mine.
Avatar of tel2
tel2
Flag of New Zealand image

Hi a1j,

Good point.  I'll try again.

Is there any reason why your solution:
    cat file | sed -n -e '/color=/s/.*color=\([^,]*\),\?.*/\1/p' > outfile
shouldn't be simplified to this:
    sed -n 's/.*color=\([^,]*\).*/\1/p' file >outfile
?
It works with the data supplied, and also if there is no color= in the line.
Avatar of a1j
a1j
Flag of United States of America image

This should work fine. It is all matter of style/habits now.
Avatar of deharvy
deharvy

ASKER

Final solution:

sed -n 's/.*color=\([^,]*\).*/\1/p' file >outfile

------------------

awk -F appears to only support one character delimiter.

------------------

Thanks everyone for your responses and your assistance.
Avatar of tel2
tel2
Flag of New Zealand image

OK, thanks a1j.

My habit is to be as lazy/simple/concise as possible (except where that causes significant problems).
Avatar of tel2
tel2
Flag of New Zealand image

No problem, deharvy.

We'll send you the bill.
Avatar of woolmilkporc
woolmilkporc
Flag of Germany image

>> awk -F appears to only support one character delimiter.  <<

Either you have a very (very!) strange implementation of awk or you didn't even test my solution.
 
Linux
Linux

Linux is a UNIX-like open source operating system with hundreds of distinct distributions, including: Fedora, openSUSE, Ubuntu, Debian, Slackware, Gentoo, CentOS, and Arch Linux. Linux is generally associated with web and database servers, but has become popular in many niche industries and applications.

71K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo