Sponsored Content
Top Forums Shell Programming and Scripting detecting the part of a filename Post 302259281 by jim mcnamara on Monday 17th of November 2008 05:13:15 PM
Old 11-17-2008
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.
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

part of a filename

Hi, I need to extract only a part of the filenames of some files. The files are named this way : .tap_profile_SIT02 I want the "SIT02" part, which is not the same for each file. I was able to get what I want with bash, but not with ksh. Here is the command I used in bash : find... (8 Replies)
Discussion started by: flame_eagle
8 Replies

2. UNIX for Dummies Questions & Answers

Strip part from filename

I've many file like this 01-file 01_-_file 01_-_file 01_-_file 01_-_file 01-file I would remove bold part from filename. Suggestions?Thanks (4 Replies)
Discussion started by: cv313x
4 Replies

3. Shell Programming and Scripting

Getting part of a filename

Hi All, I'm trying to get part of a filename and my skill with regular expression are lacking. I know I need to use SED but have no idea how to use it. I'm hoping that someone can help me out. The file names would be: prefix<partwewant>suffix.extension the prefix and suffix are always 3... (4 Replies)
Discussion started by: imonkey
4 Replies

4. Shell Programming and Scripting

cut the some part in filename

Hi All, I have the file & name is "/a/b/c/d/e/xyz.dat" I need "/a/b/c/d/e/" from the above file name. I tryning with echo and awk. But it not come. Please help me in this regard. Thanks & Regards, Dathu (3 Replies)
Discussion started by: pdathu
3 Replies

5. UNIX for Dummies Questions & Answers

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv OTP_UFSC_20120530000000_prom.csv... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

6. Programming

to extract all the part of the filename before a particular word in the filename

Hi All, Thanks in Advance I am working on a shell script. I need some assistance. My code: if then set "subscriber" "promplan" "mapping" "dedicatedaccount" "faflistSub" "faflistAcc" "accumulator"\ "pam_account"; for i in 1 2 3 4 5 6 7 8;... (0 Replies)
Discussion started by: aealexanderraj
0 Replies

7. Shell Programming and Scripting

Extract a part of a filename containing a particular word

Hi All, Thanks in Advance Shell Script or Perl Script I am working on a shell script. I need some assistance. My Requirement: 1) There are some set of files in a directory like given below OTP_UFSC_20120530000000_acc.csv OTP_UFSC_20120530000000_faf.csv... (7 Replies)
Discussion started by: aealexanderraj
7 Replies

8. UNIX for Dummies Questions & Answers

Replacing part of filename

Hi guys! I have quite a lot of files like all_10001_ct1212307460308.alf* and I want to get rid of the first number for all at once like: all_ct1212307460308.alf* How can I do this in the shell? (12 Replies)
Discussion started by: TimmyTiz
12 Replies

9. UNIX for Dummies Questions & Answers

Extract Date part from the filename

Hi All, I have incoming source files abcmmyy.txt I need to extract the mmyy part from the filename and pass that to a variable . I really appreciate your quick response on this. Thanks raj (7 Replies)
Discussion started by: rajeevm
7 Replies

10. Shell Programming and Scripting

Finding the part of a filename

Hi, I am writing an ebuild for Gentoo Linux operating system. Writing an ebuild is about Bash scripting where I am a newbie. So, my ebuild must find a part of a specific filename. Such a filaname my look like this: libvclient_release_x64.so.740and I must to find the number at the and of... (18 Replies)
Discussion started by: csanyipal
18 Replies
ftpconversions(4)					     Kernel Interfaces Manual						 ftpconversions(4)

NAME
ftpconversions - ftpd conversions database SYNOPSIS
DESCRIPTION
The conversions known by and their attributes are stored in an ASCII file that is structured as below. Each line in the file provides a description for a single conversion. Fields are separated by colons (:). Field Description 1 strip prefix 2 strip postfix 3 addon prefix 4 addon postfix 5 external command 6 types 7 options 8 description strip postfix and addon postfix can be the file extensions or or or The file extensions the ftp server supports and the action that the ftp server performs is summarized in the table below. This feature is supported only when using the command. | | True Filename | Specified Filename | Action --------------+--------------------+--------------------------------------- filename.Z | filename | Decompress file before transmitting filename | filename.Z | Compress filename before transmitting filename | filename.tar | Tar filename before transmitting filename | filename.tar.Z | Tar and compress filename | | before transmitting external command field is used to specify the path of the program/script to be used to perform the action specified for the different file extensions as described in the above table. types field indicates the flags that must be checked before compressing, decompressing, tarring and untarring the file requested by the command. options field specifies the options for the program/script specified in the external command field. description is a short description of each conversion. NOTE: The HP-UX command does not support any option to do or If the user wants to perform any such action, he/she must then write his/her own program/script to do it and specify the pathname of the program/script in the external command field in the file. Also a copy of the programs/scripts used to perform the conversions and the respective libraries used by the programs/scripts must be copied into the home directory of each of the and user accounts for the and users to be able to perform on-the-fly conversions. WARNINGS
The conversions mechanism does not currently support the strip prefix and addon prefix fields. FILES
AUTHOR
was developed by the Washington University, St. Louis, Missouri. SEE ALSO
ftpd(1M), ftpaccess(4). ftpconversions(4)
All times are GMT -4. The time now is 04:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy