For Programmers: Free Programming Magazines  


Home > Archive > AWK > July 2004 > Formatting Problem









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 Formatting Problem
Unixtrekkor

2004-07-09, 3:56 pm

My input looks like so:

0100 2000
0200 2001
0300 2002
0400 2003
..
..
..
2400 2024
0100 2100
0200 2101
0300 2102
0400 2103
..
..
..
2400 2124
0100 2200
0200 2201
0300 2202
0400 2203
..
..
..
..
2400 2224

The output should look like the following:

0100 2000 2100 2200
0200 2001 2101 2201
0300 2002 2102 2202
..
..
..
2400 2024 2124 2224

I need to account for days when the first column is 2300 or 2500. I
have tried using an array. I know I can use awk '/0100/ {print
$NF},but I would have to do that for every hour. Any help would be
appreciated as I work to expand my limited awk knowledge.
Ed Morton

2004-07-09, 3:56 pm



Unixtrekkor wrote:
> My input looks like so:
>
> 0100 2000
> 0200 2001
> 0300 2002
> 0400 2003
> .
> .
> .
> 2400 2024
> 0100 2100
> 0200 2101
> 0300 2102
> 0400 2103
> .
> .
> .
> 2400 2124
> 0100 2200
> 0200 2201
> 0300 2202
> 0400 2203
> .
> .
> .
> .
> 2400 2224
>
> The output should look like the following:
>
> 0100 2000 2100 2200
> 0200 2001 2101 2201
> 0300 2002 2102 2202
> .
> .
> .
> 2400 2024 2124 2224


Something like this will do it:

awk '{info[$1] = info[$1] sep $2}
$1 == "0100" && $1 in info { sep = " " }
END { for (i in info) print $i sep info[$i] }'

You may want to sort the output. There's various ways to do that, but
I'm not suggesting any until I understand what your subsequent statments
mean.

> I need to account for days when the first column is 2300 or 2500. I
> have tried using an array.


I have no idea what you mean by the above sentence.

I know I can use awk '/0100/ {print
> $NF},but I would have to do that for every hour.


I don't see what that has to do with your problem either.

Any help would be
> appreciated as I work to expand my limited awk knowledge.


Regards,

Ed.

Robert Stearns

2004-07-09, 3:56 pm

awk '{t[$1]=t[$1] $2 " "};END{for(i in t)print i " " t[i]}' infile|sort

That should do the trick. Untested. Will handle any (reasonable) number
of values, but is limited by memory.

Unixtrekkor wrote:
> My input looks like so:
>
> 0100 2000
> 0200 2001
> 0300 2002
> 0400 2003
> .
> .
> .
> 2400 2024
> 0100 2100
> 0200 2101
> 0300 2102
> 0400 2103
> .
> .
> .
> 2400 2124
> 0100 2200
> 0200 2201
> 0300 2202
> 0400 2203
> .
> .
> .
> .
> 2400 2224
>
> The output should look like the following:
>
> 0100 2000 2100 2200
> 0200 2001 2101 2201
> 0300 2002 2102 2202
> .
> .
> .
> 2400 2024 2124 2224
>
> I need to account for days when the first column is 2300 or 2500. I
> have tried using an array. I know I can use awk '/0100/ {print
> $NF},but I would have to do that for every hour. Any help would be
> appreciated as I work to expand my limited awk knowledge.


Unixtrekkor

2004-07-14, 3:56 pm

Ed Morton <morton@lsupcaemnt.com> wrote in message news:<iOydnVmRHLCobHPdRVn-ug@comcast.com>...
> Unixtrekkor wrote:
>
> Something like this will do it:
>
> awk '{info[$1] = info[$1] sep $2}
> $1 == "0100" && $1 in info { sep = " " }
> END { for (i in info) print $i sep info[$i] }'
>
> You may want to sort the output. There's various ways to do that, but
> I'm not suggesting any until I understand what your subsequent statments
> mean.
>
[color=darkred]
>
> I have no idea what you mean by the above sentence.


Column one can vary from 2300 to 2500. This may not matter.
>
> I know I can use awk '/0100/ {print
>
> I don't see what that has to do with your problem either.
>

Sorry about that. I was thinking a possible solution might be
awk '/0100/ {print $NF}
awk '/0200/ {print $NF}
awk '/0300/ {print $NF}
etc.
Just thinking out loud and now realize that probably wouldn't work either.

> Any help would be
>
> Regards,
>
> Ed.


I tried the solution and no output was produced. Thanks for your help.

Heuguette
Ed Morton

2004-07-14, 3:56 pm



Unixtrekkor wrote:
> Ed Morton <morton@lsupcaemnt.com> wrote in message news:<iOydnVmRHLCobHPdRVn-ug@comcast.com>...

<snip>
<snip>[color=darkred]
> I tried the solution and no output was produced. Thanks for your help.


Just change the "$i"s to "i"s in the above line:

END { for (i in info) print i sep info[i] }'

Regards,

Ed.

Kenny McCormack

2004-07-14, 3:56 pm

In article <cd3jl6$e4l@netnews.proxy.lucent.com>,
Ed Morton <morton@lsupcaemnt.com> wrote:
>
>
>Unixtrekkor wrote:
><snip>
><snip>

I'm sure that you did get output - just not the output that you wanted.
[color=darkred]
>Just change the "$i"s to "i"s in the above line:
>
>END { for (i in info) print i sep info[i] }'


Yes, this $i stuff is a Perl/shell-ism; avoid it!

(Not, mind you, that $i doesn't mean something and isn't occasionally
useful in AWK - but it is rarely used)

Unixtrekkor

2004-07-21, 8:55 am

Thanks a bunch. That worked perfectly.


Robert Stearns <is@uga.edu> wrote in message news:<ccmrgs$8mq$1@cronkite.cc.uga.edu>...[color=darkred]
> awk '{t[$1]=t[$1] $2 " "};END{for(i in t)print i " " t[i]}' infile|sort
>
> That should do the trick. Untested. Will handle any (reasonable) number
> of values, but is limited by memory.
>
> Unixtrekkor wrote:
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com