CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2005
    Posts
    43

    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?

  2. #2
    Join Date
    Jul 2005
    Posts
    43

    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?

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    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
  •  





Click Here to Expand Forum to Full Width

Featured