awk - script help: column to row format of data allignment?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk - script help: column to row format of data allignment?
# 1  
Old 01-11-2013
awk - script help: column to row format of data allignment?

Experts Good day,

I have the following data, file1

Code:
BRAAGRP1
A2X
B2X
C2X
D2X

BRBGRP12
A3X
B3X
Z10
D09

BRC1GRP2
LO01
LO02
LO03
LO09

BR11GR112
NN01
NN02
M001
M002



I am trying to get it into this format & horizontally :
Code:
BRAAGRP1 	BRBGRP12	BRC1GRP2	BR11GR12
A2X       	A3X          	LO01        	NN01
B2X         	B3X          	LO02        	NN02
C2X         	Z10          	LO03        	M001
D2X        	D09          	LO09        	M002


Can any one advise with good awk or other unix script or code,
Thanks a lot.
# 2  
Old 01-11-2013
Try:
Code:
awk '/^$/{i=0}!/^$/{i++;x=sprintf ("%-17s", $0);a[i]=a[i]""x}END{for (j=1;j<=i;j++) print a[j]}' file

This User Gave Thanks to bartus11 For This Post:
# 3  
Old 01-12-2013
burstus1 , Thanks! This is a great code and very interesting , I am not able to understand fully ,
I got sprint %-17s , to get the space, then what is the array function and till end couldnt understand. Appreciate if you can explain little bit. Thanks again for the help.

Reveri.
# 4  
Old 01-12-2013
Quote:
Originally Posted by rveri
burstus1 , Thanks! This is a great code and very interesting , I am not able to understand fully ,
I got sprint %-17s , to get the space, then what is the array function and till end couldnt understand. Appreciate if you can explain little bit. Thanks again for the help.

Reveri.
An equivalent awk script with comments is:
Code:
awk '
/^$/{   i=0     # Set output row counter to 0 when you find an empty line.
}
!/^$/{  i++     # Increment output row counter when you find a line with data.
        x=sprintf ("%-17s", $0) # Set x to current line expanded to length 17
                                # by adding trailing spaces.
        a[i]=a[i] x     # Add expanded current line to the end of row i data.
}
END{    for(j=1;j<=i;j++) print a[j]    # Print data for each accumulated row.
}' file1        # Read input data from a file named file1.

These 2 Users Gave Thanks to Don Cragun For This Post:
# 5  
Old 01-12-2013
Print the data in some different format.
Code:
awk '$1=$1' FS='\n' OFS='\t' RS=

Code:
BRAAGRP1        A2X     B2X     C2X     D2X
BRBGRP12        A3X     B3X     Z10     D09
BRC1GRP2        LO01    LO02    LO03    LO09
BR11GR112       NN01    NN02    M001    M002

This User Gave Thanks to Jotne For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Script to do column to row in awk

Hi , Can anyone help me suggesting - how to do the below trick with awk Input 120 130 140 210 310 410 645 729 800 Output 120 130 140 (6 Replies)
Discussion started by: Indra2011
6 Replies

2. Shell Programming and Scripting

awk script row to column

Hi.. I have data : Report testing1 20180419 08:00 Report testing2 20180419 07:35 Report testing 20180419 08:01 Source = data1 Report testing4 20180419 08:05 Source = data1 Report testing5 20180419 08:10 Source = data2 Report testing6 20180419 08:01 Report testing7 20180419 08:19... (4 Replies)
Discussion started by: buncit8
4 Replies

3. Shell Programming and Scripting

How to get row data printed in column using awk?

Hi team, I have below sample file. $ cat sample dn: MSISDN=400512345677,dc=msisdn,ou=NPSD,serv=CSPS,ou=servCommonData,dc=stc structuralObjectClass: NphData objectClass: NphData objectClass: MSISDN entryDS: 0 nodeId: 35 createTimestamp: 20170216121047Z modifyTimestamp: 20170216121047Z... (3 Replies)
Discussion started by: shanul karim
3 Replies

4. UNIX for Dummies Questions & Answers

Des/awk for change format and adding integers in a column of data?

Greetings! I need a quick way to change the format in a table of data Here is an example of the input: 10 72 Value=177 VDB=0.0245 Value4=0,0,171,0 10 274 Value=238 VDB=0.0433 Value4=29,0,205,0 10 312 Value=222 VDB=0.0384 Value4=8,0,190,19 10 540 Value=405 VDB=0.0391 Value4=13,30,153,195... (3 Replies)
Discussion started by: Twinklefingers
3 Replies

5. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

6. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

7. Shell Programming and Scripting

Awk to add selected row column data

Looks at the most efficient way to add up the column of data based off of the rows. Random data Name-Number-ID Sarah-2.0-15 Bob-6.3-15 Sally-1.0-10 James-1.0-10 Scotty-10.7-15 So I would select all those who have ID = 15 and then add up total number read - p "Enter ID number" Num ... (3 Replies)
Discussion started by: Ironguru
3 Replies

8. Shell Programming and Scripting

Sort a the file & refine data column & row format

cat file1.txt field1 "user1": field2:"data-cde" field3:"data-pqr" field4:"data-mno" field1 "user1": field2:"data-dcb" field3:"data-mxz" field4:"data-zul" field1 "user2": field2:"data-cqz" field3:"data-xoq" field4:"data-pos" Now i need to have the date like below. i have just... (7 Replies)
Discussion started by: ckaramsetty
7 Replies

9. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

10. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies
Login or Register to Ask a Question