awk search then transpose rows to columns

Graham

quite new to awk and data manipulation but have got stuck and was looking for some help.

have a file: Ntab.txt which is a sample file of two hosts, there is numerous hosts in the real file one after each other.

Each host has multiple "displayName"s (addresses) with corresponding figures for each 'displayName'.

>cat Ntab.txt
name    devtwr1
displayName     00:67:BB
capacityInKB    104,857,600
consumedCapacityInKB    4,042,752
dpPoolID        20
displayName     00:7B:FD
capacityInKB    52,428,800
consumedCapacityInKB    14,880,768
dpPoolID        10
displayName     00:7C:28
capacityInKB    34,179,712
consumedCapacityInKB    29,804,544
dpPoolID        20
displayName     00:7C:29
capacityInKB    34,179,712
consumedCapacityInKB    5,462,016
dpPoolID        20
name    devtwr2
displayName     00:67:BB
capacityInKB    104,857,600
consumedCapacityInKB    4,042,752
dpPoolID        20
displayName     00:7B:FD
capacityInKB    52,428,800
consumedCapacityInKB    14,880,768
dpPoolID        10
displayName     00:7C:28
capacityInKB    34,179,712
consumedCapacityInKB    29,804,544
dpPoolID        20
displayName     00:7C:29
capacityInKB    34,179,712
consumedCapacityInKB    5,462,016
dpPoolID        20

I need to be able to produce the $2 data from the rows into columns after each 'name' (host) and in csv type format, headings optional. I cant use (,:) as separators as the data includes them so either (tab or ;).

Like:

name;displayName;capacityInKB;consumedCapacityInKB;dpPoolID        
devtwr1;00:67:BB;104,857,600;4,042,752;20
 ;00:7B:FD;52,428,800;14,880,768;10
 ;00:7C:28;34,179,712;29,804,544;20
 ;00:7C:29;34,179,712;5,462,016;20
devtwr2;00:67:BB;104,857,600;4,042,752;20
 ;00:7B:FD;52,428,800;14,880,768;10
 ;00:7C:28;34,179,712;29,804,544;20
 ;00:7C:29;34,179,712;5,462,016;20

Hope someone can help.

Mark Reed

You asked for awk, and should accept John1024's solution if it works for you. But this is the sort of problem where I would reach for Perl instead. This is one solution in that language. It has a slight advantage (IMO) in that it is not dependent upon the specific order of the fields in each record, apart from displayName signalling the start of a new set.

$ perl -lane '
BEGIN {
  @fields = qw(name displayName capacityInKB consumedCapacityInKB dpPoolID);
  print join ";", @fields;
}
if (/^(name|displayName)/ && $data{displayName}) {
  print join ";", @data{@fields};
  %data = ( name => $data{name} );
}
$data{$F[0]} = $F[1];
END {
  print join ";",@data{@fields};
}' ntab.txt

Output:

name;displayName;capacityInKB;consumedCapacityInKB;dpPoolID
devtwr1;00:67:BB;104,857,600;4,042,752;20
devtwr1;00:7B:FD;52,428,800;14,880,768;10
devtwr1;00:7C:28;34,179,712;29,804,544;20
devtwr1;00:7C:29;34,179,712;5,462,016;20
devtwr2;00:67:BB;104,857,600;4,042,752;20
devtwr2;00:7B:FD;52,428,800;14,880,768;10
devtwr2;00:7C:28;34,179,712;29,804,544;20
devtwr2;00:7C:29;34,179,712;5,462,016;20

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Transpose Rows to Columns with Pandas

分類Dev

Transpose specific rows into columns in pandas

分類Dev

SQL: aggregate & transpose rows to columns

分類Dev

SQL Server : expand rows and transpose columns

分類Dev

Oracle SQL Developer: How to transpose rows to columns using PIVOT function

分類Dev

Merge rows by duplicate columns in awk/unix with condition

分類Dev

Memory efficient transpose - Awk

分類Dev

Transpose of certain related columns in R

分類Dev

Pivot(transpose) by some columns SQL

分類Dev

pivot\transpose some of the columns in python and saved it to dataframe

分類Dev

transpose columns to row using cross apply

分類Dev

Transpose DF columns based on column values - Pandas

分類Dev

VBA Transpose Table after n columns

分類Dev

Copy lines by rows in awk

分類Dev

Remove empty columns by awk

分類Dev

Search multiple rows by space

分類Dev

setNames for rows and columns?

分類Dev

Split an array in rows and columns

分類Dev

Group by on selected rows in columns

分類Dev

hive tranpose rows to columns

分類Dev

Oracle pivot rows to columns

分類Dev

Convert rows to columns in mysql?

分類Dev

Apply Search to Multiple Columns

分類Dev

transpose postgresql query result from rows to group by column value

分類Dev

Merge columns with the same value with awk

分類Dev

Subtotal using awk for multiple columns

分類Dev

How to Transpose Column B to y-axis columns in DataFrame

分類Dev

macro to transpose and add a prefix after ordering columns' names

分類Dev

force global search Datatables to search on columns that not listed in columns option