![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| 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 !! |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
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 }'
|
|
#3
|
|||
|
|||
|
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
|
|||
|
|||
|
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") }'
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 |
|
#5
|
|||
|
|||
|
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
|
|||
|
|||
|
Code:
find /data/XYZ -mtime -7 -name 'DailyFile*.txt' -print | xargs wc ... |
|
#7
|
|||
|
|||
|
The command you gave gives the word count of the file.I want the total number of files (line count).
|
|||
| Google The UNIX and Linux Forums |