Perl Script to identify files without extension and assign a value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Perl Script to identify files without extension and assign a value
# 1  
Old 02-03-2012
Perl Script to identify files without extension and assign a value

Hi,
I have a perl script which is a part of a shell script which read lines from a flat file(which is generated as part of a script after a series of bteq/fexp) and assigns a value for each object in the file based on the type of file name. (i.e extensions like .bteq/.ctl/.ksh etc)
For example,
If the filename in the flat file is xxxxx.bteq.ctl, it checks for the keywords(i.e bteq.ctl) and assigns a value of 26(which is required to be loaded in a table).
Extract from the script is given below,
Code:
while ( <MSTR_FL> )
{
chomp;
next if ( -d $_ ); # No dirs
#next if ( /(?:Z$)|(?:gz$)|(?:tmp$)|(?:orig$)|(?:bak$)|(?:bkp$)|(?:old$)|(?:bad$)/ );
next if ( /(?:gz$)|(?:tmp$)|(?:orig$)|(?:bak$)|(?:bkp$)|(?:old$)|(?:bad$)/ );
next if ( m#(?:bid/stg/)|(?:backup)|(?:cvd_conv)|(?:/bak/)|(?:/st3527/)|(?:/bidops/)# );
 
# We have a compressed file. so remove the .Z at the end
s/.Z$//;
 
my $dir;
my $file_name;
my $obj_type_id;
if ( $file_name =~ /\.bteq\.ctl$/io )
{
$obj_type_id = 26;
}

Currently, I require the same perl script to look into the filenames without any extension and assign it a value.
eg: network_usage_dt_code ( a file with schedule details.)
These are schedule files, they are differently named and do not have any extension but a valid files and are required. These files are from specific paths as well on servers.
Please let me know if there is any such way to identify such files based on the path from which they are pulled instead of key words like(bteq, mload etc).
Thanks.

Moderator's Comments:
Mod Comment Please use next time code tags for your code and data

Last edited by vbe; 02-03-2012 at 10:40 AM..
# 2  
Old 02-03-2012
Quote:
Originally Posted by yohasini
...I require the same perl script to look into the filenames without any extension and assign it a value.
eg: network_usage_dt_code ( a file with schedule details.)
...
Please let me know if there is any such way to identify such files based on the path from which they are pulled instead of key words like(bteq, mload etc). ...
You could simply check if the file name does not have a period in it, which would mean it does not have an extension -

Code:
$
$ perl -le '@files = qw(network_usage_dt_code network_usage_dt_code.dat);
          foreach $f (@files) {
            if ($f =~ /^\w+$/) {printf("%-30s %s\n", $f, "File with no extension")}
            else               {printf("%-30s %s\n", $f, "File with an extension")}
          }'
network_usage_dt_code          File with no extension
network_usage_dt_code.dat      File with an extension
$
$

Otherwise if you want to check the specific directory to identify such files then -

Code:
$
$ perl -le '@files = qw(/my/path/network_usage_dt_code /not/my/path/network_usage_dt_code);
            foreach $f (@files) {
              ($dir, $filename) = $f =~ m|^(.*/)(.*?)$|;
              if ($dir eq "/my/path/") {printf("%-40s %s\n", $f, "File is in my path")}
              else                     {printf("%-40s %s\n", $f, "File is not in my path")}
            }'
/my/path/network_usage_dt_code           File is in my path
/not/my/path/network_usage_dt_code       File is not in my path
$
$

Of course, the latter script would work only if your file handle "MSTR_FL" points to a file with an absolute path, rather than just the file name.

tyler_durden

Last edited by durden_tyler; 02-03-2012 at 10:01 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Display the .csv extension files based on .done extension fine

Hi All, I want to fetch the files based on .done file and display the .csv files and Wil take .csv files for processing. 1.I need to display the .done files from the directory. 2.next i need to search for the .Csv files based on .done file.then move .csv files for the one directory ... (2 Replies)
Discussion started by: girija.g6
2 Replies

2. Shell Programming and Scripting

How to read a two files, line by line in UNIX script and how to assign shell variable to awk ..?

Input are file and file1 file contains store.bal product.bal category.bal admin.bal file1 contains flip.store.bal ::FFFF:BADC:CD28,::FFFF:558E:11C5,6,8,2,1,::FFFF:81C8:CA8B,::FFFF:BADC:CD28,1,0,0,0,::FFFF:81C8:11C5,2,1,0,0,::FFFF:81DC:3111,1,0,1,0 store.bal.... (2 Replies)
Discussion started by: veeruasu
2 Replies

3. Shell Programming and Scripting

Perl help - how to assign output of perl to variable

Hi, guys, i have a script i inherited from a coworker but i'm not perl savy. The script works but i would like it to work better. I want to run this command ./ciscomgrtest.pl -r "show version" -h hosts.router and have the script goto each router in the hosts.router file and run the command... (2 Replies)
Discussion started by: whipuras
2 Replies

4. Shell Programming and Scripting

Perl -> Identify Values

Hi, I tried to find out the users under the file /etc/shadow , where the value should be 90, which shouldn't be less or more than that.. if its more or less, it should give me the output as BAD. { print (GOOD); } Else { print (BAD); } (10 Replies)
Discussion started by: gsiva
10 Replies

5. Shell Programming and Scripting

perl script to remove the extension from its name

There are few files in my windows directory and I need a perl script to rename the files to its original names i.e., the last extension(.orig) needs to be removed programatically, files in directory data1.htm.orig data2.htm.orig data3.htm.orig to be renamed to data1.htm data2.htm... (4 Replies)
Discussion started by: giridhar276
4 Replies

6. Shell Programming and Scripting

Bash script to find the number of files and identify which ones are 0 bytes.

I am writing a bash script to find out all the files in a directory which are empty. I am running into multiple issues. I will really appreciate if someone can please help me. #!/bin/bash DATE=$(date +%m%d%y) TIME=$(date +%H%M) DIR="/home/statsetl/input/civil/test" ... (1 Reply)
Discussion started by: monasharma13
1 Replies

7. UNIX for Advanced & Expert Users

Script to sort the files and append the extension .sort to the sorted version of the file

Hello all - I am to this forum and fairly new in learning unix and finding some difficulty in preparing a small shell script. I am trying to make script to sort all the files given by user as input (either the exact full name of the file or say the files matching the criteria like all files... (3 Replies)
Discussion started by: pankaj80
3 Replies

8. Shell Programming and Scripting

Help - Script to move files to subdir depending on file extension.

Hi, First off I'm pretty new to scripting so please be gentle. I am looking for some help with a script that will move all files with a certain extension into a folder within their current location. Just for clarity I have all my photos organised in directories such as: ... (4 Replies)
Discussion started by: guinch
4 Replies

9. Shell Programming and Scripting

Using an awk script to identify dupes in two files

Hello, I have two files. File1 or the master file contains two columns separated by a delimiter: a=b b=d e=f g=h File 2 which is the file to be processed has only a single column a h c b What I need is an awk script to identify unique names from file 2 which are not found in the... (6 Replies)
Discussion started by: gimley
6 Replies

10. Shell Programming and Scripting

Shell script to identify the number of files and to append data

Hi I am having a question where I have to 1) Identify the number of files in a directory with a specific format and if the count is >1 we need to concatenate those two files into one file and remember that in the second file the header should not be copied. it should be form first file.... (4 Replies)
Discussion started by: pradkumar
4 Replies
Login or Register to Ask a Question