The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
remove a file after 30 days dr46014 Shell Programming and Scripting 7 11-09-2007 06:06 AM
Find accessed file in past 1 or 2 minutes, and throw mail. varungupta UNIX for Advanced & Expert Users 2 09-12-2007 12:07 AM
Find the file from 15 days ago YoungBlood Shell Programming and Scripting 2 03-03-2007 04:28 PM
file was created before 15 days ago. YoungBlood UNIX for Dummies Questions & Answers 1 03-02-2007 10:23 AM
ls latest 4 days or specify days of files in the directory happyv Shell Programming and Scripting 3 01-22-2007 04:16 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-23-2008
Registered User
 

Join Date: Jun 2007
Posts: 70
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.
Reply With Quote
Forum Sponsor
  #2  
Old 08-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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.
Reply With Quote
  #3  
Old 08-23-2008
Registered User
 

Join Date: Jun 2007
Posts: 70
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
Reply With Quote
  #4  
Old 08-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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 11:12 AM. Reason: Link to Wikipedia
Reply With Quote
  #5  
Old 08-23-2008
Registered User
 

Join Date: Jun 2007
Posts: 70
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.
Reply With Quote
  #6  
Old 08-23-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Code:
find /data/XYZ -mtime -7 -name 'DailyFile*.txt' -print | xargs wc ...
Reply With Quote
  #7  
Old 08-24-2008
Registered User
 

Join Date: Jun 2007
Posts: 70
The command you gave gives the word count of the file.I want the total number of files (line count).
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
csv, file size

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 09:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0