5

I have a file called pet_owners.txt that looks like:

petOwner:Jane,petName:Fluffy,petType:cat
petOwner:John,petName:Oreo,petType:dog
...
petOwner:Jake,petName:Lucky,petType:dog

I'd like to use awk to split the file using the delimiters: 'petOwner', 'petName', and 'petType' so that I can extract the pet owners and pet types. My desired output is:

Jane,cat
John,dog
...
Jake,dog

So far I've tried:

awk < pet_owners.txt -F'['petOwner''petName''petType']' '{print $1 $3}'

but the result is a bunch of newlines.

Any ideas for how I can achieve this?

Brinley
  • 591
  • 2
  • 14
  • 26

3 Answers3

6
$ awk -F'[:,]' -v OFS=',' '{print $2,$6}' file
Jane,cat
John,dog
Jake,dog

As for why your attempt wasn't working, mainly it's because [ and ] in the context of a regular expression are the "bracket expression" delimiters and what goes inside that is a set of characters (which may be individual characters, ranges, lists, and/or classes) so when you wrote:

-F'['petOwner''petName''petType']'

that would set FS to the set of characters p, e, t, etc. not the set of strings petOwner, etc. The multiple internal 's are canceling each other out as you jump in/out of shell for no reason exactly as if you had written -F'[petOwnerpetNamepetType]' given there's no metacharacters in there that the shell would expand.

To set FS to a set of strings (actually regexps so watch out for metachars) would be:

-F'petOwner|petName|petType'
Ed Morton
  • 188,023
  • 17
  • 78
  • 185
4

you can also write the delimiters in this form instead of char set

$ awk -F'pet(Owner|Name|Type):' '{print $2,$4}' file

Jane, cat
John, dog

Jake, dog
karakfa
  • 66,216
  • 7
  • 41
  • 56
0

You can also define what a field is, instead of defining what a separator is. For that you use the FPAT variable, like this:

~ $ awk '{ print $2,$6 }' FPAT="[^,:]+" OFS="," pet_owners.txt
Jane,cat
John,dog

That way you're defining as a field everything that is not a comma or a colon.

Sometimes it makes programs easier.

OFS sets the output field separator to a comma.

valrog
  • 216
  • 1
  • 6