building table from list


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting building table from list
# 1  
Old 12-10-2009
building table from list

Hi,
I have a file with the following structure

Code:
M17XX-050-01-001 1100000000
A16	1.341E+05
...
B18	3.084E+02
total	1.344E+05
XY35	5.694E+03
...
XY241	6.725E+02
total	9.897E+05
Wr81Z	5.195E+00
...
Wr91Z	1.029E+02
Wr92Z	1.285E+02
total	9.897E+05
M17XX-050-01-001 1010000000
A16	1.341E+05
...
B18	3.084E+02
total	1.344E+05
XY35	5.694E+03
...
XY241	6.725E+02
total	9.897E+05
Wr81Z	5.195E+00
...
Wr91Z	1.029E+02
Wr92Z	1.285E+02
total	9.897E+05
etc..

So a set of data which start with a string like
M17XX-050-01-001 1100000000
after that there are 3 subset of data. The end of each subset of data is givel by the line starting with the string "total"

I would like to put the data in a table like format, without repeating the first of each string. Somwthing like

Code:
A16	1.34E+05	1.34E+05 ...
B18	3.08E+02	3.08E+02 ...
total	1.34E+05	1.34E+05
XY35	5.69E+03	5.19E+03
XY38	9.37E+05	9.33E+05
XY238	9.41E+02	9.25E+02
XY239	2.40E+04	2.19E+04
XY240	1.20E+04	1.21E+04
XY241	5.64E+03	5.97E+03
XY242	3.19E+03	3.33E+03
XY241	6.73E+02	6.36E+02
total	9.90E+05	5.59E+02
Wr81Z	5.20E+00	9.85E+05
We82Z	7.15E+00	7.69E+00
Wr83Z	1.06E+01	1.07E+01
Wr84Z	2.04E+01	1.56E+01
Wb85Z	1.71E+01	3.08E+01
Wr86Z	3.24E+01	2.55E+01
Wb87Z	4.21E+01	4.83E+01
Wr88Z	5.86E+01	6.27E+01
W89	7.18E+01	8.73E+01

The main problem is that the structure of each subset is not necessarly always the same. E.g. one line might be missing.
So I would like to define a subset of strings
e.g.

Code:
We82Z, Wr83Z, Wr84Z, Wb85Z

and then build a table according to the value they have in the file.
If the do not appear I would put 0.

Any idea at least how to tackle the problem?
Thanks,
# 2  
Old 12-10-2009
Quote:
Originally Posted by f_o_555
...
The main problem is that the structure of each subset is not necessarly always the same. E.g. one line might be missing.
So I would like to define a subset of strings
...
and then build a table according to the value they have in the file.
If the do not appear I would put 0.

Any idea at least how to tackle the problem?
Here's one way to do it with Perl:

Code:
$ 
$ 
$ cat -n f5
     1  M17XX-050-01-001 1100000000
     2  Wr81Z  5.195E+00           
     3  We82Z  2.309E+00           
     4  We82Z  4.567E+00           
     5  total  1.344E+05           
     6  We82Z  8.260E+00           
     7  Wr91Z  1.029E+02           
     8  total  9.897E+05           
     9  Wr81Z  5.195E+00           
    10  We82Z  2.309E+00           
    11  We82Z  8.260E+00           
    12  We82Z  1.117E+00           
    13  Wr91Z  1.029E+02           
    14  total  9.976E+05           
    15  M17XX-050-01-001 1010000000
    16  Wr81Z  5.195E+00           
    17  Wr91Z  1.029E+02           
    18  total  4.344E+05           
    19  Wr91Z  1.029E+02           
    20  total  9.897E+05           
    21  Wr81Z  5.195E+00           
    22  Wr91Z  1.029E+02           
    23  total  9.897E+05           
$
$
$ ##
$ perl -lne 'BEGIN {@items = qw (Wr81Z We82Z Wr91Z)}
>            @x = split;
>            if (/^total/) {
>              foreach $i (@items) {print $i,"\t",defined $tokens{$i} ? $tokens{$i} : 0}
>              print $x[0],"\t",$x[1];
>              %tokens = ();
>            } elsif (!/^M17/) {
>              if (defined $tokens{$x[0]}) {$tokens{$x[0]} .= "\t".$x[1]}
>              else {$tokens{$x[0]} = $x[1]}
>            }' f5
Wr81Z   5.195E+00
We82Z   2.309E+00       4.567E+00
Wr91Z   0
total   1.344E+05
Wr81Z   0
We82Z   8.260E+00
Wr91Z   1.029E+02
total   9.897E+05
Wr81Z   5.195E+00
We82Z   2.309E+00       8.260E+00       1.117E+00
Wr91Z   1.029E+02
total   9.976E+05
Wr81Z   5.195E+00
We82Z   0
Wr91Z   1.029E+02
total   4.344E+05
Wr81Z   0
We82Z   0
Wr91Z   1.029E+02
total   9.897E+05
Wr81Z   5.195E+00
We82Z   0
Wr91Z   1.029E+02
total   9.897E+05
$
$

tyler_durden
# 3  
Old 12-15-2009
thank you
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

Getting Rid of Annoying Bootstrap Table Borders and Wayward Table Lines

Bootstrap is great; but we have had some issues with Bootstrapped <tables> (and legacy <fieldset> elements) showing annoying, wayward lines. I solved that problem today with this simple jQuery in the footer: <script> $(function(){ $('tr, td, fieldset,... (0 Replies)
Discussion started by: Neo
0 Replies

2. UNIX for Beginners Questions & Answers

Building hierarchy with the list

Hi All, Sorry for more question today. I am having a text file . Like below 704925680_TOTAL->MANUAL->TT IOR GSB 775116444_TOTAL->POO TO->TT -572275295_TOTAL->MANUAL->MTO -611408278_TOTAL->PRIE LEL 456690129_TOTAL->BTT TOO 475919266_TOTAL->MANUAL->COM -172680236_TOTAL->BTT TOO->MTO... (15 Replies)
Discussion started by: arunkumar_mca
15 Replies

3. Shell Programming and Scripting

Build a table from a list by comparing existing table entries

I am new to this shell scripting.... I have a file which contains list of users. This files get updated when new user comes into the system. I want to create script which will give a table containing unique list of users. When I say unique, it means script should match table while parsing... (3 Replies)
Discussion started by: dchavan1901
3 Replies

4. UNIX for Dummies Questions & Answers

Creating a condensed table from a pre-existing table in putty

Hello, I'm working with putty on Windows 7 professional and I'd like to know if there's a way to gather specific lines from a pre-existing table and make a new table with that information. More specifically, I'd like the program to look at a specific column, say column N, and see if any of the... (5 Replies)
Discussion started by: Deedee393
5 Replies

5. Shell Programming and Scripting

Reformatting a list to table

Hi! I have a list with a lot of records that I need to work with. The problem is that the list is populated successive one record at the time in a text file, and to gain anything from these records I need them to be put out in a table. This is an example of what the list looks like: (145)... (5 Replies)
Discussion started by: ivar.friheim
5 Replies

6. UNIX for Dummies Questions & Answers

Reading Table name from a list of files in a Directory

Hi , I have searched through the forum but not able to find out any help :( i have a directory having lot of files which contains sql statemtns eg : file 1 contains select from table_name1 where ..................... select from table_name2 where .......... select from ... (3 Replies)
Discussion started by: Trendz
3 Replies

7. UNIX and Linux Applications

create table via stored procedure (passing the table name to it)

hi there, I am trying to create a stored procedure that i can pass the table name to and it will create a table with that name. but for some reason it creates with what i have defined as the variable name . In the case of the example below it creates a table called 'tname' for example ... (6 Replies)
Discussion started by: rethink
6 Replies

8. Shell Programming and Scripting

select values from db1 table and insert into table of DB2

Hi I am having three oracle databases running in three different machine. their ip address is different. from one of the DB am able to access both the databases.(means am able to select values and insert values in to tables individually.) I need to fetch some data from DB1 table(say DB1 ip is... (2 Replies)
Discussion started by: aemunathan
2 Replies

9. UNIX for Advanced & Expert Users

Rsync building file list/catalog path/location

Where is the file list created by rsync when it says building file list ? (1 Reply)
Discussion started by: glev2005
1 Replies

10. Shell Programming and Scripting

Crosstab to List table using awk Function

What I am trying to achieve is turning crosstab into a normal table e.g. convert following table Jan Feb Mar Apr May Jun Australia 1 2 3 4 5 6 USA 7 8 9 10 11 12 China 13 14 15 16 17 18 to Australia Jan 1 Australia Feb 2 Australia Mar 3 ... (2 Replies)
Discussion started by: asdban
2 Replies
Login or Register to Ask a Question