awk question

This is a discussion on awk question within the Linux Programming forums, part of the Platform Specific Boards category; 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 ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    79

    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
    christos

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Perhaps something like:
    Code:
    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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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 Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21