Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using awk to split line with multiple string delimiters

Tags:

bash

awk

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?

like image 812
Brinley Avatar asked Aug 07 '17 17:08

Brinley


People also ask

Can awk use multiple field separators?

As you can see, you can combine more than one delimiter in the AWK field separator to get specific information.

Which function in awk is used to divide a string into pieces separated by the field separator and store the pieces in an array?

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.


1 Answers

$ 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'
like image 70
Ed Morton Avatar answered Oct 22 '22 16:10

Ed Morton