Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert to columns 2

Status
Not open for further replies.

sunixadm

Technical User
Sep 20, 2001
73
DE
Hi,

I have the following data in a file:
11/01/04
252.37
286.42
34.05

11/02/04
268.19
307.22
39.03

11/03/04
300.27
314.35
14.08

I need the data to look like this:

11/01/04 11/02/04 11/03/04
252.37 268.19 300.27
286.42 307.22 314.35
34.05 39.03 14.08


The file grows daily by 4 entries.
Any help is appreciated!

Thanks,

-Joe
 
Code:
BEGIN { RS = "" }

4==NF {
  for (i=1; i<5; i++)
    a[i] = a[i] sprintf( "%9s", $i)
}

END {
  for (i=1; i<5; i++)
    print a[i]
}
 
A starting point:
awk '{a[NR%5]=a[NR%5]" "$0}END{for(i=1;i<5;++i)print substr(a,2)}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks futurelet,

your solution was exactly what I needed!

Thanks PHV,

Your solution worked as well!

Thanks to both of you for quick responses.

-Joe
 
futurelet,

I guess I don't know awk as well as I thought I did.

I tried to modify your code to work with my modified output and haven't been able to find the right answer.

The data in my file looks like this:

11/01/04
252.37
286.42

11/02/04
268.19
307.22

11/03/04
300.27
314.35

Thanks!

-Joe
 
awk '{a[NR%4]=a[NR%4]" "$0}END{for(i=1;i<5;++i)print substr(a,2)}' /path/to/input

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Oops, hit submit too fast ...
awk '{a[NR%4]=a[NR%4]" "$0}END{for(i=1;i<4;++i)print substr(a,2)}' /path/to/input

Or
Code:
BEGIN { RS = "" }

3==NF {
  for (i=1; i<4; i++)
    a[i] = a[i] sprintf( "%9s", $i)
}

END {
  for (i=1; i<4; i++)
    print a[i]
}

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi PHV,

Thanks again for a quick response.

The first solution works but the second doesn't :-(

I run awk -f awkfile INPUT-FILE

and get no output.

Thanks,

Joe
 
Code:
BEGIN { nf = 3; RS = "" }

nf==NF {
  for (i=1; i<=nf; i++)
    a[i] = a[i] sprintf( "%9s", $i)
}

END {
  for (i=1; i<=nf; i++)
    print a[i]
}
 
Thanks for the reply futurelet but, still no out put. Any tips??

Thanks,

-Joe
 
When [tt]RS[/tt] is set to [tt]""[/tt], it tells Awk that one or more empty
lines should be the record separator. The problem with this is that if the
line isn't really empty but instead contains blanks or tabs, the line won't
be seen as a record separator.

Brian Kernighan's Awk doesn't handle a [tt]RS[/tt] longer than one character.
Mawk, Gawk, and perhaps others, do.

So make sure the "empty" lines are really empty or use an Awk that can handle
this:
Code:
BEGIN { nf = 3
  # Empty lines are record separators.
  RS = ""
  # Lines containing nothing more than whitespace
  # are record separators.
  RS = "\n([ \t]*\n)+"
}

nf==NF {
  for (i=1; i<=nf; i++)
    a[i] = a[i] sprintf( "%9s", $i)
}

END {
  for (i=1; i<=nf; i++)
    print a[i]
}

Perhaps it would be best not to rely on multi-line records:
Code:
BEGIN { nf = 3 }

# Trim trailing whitespace.
{ sub( /[ \t]+$/, "" ) }

$0 {
  i = count % nf
  a[i] = a[i] sprintf( "%9s", $0)
  count++; 
}

END {
  for (i=0; i<nf; i++)
    print a[i]
}
 
Give this a try. Should work no matter how many dates you have or data items per date. Will handle different numbers of data items per date.
Code:
!/^[0-9]/ {next}
/^[0-9][0-9]\// {
    i++
    j = 0
}
{
    data[i,++j] = $0
    if (j > maxj) maxj = j
}
END {
    for (k=1; k<=maxj; k++)
        for (m=1; m<=i; m++)
            printf "%-10s%s", data[m,k], m<i? "": "\n"
}
HTH
 
mikevh wrote:
> Should work no matter how many dates you have or
> data items per date.

Good idea.
Code:
# Trim trailing whitespace.
{ sub( /[ \t]+$/, "" ) }

# If line isn't blank...
$0 { i++
     a[i] = a[i] sprintf( "%9s", $0)
     next
}

# Line is blank; reset field index.
{ i = 0 }

END { for (i=1; i in a; i++)
        print a[i]
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top