Unix administration - awk to find next column in sequence

This is Interesting: Free IT Magazines  
Home > Archive > Unix administration > February 2006 > awk to find next column in sequence





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author awk to find next column in sequence
cconnell_1@lycos.com

2006-01-31, 7:21 pm

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

ted@loft.tnolan.com (Ted Nolan

2006-01-31, 7:21 pm

In article <1138724065.332516.225260@g14g2000cwa.googlegroups.com>,
<cconnell_1@lycos.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


cconnell_1@lycos.com

2006-01-31, 7:21 pm

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.

Hajo Ehlers

2006-01-31, 7:21 pm

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

ted@loft.tnolan.com (Ted Nolan

2006-01-31, 7:21 pm

In article <1138743164.345750.152570@g14g2000cwa.googlegroups.com>,
<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.
>


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

Theo v. Werkhoven

2006-01-31, 7:21 pm

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"
cconnell_1@lycos.com

2006-02-01, 7:51 am

That works fanstasic. Thanku for the help :-)

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com