File Extension Substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting File Extension Substitution
# 1  
Old 05-26-2016
File Extension Substitution

Hi,

I have a script in which the file name is always known, but the extension could vary. I want to be able to use a single variable; no if-else statements. For example, if I have
Code:
config.txt

in the directory then that's what I want to use, but if
Code:
config.xml

is in the directory then use that. The point being,
Code:
config

is always constant. Is this possible?

I have a variable like this in my script:

Code:
file=/home/config.{xml,txt}

The script returns with
Code:
Cannot find or open file /home/config.{xml,txt}

Help is appreciated.
# 2  
Old 05-26-2016
Hello ocbit,

To traverse through all the .txt and .xml files, then following may help you in same.
Code:
for file in *.{txt,xml}; do echo $file; done
OR
for file in *.{txt,xml}
do
    echo $file
done
OR (EDIT)
perl -e '
   $DIR="/path/to/check";
   @a= glob "${DIR}/*.txt";
   @b= glob "${DIR}/*.xml"; 
   print  @a  ? "Found txt file.\n" : "Not found txt file.\n";
   print  @b  ? "Found xml file.\n" : "Not found xml file.\n"
 '

In above code I am not using any file name you could add file first name in it. Also I am simply printing the Input_file names, you could do operations as per your requirement too. If you have some other requirement then please do let us know it with complete details.

EDIT: Added a perl code above too.

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-26-2016 at 11:55 AM.. Reason: Added a perl code now too.
# 3  
Old 05-26-2016
To clarify:

I have an awk script which does string replacement based on an input xml and a properties file; this script returns the same xml with updated values.

The input xml can either be
Code:
config.xml

or
Code:
config.xml.bak

. (I mentioned txt and xml before but it doesn't matter).

In my main script I want to define a variable such that any config extension type can be accepted, since only xml or xml.bak will be present in the directory.

Code:
#!/bin/sh
deploymentXML=/home/Config.{xml,xml.bak}
properties=/home/prop.txt

/home/updateXML.sh "$properties" ${deploymentXML}


Last edited by ocbit; 05-26-2016 at 12:15 PM.. Reason: added tags
# 4  
Old 05-26-2016
Quote:
Originally Posted by ocbit
Thanks Ravinder, but I don't wish to traverse any files.
I will only have
Code:
.txt

OR
Code:
.xml

file in my directory at any point in time (one or the other). In the script, I want to assign a variable which will be general enough to accept a file name such as
Code:
config.txt

OR
Code:
config.xml

.
How can I assign a shell variable to accept something like
Code:
file=/home/config.<txt,xml>

Hello ocbit,

I am still unsure about your complete requirement. I am still assuming like you need to check file's status only either it is present or not. If yes, then why not create 2 variables and check their status like as follows.
Code:
file_txt=/my/path/config.txt
file_xml=/my/path/config.xml
if [[ -f $file_txt ]]; then echo $file_txt " is present."; fi
if [[ -f $file_xml ]]; then echo $file_txt " is present."; fi

You could take it above as a start and try, if you have more requirements then please be more precise into them and show us sample Input_file with expected output.

EDIT: Seems you have updated your post now, still I am not that much sure about your requirement but seeing your code assuming following things you could do.

I- No need to call a separate script within script, you could directly call that awk command there itself, not sure if that has more than command, if no then you could directly mention command there and pass arguments.
II- You could still avoid creating mess of a variable having 2 extensions by creating 1 single variable which has directory name and then could check 2 files differently too.
Following could be the code but it is based of lot of assumptions.
Code:
#!/bin/sh
deploymentXML=/home/Config
properties=/home/prop.txt
if [[ -f $deploymentXML".xml" ]]
then
 VAL=deploymentXML".xml"
fi
if [[ -f $deploymentXML".xml.bak" ]]
then
 VAL=deploymentXML".xml.bak"
fi
 
awk '{}' "$properties" $VAL

Thanks,
R. Singh

Last edited by RavinderSingh13; 05-26-2016 at 12:35 PM.. Reason: Adding comments as user changed his/her post.
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 05-26-2016
Thanks Ravinder.

I was basically looking for a way to have a variable with 2 extensions. I don't want to use if-else statements.

We are not using two files anymore, just single file.
# 6  
Old 05-26-2016
If you know that exactly one of /home/config.txt and /home/config.xml exist and that /home/config.* does not match any other filename extension, you could use:
Code:
file=$(echo /home/config.[tx][xm][tl])

without using any if statements, but:
Code:
if [ -f /home/config.txt ]
then	file=/home/config.txt
else	file=/home/config.xml
fi

or, the equivalent:
Code:
[ -f /home/config.txt ] && file=/home/config.txt || file=/home/config.xml

will be faster (considerably faster in a large directory) because no subshell environment needs to be created for the command substitution and a single stat() system call will be faster than searching an entire directory for possible matching filenames to determine which of the two names is present.
# 7  
Old 05-26-2016
Quote:
Originally Posted by ocbit
.
.
.
since only xml or xml.bak will be present in the directory.
.
.
.
then /home/config.* will do what you need.

/home/Config.{xml,xml.bak} is a construct that recent shells (like bash, which you may or may not use) will expand to /home/Config.xml /home/Config.xml.bak - exactly what * with above conditions would yield.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Rename specific file extension in directory with match to another file in bash

I have a specific set (all ending with .bam) of downloaded files in a directory /home/cmccabe/Desktop/NGS/API/2-15-2016. What I am trying to do is use a match to $2 in name to rename the downloaded files. To make things a more involved the date of the folder is unique and in the header of name... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. UNIX for Dummies Questions & Answers

File Extension Missing in file-$var.csv

I'm afraid this is a silly question but I can't figure it out. I have a script like so... echo "Enter DRDL Signature Version Number" read DRDL_Number mv signature_output.csv SERVICE_OBJECTS_S-$DRDL_Number.csv The resultant filename does not contain the .csv as follows.... (3 Replies)
Discussion started by: Cludgie
3 Replies

3. 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

4. Shell Programming and Scripting

Getting the file extension

I have a file n06-z30-sr65-rgdt0p25-varp0.25-8x6drw-test.cmod and I want to get the extension. At the moment I have set filextension = `echo $f | awk 'BEGIN {FS="."} {print $2}'` which of course does not work as there is a point in varp0.25 (13 Replies)
Discussion started by: kristinu
13 Replies

5. UNIX for Dummies Questions & Answers

creating separate directories according to file extension and keeping file in different directory as

unix program to which a directory name will be passed as parameter. This directory will contain files with various extensions. This script will create directories with the names of the extention of the files and then put the files in the corresponding folder. All files which do not have any... (2 Replies)
Discussion started by: Deekay.p
2 Replies

6. UNIX for Dummies Questions & Answers

Changing file extension

Hello all, I need to change file extension for all .doc files to .txt file in multiple folders. I know the way to rename them by going to each folder and doing that, but I need something which I can run from home directory so that It does the renaming in all the nested directories. Thanks. (4 Replies)
Discussion started by: jaysean
4 Replies

7. Shell Programming and Scripting

bad substitution Error while renaming Extension

Hi All, We are in the process of Migrating from AIX 4 to Solaris 10 and getting a Few Errors. I have been programming in shell but could never establish muself as an expert, hence please need you help. I am Getting Bad Substitution error in my script, I have isolated the issue and its... (6 Replies)
Discussion started by: paragkhanore
6 Replies

8. UNIX for Dummies Questions & Answers

About the file extension

Hi everyone, Can we know that which type of file is there without opening a file thanks in advance.... kunal patil (3 Replies)
Discussion started by: kunalpatil09
3 Replies

9. UNIX for Dummies Questions & Answers

changing file extension

Hello, everyone! :] I'm having an issue with my camera/uploading to Photobucket. When my camera transfers it's photos to my hard drive, it transfers them as .JPG. Unfortunately, when I go to upload to my Photobucket account, it only accepts .jpg & .jpeg files. Every time I want to upload, I... (6 Replies)
Discussion started by: spidydude
6 Replies

10. Shell Programming and Scripting

rename file extension

I am trying for loop to rename file extension from .txt to .html : as below : for i in *.txt; do mv "$i" `basename $i`.html; done ------------------------------------------- But this renames a file file1.txt as file1.txt.html anyone know how get avoid .html added after .txt ? it... (4 Replies)
Discussion started by: sriram003
4 Replies
Login or Register to Ask a Question