-
March 2nd, 2011, 12:07 AM
#1
Unix Parsing and grouping with AWK
When parsing multiple fields in a csv file using AWK, how do you group by one of the fields and
parse by delimiters?
-
March 2nd, 2011, 06:51 AM
#2
Re: Unix Parsing and grouping with AWK
to clarify
If a file had
tom, 223-2222-4444, randomfield
ivan, 123-2422-4444, random filed
How would you group by the social security while parsing the (comma delimited) fields using awk?
-
March 2nd, 2011, 11:16 PM
#3
Re: Unix Parsing and grouping with AWK
I'm not sure what you mean by parse by delimiters, but if you wanted to sort this file by the SSN-like fields, you could do this with:
Code:
cat test.txt | sort -k2 -t ,
Actually, I would want to get rid of the spaces after the commas too so I would use:
Code:
cat test.txt | sed 's/, /,/g' | sort -k2 -t ,
I very rarely use awk. Almost always I use grep, sed and sort for simple things on command line. They are extremely powerful when piped together. Anything more complicated and perl is usually a faster choice (for me anyway, YMMV).
Best Regards,
BioPhysEngr
http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|