Thread: awk question

  1. #1
    Registered User
    Join Date
    Apr 2010

    awk question

    Hello to All,

    I have a problem. I have a list like below:

    A_1_ccc_2_0 = 2
    A_3_ccc_4_1 = 1
    A_2_ccc_2_2 = 4

    and I would like to store this in an array in awk in a way where

    a[1,2,0] = 2
    a[3,4,1] = 1
    a[2,2,2] = 4
    my problem is how to retrieve the numbers from the string.

    Many thanks

  2. #2
    Registered User
    Join Date
    Oct 2008
    Perhaps something like:
    awk -F"[^0-9]*" '{s="";for(i=1;i<NF;i++) s=sprintf("%s%s",s?s",":"",$i);a[s]=$NF}' listfile

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Use split() and _ as the separator.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. mistake of using awk
    By lehe in forum Linux Programming
    Replies: 6
    Last Post: 04-02-2009, 04:41 PM
  4. Alice....
    By Lurker in forum A Brief History of
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM