Create csv with output filenames and file size


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Create csv with output filenames and file size
# 1  
Old 04-17-2013
Create csv with output filenames and file size

Hello All,

Here is seeking a bit of help in trying to solve a problem.

I am required to create a csv file as shown below:
output.csv ->
output_1,output_2,output_3,...,output_<N>
filename1:20,filename2:30,filename3:30,...,filename<N>:30

by listing output_1, output_2,... , output<N> as header separated by a comma in line 1 for all file names in a particular directory and their corresponding file names and file size(s) separated by a colon in line 2

I've tried to use the code shown below but I'm having trouble trying to resolve the value of the parameter in the log.

#capture the file size of output files
typeset -i op_cnt=1;
ls -lrt <dirname>|awk '{FS = ",|[ \t]+"} NR>1 { print $9, $5 }'|tr " " ":"|while read line
do
op_file_$op_cnt=`echo $line`
op_val_$op_cnt="echo \$$op_file_$op_cnt"
op_val_$op_cnt=`eval $op_val_$op_cnt`
echo "$op_file_$op_cnt=$op_val_$op_cnt" >> <output.csv>
done

Requesting your help and expert guidance.
# 2  
Old 04-17-2013
Could you use code tags? Thanks
This User Gave Thanks to hanson44 For This Post:
# 3  
Old 04-17-2013
Please use code tags when posting code fragments or data samples.

Also you did not mention what OS or SHELL you are using!

Here is a KSH code using Indexed Arrays:
Code:
#!/bin/ksh

typeset -a File_Array

for file in *
do
        (( ++c ))
        File_Array[$c]="$file:$(stat --printf=%s $file)"
done

for k in ${!File_Array[@]}
do
        [[ ! -z "$header" ]] && header="${header},output_${k}" || header="output_${k}"
        [[ ! -z "$fields" ]] && fields="${fields},${File_Array[$k]}" || fields="${File_Array[$k]}"
done

print "$header"
print "$fields"

This User Gave Thanks to Yoda For This Post:
# 4  
Old 04-18-2013
Thank you for your replies. I will ensure I use code tags when I write code henceforth.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk and sed script to create one output CSV file

Hi All , I would require your help to generate one output file after post processing of one CSV file as stated below This file is just a small cut from a big file . Big file is having 20000 lines PATTERN,pat0,pat1,pat2,pat3,pat4,pat5,pat6,pat7,pat8,pat9... (2 Replies)
Discussion started by: kshitij
2 Replies

2. Shell Programming and Scripting

Create csv from text file

Gents, I am trying to create a csv file using the file attached. I have a problem to get all information required because the rows are not continues. Here is my code till now. awk ' /"ffid"/{if(s){print s;s=$NF}else{s=$NF}} /"LineNumber"/{s=s $NF} /"PointNumber"/{s=s $NF}... (4 Replies)
Discussion started by: jiam912
4 Replies

3. 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

4. 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

5. Shell Programming and Scripting

Compare 2 files of csv file and match column data and create a new csv file of them

Hi, I am newbie in shell script. I need your help to solve my problem. Firstly, I have 2 files of csv and i want to compare of the contents then the output will be written in a new csv file. File1: SourceFile,DateTimeOriginal /home/intannf/foto/IMG_0713.JPG,2015:02:17 11:14:07... (8 Replies)
Discussion started by: refrain
8 Replies

6. Shell Programming and Scripting

Need help. How to create csv file?

Hi I'm a beginner and I have some problem. I have multiple files in the same directory which has one column but rows following the format. File: directory/Disk.txt Content: a b c d e File: directory/Memory.txt a b c d e File: directory/CPU.txt (3 Replies)
Discussion started by: thenuie
3 Replies

7. Shell Programming and Scripting

Script to create a CSV file

I created a script that will go out and so a "/sbin/chkconfig --list | egrep XXX" against a server list that would create an output file like the following example: ---------------------------------------------------------------------------------- SERVER1 RC_Script_1 0:off 1:off 2:off... (4 Replies)
Discussion started by: asnatlas
4 Replies

8. Shell Programming and Scripting

Extract variables from filenames and output to file

I need some help. I have a list of files (thousands) and would like to extract some variables from the file name and save that to a file The list of files look like: I am trying to write the following script but I am stuck at how I can get thevariables 'doy' and 'yr' from each file and then... (5 Replies)
Discussion started by: malandisa
5 Replies

9. UNIX for Dummies Questions & Answers

need to create a file of specified size

Hi, I need to create a file using touch command . I want the size to be of 300 MB . Is it possible with touch or any other command. Thanx for your help. (2 Replies)
Discussion started by: reply2soumya
2 Replies

10. UNIX for Dummies Questions & Answers

to create a file of specified size

hi guys, Is there any way to create a file of specified size ..ie...during creation itself u should be able to specify the size of it... (12 Replies)
Discussion started by: deep
12 Replies
Login or Register to Ask a Question