CSV Table Filtered/transposed/matched using CSH


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting CSV Table Filtered/transposed/matched using CSH
# 1  
Old 10-24-2008
MySQL CSV Table Filtered/transposed/matched using CSH

Hello Everybody:

I have a csv file that i would want to be converted to a table (csv also) filtered, transposed and matched with the header (quite confusing, sorry). So the output can used on a spreadsheet and plot on a grap. I'm using CSH on unix.
To further explain, here is an example input and expected output:

+++++++
Input file:
++++++++

Oct|24|12:08:18|2008,Bin,1,3,4,6,7,8,15,
Oct|24|12:08:18|2008,Total,461,1,4,3,31,1,213,
Oct|24|12:08:18|2008,Percentage,64.5,0.1,0.6,0.4,4.3,0.1,29.8,

Oct|24|12:23:24|2008,Bin,1,3,4,6,7,8,15,16
Oct|24|12:23:24|2008,Total,610,1,10,4,41,1,237,1
Oct|24|12:23:24|2008,Percentage,67.4,0.1,1.1,0.4,4.5,0.1,26.2,0.1

Oct|24|12:38:18|2008,Bin,1,3,4,6,7,8,13,15,16
Oct|24|12:38:18|2008,Total,655,1,12,5,48,1,1,260,1
Oct|24|12:38:18|2008,Percentage,66.6,0.1,1.2,0.5,4.9,0.1,0.1,26.4,0.1

#This input file is a log file which logs every 15 minutes of 3 rows of data, each row for Bin, Total and Percentage, with the date on 1st column. In the example above, there is only 3 log incidents but in actual it can be more than close to 300 lines in 1 day (3rows every 15mins x 4 x 24).

#On the Bin row, some Bin numbers are not present on all log incidents as can be seen on the 1st log - Bin13 is not present but present at 2nd and 3rd incidents, so is Bin16 which not present at 1st but present at 2nd and 3rd incident.

++++++
Output:
++++++
CSV format below. To better visualize, see attached pix in spreadsheet.
# Header is Bin and numbers 1 to 16. (1st columns is a blank " ")
# Values in the Input file in Percentage row of every log incident is transposed if the Bin number of this log incident is matching the Bin header number.
#Some Bin numbers do not appear in all log instances, take note on Bin13 and Bin16 values in the output.

,Bin,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
Oct|24|12:08:18|2008,Percentage,64.5,,0.1,0.6,,0.4,4.3,0.1,,,,,,,29.8,,
Oct|24|12:23:24|2008,Percentage,67.4,,0.1,1.1,,0.4,4.5,0.1,,,,,,,26.2,0.1,
Oct|24|12:38:18|2008,Percentage,66.6,,0.1,1.2,,0.5,4.9,0.1,,,,,0.1,,26.4,0.1,

Thank you in advance for the help and time!
Hope to have a solution around there.
CSV Table Filtered/transposed/matched using CSH-outputgif
CSV Table Filtered/transposed/matched using CSH-outputjpg
# 2  
Old 10-24-2008
Something like this:
(use nawk or /usr/xpg4/bin/awk on Solaris)

Code:
awk 'BEGIN { 
  FS = ","; ORS = OFS = ""; max = 16 
  print ",bin,"; while (++c <= max) 
    print c, (c == max ? FS RS : FS) 
  }
/Bin/ { for (i=3; i<=NF; i++) num_bin[$i] }
/Percentage/ { printf "%s,%s,", $1, $2; c = 3
  for (i=1; i<=max; i++) 
    print i in num_bin ? $(c++) : "", (i == max ? FS RS : FS)
  split("", num_bin)
}' infile

The above code produces:

Code:
% cat file
Oct|24|12:08:18|2008,Bin,1,3,4,6,7,8,15,
Oct|24|12:08:18|2008,Total,461,1,4,3,31,1,213,
Oct|24|12:08:18|2008,Percentage,64.5,0.1,0.6,0.4,4.3,0.1,29.8,

Oct|24|12:23:24|2008,Bin,1,3,4,6,7,8,15,16
Oct|24|12:23:24|2008,Total,610,1,10,4,41,1,237,1
Oct|24|12:23:24|2008,Percentage,67.4,0.1,1.1,0.4,4.5,0.1,26.2,0.1

Oct|24|12:38:18|2008,Bin,1,3,4,6,7,8,13,15,16
Oct|24|12:38:18|2008,Total,655,1,12,5,48,1,1,260,1
Oct|24|12:38:18|2008,Percentage,66.6,0.1,1.2,0.5,4.9,0.1,0.1,26.4,0.1

% awk 'BEGIN {    
  FS = ","; ORS = OFS = ""; max = 16
  print ",bin,"; while (++c <= max)
    print c, (c == max ? FS RS : FS)
  }
/Bin/ { for (i=3; i<=NF; i++) num_bin[$i] }
/Percentage/ { printf "%s,%s,", $1, $2; c = 3
  for (i=1; i<=max; i++)
    print i in num_bin ? $(c++) : "", (i == max ? FS RS : FS)
  split("", num_bin)
}' file
,bin,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,
Oct|24|12:08:18|2008,Percentage,64.5,,0.1,0.6,,0.4,4.3,0.1,,,,,,,29.8,,
Oct|24|12:23:24|2008,Percentage,67.4,,0.1,1.1,,0.4,4.5,0.1,,,,,,,26.2,0.1,
Oct|24|12:38:18|2008,Percentage,66.6,,0.1,1.2,,0.5,4.9,0.1,,,,,0.1,,26.4,0.1,

# 3  
Old 10-28-2008
thanks radoulov! your advice helped me lot, just needed to tweak somethings on the codes. thanks again!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Plain text table to csv problems

I´m trying to input a plain text table and I want to output a csv file with semicolon field separator. I have some problems with the \r and the fields with more of one line.. Some gnu util to do this without problems or awk solution? I´m attaching the original plain table file and the needed... (2 Replies)
Discussion started by: faka
2 Replies

2. Shell Programming and Scripting

Making a composite file of transposed columns

Hello, I have a directory with allot of tab delimited text files that have data that look like, filePath distance (1,4-dioxan-2-ylmethyl)methylamine 0.0 4-methylmorpholine 0.0755473632594 1-propyl-4-piperidone 0.157792911954 heptaminol 0.158142893249 N-acetylputrescine 0.158689628956... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

3. Shell Programming and Scripting

Create table based on matched patterns

hi, i need help to create a table from an input file like this:- DB|QZX3 140 165 RT_2 VgGIGvGVR DB|QZX3 155 182 UT_1 rlgslqqLaIvlGiFT DB|QZX3 345 362 RT_1 GRKpllligS DB|ZXK6 174 199 RT_2 IstvtvptYlgEiatvkaR DB|ZXK6 189 216 UT_1 algtiyqLfLviGiLF DB|AZ264 15 17... (7 Replies)
Discussion started by: redse171
7 Replies

4. Shell Programming and Scripting

Update the table using values from a csv file

i want to run update query for oracle which is in up.sql taking values from a.csv. I have implemented shell script to do it. extn="perl" ls -1 | while read file do echo "$file,$extn" > a.csv done up.sql contains update file_list set filename=$1 where extn=$2; The code to update is... (2 Replies)
Discussion started by: millan
2 Replies

5. Shell Programming and Scripting

Find and replace variables using a csv table

I have a flat file (template) where I want to replace variables based upon a value in another file (csv). The variables in the template are named %VAR_X_z% The values are in the csv file and X is field 0 of each line and y field 1 and up. Example of the csv: Badidas, 13.00, 12.00, 11.00,... (8 Replies)
Discussion started by: biscayne
8 Replies

6. Shell Programming and Scripting

Convert file in csv or table

Hi there, i have a file like that in attachment (PLEVA3_280711_SAP.txt), i would extract some basic information from it and report in a new file or table like this: i try to use bash and i extract the single object in this way (see attach scriptino.sh), but i receive a strange... (5 Replies)
Discussion started by: alen192
5 Replies

7. UNIX for Dummies Questions & Answers

Storing data from a table into a csv file

Hi I need to write a bash script to take the data stored in 3 oracle tables .. and filter them and store the results in a csv file. It is an Oracle database Thank you (1 Reply)
Discussion started by: ladyAnne
1 Replies

8. Shell Programming and Scripting

Store table contents in csv file

I need to write a script to store the contents of a table in a csv file I'm using Toad, it's a Oracle database. (5 Replies)
Discussion started by: ladyAnne
5 Replies

9. Shell Programming and Scripting

csv to table one-liner

I've googled a lot on this, but could not fine a simple one-liner to do this. I have a .csv file that looks like this: Header one Header two Header three col1,col2,col3 short data, very long data, dataIf I use sed and change the comma to tab, being the colums of variable length I don't get a... (6 Replies)
Discussion started by: ahsog
6 Replies

10. Shell Programming and Scripting

HTML table to CSV

Hi !! I have HTML Tables through which i want to generate graphs, but for creating graphs i need the file in CSV format so can anyone can please help me in how can i convert my HTML table file to CSV format. Thanks in Advance (2 Replies)
Discussion started by: i_priyank
2 Replies
Login or Register to Ask a Question