![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| gzcat into awk and then change FILENAME and process new FILENAME | timj123 | Shell Programming and Scripting | 6 | 06-23-2008 07:45 AM |
| part of a filename | flame_eagle | Shell Programming and Scripting | 8 | 02-28-2008 11:18 AM |
| Report of duplicate files based on part of the filename | sudheshnaiyer | UNIX for Dummies Questions & Answers | 1 | 12-18-2007 04:31 PM |
| read a part of filename from the list in the script | happyv | Shell Programming and Scripting | 3 | 10-20-2006 09:58 AM |
| Wanted to eliminate numeric part from a filename | Sona | UNIX for Dummies Questions & Answers | 8 | 07-20-2006 02:49 PM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
||||
|
detecting the part of a filename
I like to have the date in the 2008-09-01 format at the beginning of my filenames. I then hyphenate after that and then have my filename.
I have a script that creates this for me. However, I may be working on files that already have the date format already in there and so I don't want to have a date twice. Is there a way to detect number or something at the beginning of the filename and if so then cut out the date? |
|
||||
|
That was in lieu of this, dates are messy
Code:
echo $filename | \
sed 's/\(^[1-2][0-9]\{3\}\)-\([0-1][0-9]\)-\([0-3][0-9]\)-\([A-Z_0-9a-z]*\)/\4/' | read newfilename
if [[ ! -z newfilename ]] ; then
filename=$newfilename
fi
ls -l instead. Chris was technically correct -no doubt - but the usefulness of the above is limited. In part this is due to the way dates are constructed - and the above regex is not perfect. It will think 2008-19-19 is a date - which it is not. Most correct date regexes are several hundred character long. The only gold standard is to refer to the output of cal. We use part of this date check algorithm below (simplified greatly) for validating old dates. This is more nearly complete && correct than most straight shell implementations using regex are likely to be. I deliberately excluded Code:
/* chpdt.c
usage chpdt filename
echo "$filname" | chpdt
*/
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>
#define isleapyr(y) (((y) % 4) == 0 && ((y) % 100) != 0 || ((y) % 400) == 0)
int leap[12]= {31,29,31,30,31,30,31,31,30,31,30,31};
int nleap[12]={31,28,31,30,31,30,31,31,30,31,30,31};
char *date_ok(char *result, char *src)
{
int value=atoi(src);
int month=0;
char *p=src;
int *v=nleap;
if( value >= 1900 && value < 2038 && (p=strchr(p, '-')) != NULL )
{
if(isleapyr(value))
v=leap;
value=atoi(++p);
if(value >= 1 && value < 13 && (p=strchr(p, '-')) != NULL)
{
month=value;
value=atoi(++p);
if( value > 0 && value < v[ month - 1 ] )
if( (p=strchr(p, '-')) != NULL)
result=++p;
}
}
return result;
}
char *name_ok(char *p, char *src)
{
if(src[4]=='-' && src[7]=='-' && src[10]=='-')
p=date_ok(p, src);
return p;
}
int main(int argc, char **argv)
{
int retval=0;
char tmp[PATH_MAX + 2]={0x0};
char *p=NULL;
if (argc < 2)
{
if(fgets(tmp, sizeof(tmp), stdin) ==NULL)
{
perror("I/O error");
retval=1;
};
p=strchr(tmp, '\n');
if(p!=NULL)
*p=0x0;
}
else
(void)strncpy(tmp, argv[1], PATH_MAX);
p=tmp;
if(strlen(tmp) > 10 && !retval) /* will return zero length if string is "date-"*/
p=name_ok(p, tmp);
if( fprintf(stdout, "%s", p) < 0)
{
perror("I/O error");
retval=1;
}
return retval;
}
|
|
||||
|
Thanks guys. I went with Chris's solution. It is limited like you said Jim (you need to have exact syntax for it to work) but Jim your solution is way over my head for a beginner! For what I am doing Chris's will do just fine. Thanks to all of you.
|
| Sponsored Links | ||
|
|
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|