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?
As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.
Before splitting the string, patsplit() deletes any previously existing elements in the arrays array and seps . Divide string into pieces separated by fieldsep and store the pieces in array and the separator strings in the seps array.
$ 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'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With