awk to find next column in sequence - Unix

This is a discussion on awk to find next column in sequence - Unix ; Hello, I have a string containing the following data in columns 000001 000002 000003 000004 000005 I have one of the values for the column defned e.g. 000002, how to I pass this value to awk to return me the ...

+ Reply to Thread
Results 1 to 7 of 7

Thread: awk to find next column in sequence

  1. awk to find next column in sequence

    Hello,
    I have a string containing the following data in columns

    000001 000002 000003 000004 000005

    I have one of the values for the column defned e.g. 000002, how to I
    pass this value
    to awk to return me the value of the next column?

    e.g. I pass the string to awk with the value 000002 and I would like
    000003
    returned?

    Thanks


  2. Re: awk to find next column in sequence

    In article <1138724065.332516.225260@g14g2000cwa.googlegroups. com>,
    wrote:
    >
    >
    >Hello,
    >I have a string containing the following data in columns
    >
    >000001 000002 000003 000004 000005
    >
    >I have one of the values for the column defned e.g. 000002, how to I
    >pass this value
    >to awk to return me the value of the next column?
    >
    >e.g. I pass the string to awk with the value 000002 and I would like
    >000003
    >returned?
    >
    >Thanks
    >


    I'm not entirely sure I follow your whole concept, but the answer to

    e.g. I pass the string to awk with the value 000002 and I would like
    000003 returned

    is

    #! /bin/sh
    echo "000002 " | awk '{
    printf("%06d\n", $1 + 1)
    }'



    Ted



  3. Re: awk to find next column in sequence

    hello,
    I see what the last command does but I want it to get the next column
    in the sequence as opposed to adding 1 to the value. so
    if i have a string "1 2 3 6 8 9", I have the value 3, I to pass the
    value to the string and then awk to work out what column it is in the
    string, then to return the value of the item in the column after it.
    So I give the string the value 3, awk will work out this is in column
    3, add 1 to it then return me the value in the next column which is 6.


  4. Re: awk to find next column in sequence

    cconnell_1@lycos.com wrote:
    > hello,
    > I see what the last command does but I want it to get the next column
    > in the sequence as opposed to adding 1 to the value. so
    > if i have a string "1 2 3 6 8 9", I have the value 3, I to pass the
    > value to the string and then awk to work out what column it is in the
    > string, then to return the value of the item in the column after it.
    > So I give the string the value 3, awk will work out this is in column
    > 3, add 1 to it then return me the value in the next column which is 6.


    1)
    transpose 1 2 3 6 8 9 to
    1
    2
    3
    6
    8
    9

    Can be done by changing the record seperator

    2)
    If Field $1 equals WhatIAmLookingFor print next line

    hth
    Hajo


  5. Re: awk to find next column in sequence

    In article <1138743164.345750.152570@g14g2000cwa.googlegroups. com>,
    wrote:
    >
    >
    >hello,
    >I see what the last command does but I want it to get the next column
    >in the sequence as opposed to adding 1 to the value. so
    >if i have a string "1 2 3 6 8 9", I have the value 3, I to pass the
    >value to the string and then awk to work out what column it is in the
    >string, then to return the value of the item in the column after it.
    >So I give the string the value 3, awk will work out this is in column
    >3, add 1 to it then return me the value in the next column which is 6.
    >


    OK,

    How about this then:


    #! /bin/sh
    echo "1 2 3 6 8 9" | awk '{
    found=0
    for(i = 1; i <= NF; i++) {
    if(desideratum == $i) {
    printf("value %s is in col %d\n", desideratum, i);
    printf("next col (%d) is valued %d\n", i+1, $(i+1));
    found = 1
    break
    }
    }

    if(!found) {
    printf("Value %s was not in the input\n", desideratum);
    }

    }' desideratum=3


  6. Re: awk to find next column in sequence

    The carbonbased lifeform cconnell_1@lycos.com inspired comp.unix.admin with:
    > Hello,
    > I have a string containing the following data in columns
    >
    > 000001 000002 000003 000004 000005
    >
    > I have one of the values for the column defned e.g. 000002, how to I
    > pass this value
    > to awk to return me the value of the next column?
    >
    > e.g. I pass the string to awk with the value 000002 and I would like
    > 000003
    > returned?


    #v+
    $ export NUM=000002
    $ echo "000001 000002 000003 000004 000005" |\
    awk -v num=$NUM '{
    for (i=1;i<=NF;i++)
    if ($i == num)
    {print $(i+1)}
    }'

    000003
    #v-

    Theo
    --
    theo at van-werkhoven.nl ICQ:277217131 SuSE Linux
    linuxcounter.org: 99872 Jabber:muadib at jabber.xs4all.nl AMD XP3000+ 1024MB
    "ik _heb_ niets tegen Microsoft, ik heb iets tegen
    de uitwassen *van* Microsoft"

  7. Re: awk to find next column in sequence

    That works fanstasic. Thanku for the help :-)


+ Reply to Thread