Re-formatting of data display


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Re-formatting of data display
# 1  
Old 11-06-2008
Re-formatting of data display

Hi All,

I have been trying to re-arrange the below data using AWK or Perl.
Can anybody help me ?
Thanks in advance.

Input:
Code:
111 222
333 444
AAA BBB
CCC DDD
555 666
777 888
EEE FFF
GGG HHH

Output:
Code:
111 222  AAA BBB
333 444  CCC DDD
555 666  EEE FFF
777 888  GGG HHH

# 2  
Old 11-07-2008
Try this:

Code:
awk '{a[NR%4]=$0}
NR%4==2 {getline;print a[1],$0;getline;print a[2],$0
}' file

Regards
# 3  
Old 11-08-2008
Hi Franklin52,

Wow, that's really cool.
It works !!
Thanks alot!

But i am a beginner in AWK, can you explain your code so that i can understand better ?
# 4  
Old 11-08-2008
OK, here we go:

Code:
awk '{a[NR%4]=$0}
NR%4==2 {getline;print a[1],$0;getline;print a[2],$0
}' file

We use as subscript of the array a, the remainder of NR/4 (division of NR by 4) with the modulo (%) operator.
The first line has the subscript 1 % 4 = 1
The second line has the subscript 2 % 4 = 2

Line 1:

If the 1st line is read, a[NR%4] = a[1] contains the 1st line ($0).
NR%4 == 1 so the code of the 2nd are skipped.

Line2:

a[NR%4] = a[2] contains the 2nd line.
NR%4 = 2 so the commands of the 2nd line of the code are processed.
The 1st statement is getline to read the next line. We use getline to continue with the next statement after reading the next line.
Now $0 contains the 3th line.
The next command prints a[1] = the first 1 and $0 (the 3th line).
After that, we get the nextline. Now $0 contains the 4th line.
The last the command prints a[2] = the 2nd line and $0 (the 4th line).

The next line is the 5th line, NR%4 = 5%4 = 1 and the we start the sequence again of line 1 and so forth.


Regards

Last edited by Franklin52; 11-08-2008 at 01:55 PM..
# 5  
Old 11-12-2008
Hi Franklin52,

Thanks for the comprehensive explanation.
What if for now my reference is on the term Device and Chip, how can the code be modified to suit this change ?


Input
Code:
Device 222
Device 444
Chip BBB
Chip DDD
Device 666
Device 888
Chip FFF
Chip HHH
Device 999
Chip GGG
Device 101
Device 102
Chip JJJ
Chip III

Output
Code:
Device 222  Chip BBB
Device 444  Chip DDD
Device 666  Chip FFF
Device 888  Chip HHH
Device 999  Chip GGG
Device 101  Chip JJJ
Device 102  CHip III


Last edited by Raynon; 11-12-2008 at 10:07 PM..
# 6  
Old 11-12-2008
Quote:
Originally Posted by Raynon
Hi Franklin52,

Thanks for the comprehensive explanation.
What if for now my reference is on the term Device and Chip, how can the code be modified to suit this change ?


Input
Code:
Device 222
Device 444
Chip BBB
Chip DDD
Device 666
Device 888
Chip FFF
Chip HHH
Device 999
Chip GGG
Device 101
Device 102
Chip JJJ
Chip III

Output
Code:
Device 222  Chip BBB
Device 444  Chip DDD
Device 666  Chip FFF
Device 888  Chip HHH
Device 999  Chip GGG
Device 101  Chip JJJ
Device 102  CHip III


Try this :

Code:
grep '^D' file >new1
grep '^C' file >new2
paste new1 new2

# 7  
Old 11-12-2008
If both Device and Chip have the same number of rows this should work:
Code:
awk '/Device/{a[_a++]=$0};/Chip/{b[_b++]=$0}END{for(i in a) print a[i],b[i]}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Data formatting using awk

Need assistance on the data extraction using awk Below is the format and would like to extract the data in another format ------------------------------------------------------------------------------------------------- Minimum Temperature (deg F ) DAY 1 2 3 4 5 6 7 8 9 10 11... (4 Replies)
Discussion started by: ajayram_arya
4 Replies

2. Shell Programming and Scripting

Help with data formatting

Hi, I am generating the following output from my script. Country,A,B,C,D,E,F INDIA ,3755019,774604,484749,329838,7333612,442031 CHINA ,3716520,889197,530899,379754,6198475,355768 JAPAN ,52038,30462,231224,147275,1272,498 USA,9494,1130,0,0,15303,451... (5 Replies)
Discussion started by: karumudi7
5 Replies

3. UNIX for Advanced & Expert Users

formatting the data

HI I want to make it single row if start with braces i.e. { .Any idea {1:XXX2460275191}{2:SEC00687921131112201641N}{3:{58910}}{4: :R:GENL :C::xx//xx1 :20C::yy//yy1 :2S:xxT} {1:XXX2460275190}{2:SEC00687921131112201641y}{3:{58911}}{4: :z:GENL :v::xx//xx1 :10C::yy//yy1 :4S:xxT ... (2 Replies)
Discussion started by: mohan705
2 Replies

4. Shell Programming and Scripting

help with data formatting

Hi, I have data coming in like below. Not all data is like that, these are the problem records that is causing the ETL load to fail. Can you pls help me with combining theese broken records! 001800018000000guyMMAAY~acct name~acct type~~"address part 1 address... (8 Replies)
Discussion started by: varman
8 Replies

5. Shell Programming and Scripting

Formatting input data

Hello everybody, I have a file containing some statistics regarding CPU usage. The file has this syntax : Fri Jul 16 14:27:16 EEST 2010 Cpu(s): 15.2%us, 1.4%sy, 0.0%ni, 82.3%id, 0.1%wa, 0.0%hi, 0.9%si, 0.0%st Fri Jul 16 15:02:17 EEST 2010 Cpu(s): 15.3%us, 1.4%sy, 0.0%ni, 82.3%id, ... (9 Replies)
Discussion started by: spiriad
9 Replies

6. Shell Programming and Scripting

Formatting Data - CSV

I want to check whether if any column data has any + , - , = prefixed to it then convert it in such a form that in excel its not read as formula. echo "$DATA" | awk 'BEGIN { OFS="," } -F" " {print $1,$2,$3,$4,$5,$6,$7,$8.$9,$10,$11,$12}' (4 Replies)
Discussion started by: dinjo_jo
4 Replies

7. UNIX for Dummies Questions & Answers

Data manipulation/ formatting question

How would I get this output to look $ cat newfile 13114 84652 84148 LIKE THIS?: 13114,84652,84148 sed,cut awk? syntax? (2 Replies)
Discussion started by: ddurden7
2 Replies

8. Shell Programming and Scripting

Script for data formatting

Hi I have to convert the data in a file ******* 01-20-09 11:14AM 60928 ABC Valuation-2009.xls 01-20-09 11:16AM 55808 DEF GHI Equation-2009.xls 01-20-09 11:02AM 52736 ABC DF Valuation-2009.xls 01-20-09 11:06AM 89600 THE... (6 Replies)
Discussion started by: shekhar_v4
6 Replies

9. UNIX for Dummies Questions & Answers

Formatting Data

i am writing a script that reads in a file and i just want it to print each element on a new line here is my code and the data that i want to read in #!/usr/bin/perl use strict; use CGI qw(:standard); use CGI qw(:cgi); my $data_file = "/tmp/results.txt"; my $configuration; my... (3 Replies)
Discussion started by: nmeliasp
3 Replies
Login or Register to Ask a Question