The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 11-17-2008
mainegate mainegate is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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?
  #2 (permalink)  
Old 11-17-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Code:
filename=2008-11-10-myfilename
newfilename=${filename##*-}
echo $newfilename
  #3 (permalink)  
Old 11-17-2008
cfajohnson's Avatar
cfajohnson cfajohnson is online now Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,311

Code:
filename=2008-11-17-qwerty-uiop
dpat=[12][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]
newfilename=${filename#$dpat-}
echo $newfilename
Not ${filename##*-}, as that fails if there is a hyphen in the balance of the filename, or if there is no date but there is a hyphen in the name.
  #4 (permalink)  
Old 11-17-2008
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
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
Basically I would suggest that naming every file with a leading date has problems. try
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;
}
Good luck with this.
  #5 (permalink)  
Old 11-18-2008
mainegate mainegate is offline
Registered User
  
 

Join Date: Aug 2007
Posts: 23
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -4. The time now is 05:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0