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
in the directory then that's what I want to use, but if
is in the directory then use that. The point being,
is always constant. Is this possible?
To traverse through all the .txt and .xml files, then following may help you in same.
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.
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
or
. (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.
Last edited by ocbit; 05-26-2016 at 12:15 PM..
Reason: added tags
Thanks Ravinder, but I don't wish to traverse any files.
I will only have
OR
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
OR
.
How can I assign a shell variable to accept something like
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.
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.
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:
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:
without using any if statements, but:
or, the equivalent:
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.
.
.
.
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)