creating a CSV file for past 7 days


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers creating a CSV file for past 7 days
# 1  
Old 08-23-2008
creating a CSV file for past 7 days

I have a requirement which will select the files with a specific naming convention which got created in past 7 days in a specific directory.Lets say the directory is /data/XYZ and the file names follow the below nomenclature like Daily_File*.txt
I just need to create one CSV file which will contain the file name,record count of the file and its size(bytes or KB or MB or GB mentioned along with the value).

Can anyone please help me writing this script.
# 2  
Old 08-23-2008
If by "record count" you mean "line count", read the wc command's manual page.

Code:
wc /data/XYZ/Daily_File*.txt | nawk -v OFS=, '{ print $4, $1, $3 }'

I guess the "below nomenclature" was supposed to explain something more about what would come after Daily_File but you didn't explain that. The above will run over all the files.
# 3  
Old 08-23-2008
Its line count .
So
wc -l /data/XYZ/Daily_File*.txt | nawk -v OFS=, '{ print $4, $3, $1 }'

How it will find the filesize.Usually the file size is presented by K or G or M in unix and only the numeric value in case of bytes.I want to have KB MB GB Bytes in my CSV file.
Like

Daily_File_Students.txt,1500,10 MB
Daily_File_Teachers.txt,1100,11 MB
Daily_File_Staff.txt,50,10 KB
Daily_File_Fees.txt,112,220 Bytes
# 4  
Old 08-23-2008
Take out the -l from the wc command and you get byte (and word) counts for each file as well. (Why did you add it in the first place?) Some simple post-processing to divide by 1,000 or 1,024 and increasing the unit is easy to add to the awk script. (Do you want MB or MiB, i.e. 1000 or 1024? See http://en.wikipedia.org/wiki/Mebibyte)

Code:
wc /data/XYZ/Daily_File*.txt |
nawk -v OFS=, '{ bytes=$3; suff="KMGT"; i=0;
   while (bytes > 1000) { bytes = int(bytes/1000); i++ }
   print $4, $1, bytes (i > 0 ? (" " substr(suff,i,1) "B") : " bytes") }'

For 1024-byte units, bytes >>= 10 is probably more efficient than bytes = int(bytes/1024)

As an engineering recommendation, I would still suggest that you put the raw numbers in the CSV file, and leave the arrangement of the presentation as KiB or whatever to the consumer of that file.

Last edited by era; 08-23-2008 at 03:12 PM.. Reason: Link to Wikipedia
# 5  
Old 08-23-2008
Thanks for the code..
But the initial requirement was to have the files created in past 7 days.We need to chek the date of file creation before posting them in the CSV file.
Can you please let me know how this code can be modified to incorporate the above requirement.
# 6  
Old 08-23-2008
Code:
find /data/XYZ -mtime -7 -name 'DailyFile*.txt' -print | xargs wc ...

# 7  
Old 08-24-2008
The command you gave gives the word count of the file.I want the total number of files (line count).
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Should pick latest file within past 3 days using UNIX script and perform steps in message below.

Hi , Can anyone help me how do perform below requirement in unix. Step1:we will receive multiple files weekly with same name(as below) in a folder(In folder we will have other files also def.dat,ghf.dat) Filenames: 1) abc_20171204_052389.dat 2)abc_20171204_052428.dat DON'T modify... (23 Replies)
Discussion started by: sunnykamal59
23 Replies

2. UNIX and Linux Applications

From past 10 days my one job is taking lots of time

One of my job is taking long running time. I need to identify from the unix log file can you please help how to troubleshoot. (1 Reply)
Discussion started by: Nsharma3006
1 Replies

3. Shell Programming and Scripting

Subtract 2 date columns in .csv file and get output as number of days

Hi, I have one .csv file. I have 2 date columns present in file, column 2 and column 3. I need to calculate how many days exist between 2 dates. I am trying to subtract date column 2 from date column 3. Eg: my file look likes s.no, Start_date,End_Date 1, 7/29/2012,10/27/2012 2,... (9 Replies)
Discussion started by: Dimple
9 Replies

4. UNIX for Dummies Questions & Answers

Problem in creating CSV file

Hi guys, I am not experienced with Unix, so please dont mind if the question seem to be irrelevant. I have written a simple script, that connects DB & fetches few records from a table. I wanted to get those details as file in .CSV format via mail. -I stored the query o/p in a file. -I... (6 Replies)
Discussion started by: sumitburnwal88
6 Replies

5. Shell Programming and Scripting

Listing files of PAST 7 days and concatenate it...

Hi All, I want to list file of LAST 7 days acc. to its modified date and then concatinate. I have following piece of code.. For concatenate cat file1 file2 >> Output (For concatinating) find . -mtime -7 -exec basename {} \; (list past files but it is including . file also) Plz... (4 Replies)
Discussion started by: vivek1489
4 Replies

6. Shell Programming and Scripting

creating a csv file from this 1 liner?

I'm trying to create a csv file by running awk and sed on a number of xml files in a directory; I'm using this below: hostname; grep "BuildDate" /dir/ABCD/configuration/*/*.xml | awk -F"/" '{ print $5 }' > /tmp/tempfile.txt; grep "BuildDate" /dir/ABCD/configuration/*/*.xml | awk -F\" '{ print $2... (2 Replies)
Discussion started by: rich@ardz
2 Replies

7. UNIX for Dummies Questions & Answers

Creating a report from csv file.

Hi Gurus, I need your help in transforming the CSV file into some what a report format. My source file looks like below Date,ProdID,TimeID,LevelID 2010-08-31,200,M,1 2010-08-31,201,Q,2 2010-08-31,202,Y,1 2010-08-31,203,M,5 Output required is ... (9 Replies)
Discussion started by: naveen.kuppili
9 Replies

8. Shell Programming and Scripting

find files from the past 7 days

Hi All, I have a file which contains the listing of another directory: >cat list.dat -rwxr-xr-x 1 test staff 10240 Oct 02 06:53 test.txtdd -rwxrwxrwx 1 test staff 0 Oct 04 07:22 test.txx -rwxrwxrwx 1 test staff 132 Sep 16 2007 test_tt.sh... (6 Replies)
Discussion started by: deepakgang
6 Replies

9. Shell Programming and Scripting

creating a csv file in awk

Hi All I am trying to create a csv file in the korn shell and the script segment is as follows: if then # NEED TO ADD INFO TO THE EMAIL FILE ABOUT THE DRIVE THAT'S FILLING UP echo "$drive $percent% $space "|\ awk '{printf("%d/t"|"%d/t"|"%d/t\n",... (6 Replies)
Discussion started by: Segwar
6 Replies
Login or Register to Ask a Question