LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-29-2007, 03:12 PM   #1
rujin69
LQ Newbie
 
Registered: Mar 2007
Posts: 4

Rep: Reputation: 0
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.
 
Old 03-29-2007, 10:55 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
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.
 
Old 03-30-2007, 03:59 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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;
}

Last edited by bigearsbilly; 03-30-2007 at 04:00 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux file extension vs Dos file Extension? manaa Linux - Newbie 6 02-12-2009 04:19 PM
what is this file extension Arkan123 Linux - Newbie 5 01-14-2006 08:07 AM
File extension...?? jojojo Linux - Newbie 1 05-23-2005 04:42 PM
why when redirecting output to a file any file extension seems to be fine? dr_zayus69 Linux - General 1 05-21-2005 04:09 AM
The FIle with no extension! JafferAB Linux - General 14 01-30-2003 09:54 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration