GreenLake Administration
- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Convert columns to rows
Operating System - HP-UX
1851362
Members
2985
Online
104058
Solutions
Forums
Categories
Company
Local Language
back
Forums
Discussions
Knowledge Base
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2003 03:06 AM
10-24-2003 03:06 AM
Hi,
I need a help!
I'm trying to convert columns to rows in the way following:
I want to convert:
18 Service_A 25
18 Service_B 38
19 Service_A 48
19 Service_B 68
19 Service_C 19
20 Service_A 47
20 Service_D 58
In:
Date Service_A Service_B Service_C Service_D
18 25 38 0 0
19 48 68 19 0
20 47 58 0 0
Please, Are there commands or scripts to get this goal.
Greetings,
I need a help!
I'm trying to convert columns to rows in the way following:
I want to convert:
18 Service_A 25
18 Service_B 38
19 Service_A 48
19 Service_B 68
19 Service_C 19
20 Service_A 47
20 Service_D 58
In:
Date Service_A Service_B Service_C Service_D
18 25 38 0 0
19 48 68 19 0
20 47 58 0 0
Please, Are there commands or scripts to get this goal.
Greetings,
Christian Aguilar
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-24-2003 04:31 AM
10-24-2003 04:31 AM
Re: Convert columns to rows
Try something like this:
awk '{print $1}' file | sort -u | while read DATE
do
...printf "\n %3d" $DATE
...grep $DATE file | awk '{printf "%3d", $3}'
done
This isn't quite right, but it will get you off of the ground.
awk '{print $1}' file | sort -u | while read DATE
do
...printf "\n %3d" $DATE
...grep $DATE file | awk '{printf "%3d", $3}'
done
This isn't quite right, but it will get you off of the ground.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2003 12:30 AM
10-27-2003 12:30 AM
Re: Convert columns to rows
Christian.
Typo in last line of your results, I think.
Try this:
cat file | awk '
{
results[$1,$2]=$3
services[$2]=1
dates [$1]=1
}
END {
printf "Date "
for (s in services)
printf ("%s ", s)
print
for (d in dates) {
printf ("%d ", d)
for (s in services)
printf ("%d ", results [d,s])
print
}
}'
-- Graham
Typo in last line of your results, I think.
Try this:
cat file | awk '
{
results[$1,$2]=$3
services[$2]=1
dates [$1]=1
}
END {
printf "Date "
for (s in services)
printf ("%s ", s)
for (d in dates) {
printf ("%d ", d)
for (s in services)
printf ("%d ", results [d,s])
}
}'
-- Graham
Computers make it easier to do a lot of things, but most of the things they make it easier to do don't need to be done.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-27-2003 02:08 AM
10-27-2003 02:08 AM
SolutionSample Perl solution:
#!/bin/perl
while (<>) {
($date,$service,$value) = split;
$x{$date.$service} = $value;
$services{$service} += $value;
$dates{$date}++;
}
print "date ";
foreach $s (sort keys %services) { printf ("%15s", $s) };
foreach $d (sort keys %dates ) {
printf ("\n%5s", $d);
foreach $s (sort keys %services) {
printf ("%15d", $x{$d.$s} ) ;
}
}
print "\n";
and a little prettier:
#!/bin/perl
while (<>) {
($date,$service,$value) = split;
$x{$date.$service} = $value;
$services{$service} += $value;
$dates{$date}++;
}
$header = " Date ";
$seperator = "----- ";
$total = "Total ";
foreach $s (sort keys %services) {
$header .= sprintf ("%12s ", $s);
$seperator .= "------------ ";
$total .= sprintf ("%12d ", $services{$s});
}
print $header . "\n" . $seperator;
foreach $d (sort keys %dates ) {
printf ("\n%5s ", $d);
foreach $s (sort keys %services) {
printf ("%12d ", $x{$d.$s} ) ;
}
}
print "\n" . $seperator . "\n" . $total . "\n";
Giving:
Date Service_A Service_B Service_C Service_D
----- ------------ ------------ ------------ ------------
18 25 38 0 0
19 48 68 19 0
20 47 0 0 58
----- ------------ ------------ ------------ ------------
Total 120 106 19 58
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Events and news
Customer resources
© Copyright 2026 Hewlett Packard Enterprise Development LP