Multicolumn csv output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multicolumn csv output
# 1  
Old 08-06-2008
Multicolumn csv output

I'm revising a quick and dirty script I wrote for my work that takes a series of data files (tab delimited) and condenses them into one. Essentially, each original file looks like these examples:
Code:
A1  data
B1 data
C1 data

Code:
A2 data
B2 data
C2 data

The script currently squashes them into the final form:
Code:
A1,data
A2,data
A3,data
B1,data
B2,data
B3,data
...

What I'm having difficulty implementing is instead changing the data format to something like:
Code:
A1,data,B1,data,C1,data
A2,data,B2,data,C2,data
A3,data,B3,data,C3,data

I'm attaching my script below... I'd appreciate any feedback-- I'm very new to this!

Code:
#!/bin/bash
#ATR Kinetic Analysis Script for KDE
#author: ParadoxDruid
#created March 13, 2008
#modified August 6th, 2008

#say hello
kdialog --msgbox "Signalyze ATR Data Converter\nPress OK to select working directory"
#navigate to proper directory
DIRECTORY=`kdialog --getexistingdirectory .`
#get necessary info from user
NAME=`kdialog --title "Trial Name" --inputbox "Name of your trial"`
echo $NAME" Trial Set" > $DIRECTORY/$NAME.csv
echo -e "Seq,Point,Integral,sd" >> $DIRECTORY/$NAME.csv
#filename set
SET=`kdialog --title "Sets" --inputbox "What is the name of your results files? (i.e.  kinetics-1_  )"`
#end filename set

#probe sequence detection
cat $DIRECTORY/"$SET"1.stx | sed '1,22d' > $DIRECTORY/$NAME.temp
SEQNUM=`cat $DIRECTORY/$NAME.temp | wc -l`
#end probe sequence detection

#data points auto-detection
POINTS=`ls $DIRECTORY | grep $SET | grep stx | sed 's/'$SET'//' | sed 's/\.stx//' | sort -n | tail -n1`
#end data points auto detection

#probe names routine
for ((c=1;c<$SEQNUM+1;c++)); do
sequences[$c]=`cat $DIRECTORY/$NAME.temp | head -n$c | tail -n1 | cut -f 1`
done
#end routine

#increment the number of data points, since the ATR doesn't use 0
let "TRIALS=$POINTS+1"

#define a function to grab data
seqgrab() {
for ((i=1;i<$TRIALS;i++)); do
data=`cat $DIRECTORY/"$SET"$i.stx | grep -w $1 | cut -f 5`
sd=`cat $DIRECTORY/"$SET"$i.stx | grep -w $1 | cut -f 6`
 echo -e $1 "," $i "," $data "," $sd  >> $DIRECTORY/$NAME.csv
done
}

#tell about our progress
dcopRef=`kdialog --progressbar "Initialising" ${#sequences[@]}`

#iterate through the data files
for ((n=1;n<${#sequences[@]}+1;n++)); do
seqgrab ${sequences[${n}]}
dcop $dcopRef setProgress $n
dcop $dcopRef setLabel "Working..."
done

rm $DIRECTORY/$NAME.temp
dcop $dcopRef close
kdialog --textbox $DIRECTORY/$NAME.csv 440 800
exit 0

Thanks again for any help!
# 2  
Old 08-06-2008
You might want to use the "paste" command instead - seems like it could do all the work.

I hope this helps.

bakunin
# 3  
Old 08-06-2008
The paste command does look promising, and I feel silly for not knowing it.

However, I may have oversimplified my explanation above to try and make it legible.
Here's a snippet from two actual origin files:
Code:
Time 6
Probe_Name    Count    Net_Signal    Net_Signal_SD    Net_Integral    Net_Integral_SD    Proc_Control    
A1    5    0.04594    0.01175    0.81596    0.23182    OK    
B1    3    0.02464    0.00381    0.59647    0.15367    OK    
C1    5    0.13487    0.02862    2.54441    0.29700    OK

Code:
Time 7
Probe_Name    Count    Net_Signal    Net_Signal_SD    Net_Integral    Net_Integral_SD    Proc_Control    
A1    5    0.04545    0.01211    0.82307    0.24171    OK    
B1    3    0.02332    0.00557    0.56161    0.10771    OK    
C1    5    0.13672    0.02963    2.54276    0.26535    OK    
D1    5    0.14061    0.07675    2.58301    1.31850    OK

All I want to preserve is the first column (A1) and the last two numbers (0.81596 and 0.23182), in a time-dependent fashion, so that my output shows that data named A1 had value 0.81596 at time 6, and 0.82307 at time 7. A current actual final data snippet:
Code:
Seq,Point,Integral,sd
A1 ,1,0.81596,0.23182
A1 ,2,0.81793,0.2443
A1 ,3,0.82073,0.24254
A1 ,4,0.82307,0.24171
A1 ,5,0.81935,0.23554
B1 ,1,0.59647,0.15367
B1 ,2,0.57585,0
B1 ,3,0.55278,0.11597
B1 ,4,0.56161,0.10771
B1 ,5,0.49331,0.08419
C1 ,1,2.54441,0.297

Currently, I do this by iterating over the time/files with cut to grab the correct fields (greping the name) and save them, but I output it via echo, which makes it difficult to line up multiple columns.

I'll look at paste more, but I'd appreciate further ideas, too. There's probably an easy way to do with with awk or paste or something that i just haven't seen. Thanks!
# 4  
Old 08-06-2008
I agree with Bakunin. paste can do all that for you with a little help from tr Smilie

Code:
paste -s file1 file2 file3 ... | tr '\t' ','

# 5  
Old 08-06-2008
Quote:
Originally Posted by shamrock
I agree with Bakunin. paste can do all that for you with a little help from tr Smilie
Code:
paste -s file1 file2 file3 ... | tr '\t' ','

I'm not sure I understand how I can use paste to make multiple columns.
When I stripped out header info from a few test files and ran a command like the above, it gave me
Code:
A1(time 1) and all numerical fields
B1(time 1) and all numerical fields
...

A1(time 2) and all numerical fields
B1(time 2) and all numerical fields
...

What I'm looking for is a final output like:
Code:
A1(time 1) and fields 5 and 6,B1(time 1) and fields 5 and 6, C1(time 1) and fields 5 and 6,...
A1(time 2) and fields 5 and 6,B1(time 2) and fields 5 and 6, C1(time 2) and fields 5 and 6,... 
...

where the columns are the data for different names (A1,B1, etc-- separate lines in the original file) and the rows are iterating through each file for the different timepoints.

Thanks again!
# 6  
Old 08-07-2008
below one should be ok. But it supposes that there is no "|" in your file.

If not, just replace "|" with another special character which will never appear in your file.

Code:
paste -d"|" file1 file2 file3 | tr "|" "\n"

# 7  
Old 08-07-2008
Check if this is what u r looking for and check if its helpful

cat file1 file2 file3 | sort | awk '{ct=1;}$1==prv{ct++;}{printf("%s\n",$1 "," ct "," $5 "," $6);ct=1;prv=$1}'
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to translate df -h output into a CSV format?

Hi, Can someone please let me know as how I can send the below df -h format of a linux system into a CSV format ? Resource Size GiB Used GiB Avail GiB Use% Cleanable GiB* ---------------- -------- -------- --------- ---- -------------- /data: pre-comp -... (10 Replies)
Discussion started by: new2prog
10 Replies

2. Shell Programming and Scripting

Save output of updated csv file as csv file itself, part 2

Hi, I have another problem. I want to sort another csv file by the first field. result.csv SourceFile,Airspeed,GPSLatitude,GPSLongitude,Temperature,Pressure,Altitude,Roll,Pitch,Yaw /home/intannf/foto5/2015_0313_090651_219.JPG,0.,-7.77223,110.37310,30.75,996.46,148.75,180.94,182.00,63.92 ... (2 Replies)
Discussion started by: refrain
2 Replies

3. Shell Programming and Scripting

Save output of updated csv file as csv file itself

Hi, all I want to sort a csv file based on timestamp from oldest to newest and save the output as csv file itself. Here is an example of my csv file. test.csv SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0739.JPG,2015:02:17 11:32:21 /home/intannf/foto/IMG_0749.JPG,2015:02:17 11:37:28... (10 Replies)
Discussion started by: refrain
10 Replies

4. Shell Programming and Scripting

Output to csv contains quotes

I borrowed scripting information from thread and modified it for our environment to uuencode it and mail a csv to me. The issue I have is the output contains quotes that hinder the csv use appropriately. My script looks like this cd /tmp df -Pg | awk 'BEGIN{ print "\t\t\t SPACE OF... (8 Replies)
Discussion started by: strykergli250hp
8 Replies

5. Shell Programming and Scripting

Csv format output file using scirpt

Hi All, I get the test result file daily after running the main test script. from the resultfile, need to fetch only server info and status and put them in tabular format in a file and as well in CSV format output file. I tried using awk command but am not able to put them in tabluar... (6 Replies)
Discussion started by: Optimus81
6 Replies

6. Shell Programming and Scripting

awk math and csv output

Hi I have this list 592;1;Z:\WB\DOCS;/FS3_100G/FILER112/BU/MPS/DOCS;;;;\\FILER112\BUMPS-DOCS\;580,116,544,878 Bytes;656,561 ;77,560 592;2;Z:\WB\FOCUS;/FS3_100G/FILER112/BU/MPS/FOCUS;;;;\\FILER112\BUMPS-FOCUS\;172,430 Bytes;6 ;0 ... (12 Replies)
Discussion started by: nakaedu
12 Replies

7. Shell Programming and Scripting

compare 2 CSV fields from same diff output

Attached is a file called diff.txt It is the output from this command: diff -y --suppress-common-lines --width=5000 1.txt 2.txt > diff.txt I have also attached 1.txt and 2.txt for your convenience. Both 1.txt and 2.txt contain one very long CSV string. File 1.txt is a CSV dump of... (0 Replies)
Discussion started by: gvolpini
0 Replies

8. Shell Programming and Scripting

handling CSV file to get desired output

Hi All , i have a CSV file , pattern is given below :- Group # name # host # account # stop # # start # # check -------------------------------------------------------------------------- file format and data exmaple :- RBP2,RB0112,sihrb001,tksrb011,. ./.profile 1>/dev/null 2>&1;stop_olc_dmn... (0 Replies)
Discussion started by: deepakiniimt
0 Replies

9. Shell Programming and Scripting

format output in csv file

I am sending the output of a file to .csv file. The output should look like this: Total Customers Processed:,8 Total Customers Skipped:,0 Total Customers Added:,8 Total Customers Changed:,0 Total Policies Deleted:,0 Total Policies Failed:,0 total:,8 Now i want this output in... (1 Reply)
Discussion started by: Prashant Jain
1 Replies

10. Shell Programming and Scripting

taking output in csv file from perl

Hi, I am new to perl I need to connect from linux server to oracle database and i need to query the database and take result into csv file. i try to do but i am getting this error: #!/usr/bin/perl use DBI; BEGIN { $ENV{ORACLE_HOME} = '/home/oracle/product/8.1.7'; ... (1 Reply)
Discussion started by: prakash.gr
1 Replies
Login or Register to Ask a Question