How to find the date format


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to find the date format
# 8  
Old 08-21-2012
Are the sizes of the "date only" file always smaller than the other files? So, can we not use size of file for the criterion? Then we do not have to worry about all possible file contents.
# 9  
Old 08-21-2012
Yeah the date only file is smaller but the other file sizes may vary.

Can it be done by sed or awk command.

get head -1 and then check if the record is like YYYYMMDDHH:MM:SS or get only first 8 characters from the first record and see if it is YYYYMMDD ?
# 10  
Old 08-21-2012
error correction in red
Let's assume your "empty files" are never larger than 20 bytes. You pick the number.
Code:
filesize=$( cat filename | wc -c)
if [ $filesize -gt 20 ] ; then
   echo 'good file'
else
   echo 'empty file'
fi
# the above stuff can be made into a one liner:
 [ $( cat filename | wc -c) -gt 20 ] && echo 'good file' || echo 'empty file'

Be sure to leave the spaces in those commands.

Note: the use cat is intentional, we just want a number.

Last edited by jim mcnamara; 08-21-2012 at 05:31 PM.. Reason: wc -l changed to wc -c
# 11  
Old 08-21-2012
wc -l < filename would be equivalent...
# 12  
Old 08-21-2012
Quote:
Originally Posted by eskay
Yeah the date only file is smaller but the other file sizes may vary.

Can it be done by sed or awk command.

get head -1 and then check if the record is like YYYYMMDDHH:MM:SS or get only first 8 characters from the first record and see if it is YYYYMMDD ?
Yes, it can be done by: "sed", "awk", "grep" and probably some dozen other utilities, but that is not the point. The point is, you - you - have to come up with a definition of what constitutes an "empty file" versus an "other file". We can help you put that definition into code, but we can't do the definition for you, because we do not know your input as well as you do.

Let's see: Your empty files have only one lines, whereas other files have several lines, yes? (I don't really know this, i just deduce from your samples and might as well be wrong. Bottom line is, you will have to decide for yourself, ultimately.) This could be tested with

Code:
wc -l /path/to/your.file

If this gives a value greater than 1 then it is an "other" file, otherwise it might be both.

Next: if it is an empty file it has a time stamp of a certain form at the beginning of the line, so we can match it:

Code:
grep -q "^20[0-9]\{7\}:[0-9]\{2\}:[0-9]\{2\}$" /path/to/your.file

This will return 0 if the expression (your sample timestamp as a regexp) is true and your file is an empty file, otherwise it will return "$? > 0" and your file is an other file.

Couple these checks and in all likelihood you will do it correctly provided that your samples were significant for your input data. If there are other things distinguishing one class of files from the other than work some checks for these into your script so that you raise the chance of correct answers.

Was that so difficult?

I hope this helps.

bakunin
# 13  
Old 08-21-2012
The reason for wc -c is that some examples seemed to have more than one line...
if that is wrong the wc -l approach is better.

@eskay: wc -l counts lines, wc -c counts bytes, in a given file
# 14  
Old 08-22-2012
Perhaps consider using "touch" to see if the first line has a valid date...
Code:
if touch -t $(head -c 13 abc.txt|cut -c1-10,12-13) abc.txt
then 
     echo empty
else
     echo not empty 
fi

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. Shell Programming and Scripting

To find files having filename containing specific date format

Hi, I have a requirement to create a shell script(tcsh) that finds all the files in a directory having the file name containing date format "YYYYMMDDHHMM" and extract the date time part ""YYYYMMDDHHMM" for further processing. Could you please have any idea on this. trades_201604040000.out... (6 Replies)
Discussion started by: gopal.biswal
6 Replies

3. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

4. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

5. Shell Programming and Scripting

Find first created file date in YYYYMMDD format

Hi All, We are copying all the files into ARCHIVE directory after we process them. We are doing this process from last 2 years, now we have a lot of files in ARCHIVE directory. Now I need to find when the first file is copied into this directory? If I Issue, ls -l /ARCHIVE/*.* | tail -1... (3 Replies)
Discussion started by: Raamc
3 Replies

6. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

7. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

9. UNIX for Advanced & Expert Users

date issue-find prevoius date in a patricular format

Hi , I have written a shell script that takes the current date on the server and stores it in a file. echo get /usr/home/data-`date '+%Y%d'`.xml> /usr/local/sandeep/GetFILE.ini I call this GetFILE.ini file from an sftp program to fetch a file from /usr/home/ as location. The file is in... (3 Replies)
Discussion started by: bsandeep_80
3 Replies

10. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies
Login or Register to Ask a Question