Sponsored Content
Full Discussion: Convert row to column
Top Forums Shell Programming and Scripting Convert row to column Post 302566436 by Skrynesaver on Thursday 20th of October 2011 10:59:02 AM
Old 10-20-2011
The following script (called like ~/$ perl test.pl tmp.dat) returns the result you require.
Code:
#!/usr/bin/perl

use strict;
use warnings;

my %table;
while(<>){
   my ($index_1,$index_2,$waste,$value)=split(/\s+/,$_);
   push @{$table{"$index_1/$index_2"}},$value;
}
my $rows=0;
$rows = $rows> $#{$table{$_}} ? $rows : $#{$table{$_}} for (keys %table);
print join( "",map {sprintf "%-11s", $_;} sort numeric_sort_col_header keys %table),"\n";
for my $index (0..$rows){
   for my $key (sort numeric_sort_col_header keys %table){
      printf "%-11s",($table{$key}->[$index]||"");
   }
   print "\n";
}
sub numeric_sort_col_header{
   my @A=split(/\//,$a);
   my @B=split(/\//,$b);
   $A[0]<=>$B[0]||$A[1]<=>$B[1]
}


Last edited by Skrynesaver; 10-20-2011 at 12:59 PM.. Reason: Make column headings line up
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

convert column into row with some modifier

A file content have 1 1:-0.289433 2:0.833778 3:0.314471 4:-0.289433 5:-0.81876 6:-0.456693 7:-0.17511 8:-0.644555 9:-0.00666341 10:-1.13603 I will like to have that column into row with numbers to be printed (red color) only after colon output shud be like that -0.289433... (1 Reply)
Discussion started by: cdfd123
1 Replies

2. Shell Programming and Scripting

column to row convert - script - help

Hi, I have a file named col.txt 1.000 2.000 3.000 4.000 5.000 6.000 7.000 8.000 I should get this 1.000 5.000 2.000 6.000 3.000 7.000 (10 Replies)
Discussion started by: G0Y
10 Replies

3. Shell Programming and Scripting

Convert column values into row

hi, I have a requirement where in I read the values from a file using awk. The resulting data should be converted into row format from column format. For ex: My log file login.lst contains the following SERVER1 DB1 SERVER2 DB2 SERVER3 DB3 SERVER4 DB4 I use awk to grep only the server... (6 Replies)
Discussion started by: senthil3d
6 Replies

4. Shell Programming and Scripting

convert a column to row output?

Getting tired of cut-and-paste...so I thought I would post a question. how do I change this column output to a single row? from this: # vgdisplay -v /dev/vgeva05 | grep dsk | awk '{print $3}' /dev/dsk/c6t0d5 /dev/dsk/c11t0d5 /dev/dsk/c15t0d5 /dev/dsk/c18t0d5 /dev/dsk/c7t0d5... (8 Replies)
Discussion started by: mr_manny
8 Replies

5. UNIX for Dummies Questions & Answers

awk: convert column to row in a specific way

Hi all! I have this kind of output: a1|b1|c1|d1|e1 a2|b2|c2 a3|b3|c3|d3 I would like to transpose columns d and e (when they exist) in column c, and under the row where they come from. Then copying the beginning of the row. In order to obtain: a1|b1|c1 a1|b1|d1 a1|b1|e1 a2|b2|c2... (1 Reply)
Discussion started by: lucasvs
1 Replies

6. Shell Programming and Scripting

convert row to column with respect of first column.

Input file A.txt :- C2062 -117.6 -118.5 -117.5 C5145 0 0 0 C5696 0 0 0 Output file B.txt C2062 X -117.6 C2062 Y -118.5 C2062 Z -117.5... (4 Replies)
Discussion started by: asavaliya
4 Replies

7. Shell Programming and Scripting

Script: Convert row in to column

Hi, i need to convert SG_ERP1 SG_ERP2 SG_ERP3 in to: SG_ERP1 SG_ERP2 SG_ERP3 It's possibile? (16 Replies)
Discussion started by: elilmal
16 Replies

8. Shell Programming and Scripting

How to convert the row to column in Perl?

Dear Perl users, Could you help me how to convert from row to column if I've a case below: Linux 2014_01_24 CPU 10 Linux 2014_01_24 MEM 20 UNIX 2014_01_24 CPU 30 UNIX 2014_01_24 MEM ... (6 Replies)
Discussion started by: askari
6 Replies

9. Shell Programming and Scripting

[Solved] Convert Row To column

Hi Folks, I am using db2 command -> db2 list tablespace show detail Tablespace ID = 10 Name = TSCDDHLMSUM Type = Database managed space Contents = All permanent data.... (5 Replies)
Discussion started by: ckwan
5 Replies

10. Shell Programming and Scripting

Convert Data from Column to Row

Hi FileA.txt E_TIM 16, ETE 15, EOND 26, EEC 81, E_1 un, E_2 un, E_3 un, E_4 284, E_TIM 17, ETE 15, EOND 29, EEC 82, E_1 un, E_2 un, E_3 un, E_4 249, (6 Replies)
Discussion started by: asavaliya
6 Replies
CLUSTER(7)						  PostgreSQL 9.2.7 Documentation						CLUSTER(7)

NAME
CLUSTER - cluster a table according to an index SYNOPSIS
CLUSTER [VERBOSE] table_name [ USING index_name ] CLUSTER [VERBOSE] DESCRIPTION
CLUSTER instructs PostgreSQL to cluster the table specified by table_name based on the index specified by index_name. The index must already have been defined on table_name. When a table is clustered, it is physically reordered based on the index information. Clustering is a one-time operation: when the table is subsequently updated, the changes are not clustered. That is, no attempt is made to store new or updated rows according to their index order. (If one wishes, one can periodically recluster by issuing the command again. Also, setting the table's FILLFACTOR storage parameter to less than 100% can aid in preserving cluster ordering during updates, since updated rows are kept on the same page if enough space is available there.) When a table is clustered, PostgreSQL remembers which index it was clustered by. The form CLUSTER table_name reclusters the table using the same index as before. You can also use the CLUSTER or SET WITHOUT CLUSTER forms of ALTER TABLE (ALTER_TABLE(7)) to set the index to be used for future cluster operations, or to clear any previous setting. CLUSTER without any parameter reclusters all the previously-clustered tables in the current database that the calling user owns, or all such tables if called by a superuser. This form of CLUSTER cannot be executed inside a transaction block. When a table is being clustered, an ACCESS EXCLUSIVE lock is acquired on it. This prevents any other database operations (both reads and writes) from operating on the table until the CLUSTER is finished. PARAMETERS
table_name The name (possibly schema-qualified) of a table. index_name The name of an index. VERBOSE Prints a progress report as each table is clustered. NOTES
In cases where you are accessing single rows randomly within a table, the actual order of the data in the table is unimportant. However, if you tend to access some data more than others, and there is an index that groups them together, you will benefit from using CLUSTER. If you are requesting a range of indexed values from a table, or a single indexed value that has multiple rows that match, CLUSTER will help because once the index identifies the table page for the first row that matches, all other rows that match are probably already on the same table page, and so you save disk accesses and speed up the query. CLUSTER can re-sort the table using either an index scan on the specified index, or (if the index is a b-tree) a sequential scan followed by sorting. It will attempt to choose the method that will be faster, based on planner cost parameters and available statistical information. When an index scan is used, a temporary copy of the table is created that contains the table data in the index order. Temporary copies of each index on the table are created as well. Therefore, you need free space on disk at least equal to the sum of the table size and the index sizes. When a sequential scan and sort is used, a temporary sort file is also created, so that the peak temporary space requirement is as much as double the table size, plus the index sizes. This method is often faster than the index scan method, but if the disk space requirement is intolerable, you can disable this choice by temporarily setting enable_sort to off. It is advisable to set maintenance_work_mem to a reasonably large value (but not more than the amount of RAM you can dedicate to the CLUSTER operation) before clustering. Because the planner records statistics about the ordering of tables, it is advisable to run ANALYZE(7) on the newly clustered table. Otherwise, the planner might make poor choices of query plans. Because CLUSTER remembers which indexes are clustered, one can cluster the tables one wants clustered manually the first time, then set up a periodic maintenance script that executes CLUSTER without any parameters, so that the desired tables are periodically reclustered. EXAMPLES
Cluster the table employees on the basis of its index employees_ind: CLUSTER employees USING employees_ind; Cluster the employees table using the same index that was used before: CLUSTER employees; Cluster all tables in the database that have previously been clustered: CLUSTER; COMPATIBILITY
There is no CLUSTER statement in the SQL standard. The syntax CLUSTER index_name ON table_name is also supported for compatibility with pre-8.3 PostgreSQL versions. SEE ALSO
clusterdb(1) PostgreSQL 9.2.7 2014-02-17 CLUSTER(7)
All times are GMT -4. The time now is 03:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy