LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Testing if a file has a particular extension (https://www.linuxquestions.org/questions/programming-9/testing-if-a-file-has-a-particular-extension-541713/)

rujin69 03-29-2007 03:12 PM

Testing if a file has a particular extension
 
I am hoping for some ideas on testing if a file has an extension passed as a command line argument. The challenge involves testing multiple extensions in a script. For example, if the file is a m3u, sfv, or xml file then cat it and perform some command. I don’t think the following works for me; for FILE in *. m3u *. sfv *. xml.

I chose FIND for added portability and additional options. I have gotten this far.

Code:

find . -depth \(-name ’*.sfv’ -o -name ‘*.m3u’\) -type f -print
The Bash script should accept command line arguments with several extensions. scriptname.sh --ext *.m3u,*.xml,*.sfv.

The script performs recursive searches and finds files with these extensions. Hard coding the extension would be the easy part. After finding the said files, it iterates (while loop) through an external file with regular expression patterns. Then it performs replacements inside the file and output a new file. This Perl line helps me achieve this goal;

Code:

perl -pi -e "s/$REGEX/$REPLACE/g" $FILE
This portion script complements the main purpose of renaming files. So the script rename a bunch of files (right now it consist of mp3 files) then renames the files in the playlist (*.m3u) and sfv (*.sfv) files. I may also need to grep for lines with mp3 line, because the script should perform the replacements on the mp3 pointers since these were the files renamed.

ntubski 03-29-2007 10:55 PM

Quote:

Originally Posted by rujin69
I am hoping for some ideas on testing if a file has an extension passed as a command line argument. The challenge involves testing multiple extensions in a script. For example, if the file is a m3u, sfv, or xml file then cat it and perform some command. I don’t think the following works for me; for FILE in *. m3u *. sfv *. xml.

I chose FIND for added portability and additional options. I have gotten this far.

Code:

find . -depth \(-name ’*.sfv’ -o -name ‘*.m3u’\) -type f -print

I don't know what the \( and \) are for?? And what's with the funny quote characters? ’‘ These are neither backtick `, nor single quote: '.


Quote:

The Bash script should accept command line arguments with several extensions. scriptname.sh --ext *.m3u,*.xml,*.sfv.
Is the script going to get a bunch of file names on the command line, or a bunch of extensions? If you want to recieve extensions and then recursively look for files, I'd do something like
Code:

find . -iregex ".*\\.\\(m3u\\|xml\\|sfv\\)$" -type f -print
If I was getting a list of files and wanted to perform an operation on every depending on its extension:
Code:


for file in $file_list; do
  case $file in
      *.m3u) echo 'got m3u file';;
      *.xml) echo 'got xml file';;
      *.sfv) echo 'got sfv file';;
      *) echo 'got unexpected file';;
  esac
done

Quote:

The script performs recursive searches and finds files with these extensions. Hard coding the extension would be the easy part. After finding the said files, it iterates (while loop) through an external file with regular expression patterns. Then it performs replacements inside the file and output a new file. This Perl line helps me achieve this goal;

Code:

perl -pi -e "s/$REGEX/$REPLACE/g" $FILE
This portion script complements the main purpose of renaming files. So the script rename a bunch of files (right now it consist of mp3 files) then renames the files in the playlist (*.m3u) and sfv (*.sfv) files. I may also need to grep for lines with mp3 line, because the script should perform the replacements on the mp3 pointers since these were the files renamed.
I'd be inclined to use sed here, if all you need is s// function. Or just make the entire thing in perl.

bigearsbilly 03-30-2007 03:59 AM

do it in one with perlpower ...

this searches for supplied file extensions, (like .c .pl) whatever (DO NOT include the * as in *.c)
and substitues THIS for THAT with a .bak file.

Code:

#! /usr/local/bin/perl -w


@ARGV or die "example:\n$0 .c .m3u \n";
warn "looking for @ARGV\n";

@L = qx/find . -type f/;
chomp @L;

# grep for file extensions
# \Q\E quotes the . (\.c \.m3u)
# =========================
foreach $ext (@ARGV) {
    push @G, grep m/\Q$ext\E$/, @L;
}

# warn "Found:@G\n";

# assign @ARGV,
# lazy way to use automagic open operator <>
# ========================================================
@ARGV = @G;

$^I = ".bak"; # edit inplace with backup files

# do substitutions
while (<>) {

    s/THIS/THAT/;
    print;
}



All times are GMT -5. The time now is 09:59 AM.