Professional Web Applications Themes

awk to find next column in sequence - Linux / Unix Administration

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...

Sponsored Links
  1. #1

    Default 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

    Sponsored Links
    cconnell_1@lycos.com Guest

  2. #2

    Default Re: awk to find next column in sequence

    In article <googlegroups.com>,
    <com> wrote: 

    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


    ted@loft.tnolan.com Guest

  3. #3

    Default 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.

    cconnell_1@lycos.com Guest

  4. #4

    Default Re: awk to find next column in sequence

    com wrote: 

    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

    Hajo Guest

  5. #5

    Default Re: awk to find next column in sequence

    In article <googlegroups.com>,
    <com> wrote: 

    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

    ted@loft.tnolan.com Guest

  6. #6

    Default Re: awk to find next column in sequence

    The carbonbased lifeform com inspired comp.unix.admin with: 

    #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"
    Theo Guest

  7. #7

    Default Re: awk to find next column in sequence

    That works fanstasic. Thanku for the help :-)

    cconnell_1@lycos.com Guest

Similar Threads

  1. find a first column value ...
    By BD in forum PERL Modules
    Replies: 3
    Last Post: May 23rd, 05:20 PM
  2. Find a column by name
    By Eagle in forum ASP.NET Data Grid Control
    Replies: 2
    Last Post: May 13th, 10:09 PM
  3. Using sequence name depending on other column
    By Andrus in forum PostgreSQL / PGSQL
    Replies: 7
    Last Post: March 23rd, 10:18 AM
  4. Postgres crashed when adding a sequence column
    By Clive Page in forum PostgreSQL / PGSQL
    Replies: 7
    Last Post: January 24th, 02:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139