separating filename and extension


Login or Register to Reply

 
Thread Tools Search this Thread
# 1  
separating filename and extension

Hi (warning: newbie question),
I am writing a script to run a series of tests on a program, which involves a line:

Code:
for file in `ls test_suite/*.args`

but later I want to send the output to file.out. But I need to separate the filename and extension somehow...Also $file contains "test_suite/" before each entry as well, how do I get rid of that (I assume it's in a similar way as the separation above)

Thanks!
# 2  
HI,
You can get the filename alone excluding the directory using following command
for file in `ls test_suite/*.args`
do
F_NAME=$(basename ${file})
done

Thanks
Raghu
This User Gave Thanks to Raghuram.P For This Post:
# 3  
Code:
for f in test_suite/*.args;do 
	fn="${f%.*}" 
	printf "filename without path and extension: %s, extension only: %s\n" "${fn##*/}" "${f##*.}"  
done

With bash you can do something like this (if I understand correctly the requirement):
Code:
set test_suite/*.args
set "${@##*/}"
printf "%s\n" "${@%.*}">file.out


Last edited by radoulov; 04-06-2007 at 07:40 AM..
Login or Register to Reply

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
[BASH] Getting a filename its extension
sea
Heyas As i often have decide things upon a filename its extension, i thought i'd write a script: Just wondering if there would be a more efficent way? out="" FN=$( echo "$1" | sed s," ","",g) # Remove any spaces and make it a single string for chance in $(echo "$FN"|sed s,"\."," ",g) # Use...... Shell Programming and Scripting
7
Shell Programming and Scripting
Get latest filename without extension
sumang24
I need to write a shell script to display the output of ls command like this ls -ltr *txt I get this -rw-r----- 1 oracle dba 51912704 Dec 11 10:27 /usr/local/sam/test12112012101247AM.txt -rw-r--r-- 1 oracle dba 7 Dec 11 11:58 /usr/local/sam/test.txt but I just need the latest...... Shell Programming and Scripting
7
Shell Programming and Scripting
removing the filename extension
daflore
Is there an easy way to strip off a filename's extension? For example, here's a filename: blahblahblah.thisisok.thisisnotok I want to get rid of .thisisnotok from the filename, so that what's left is blahblahblah.thisisok Thanks. I have a directory full of filenames that need to be...... Shell Programming and Scripting
5
Shell Programming and Scripting
cut filename extension
Essbaumer
I need a small script (sh) to remove in a variable the filename extension. Example: f = "testfile.txt" and I need a $a with "testfile". Some one a idea?... Shell Programming and Scripting
4
Shell Programming and Scripting
changing filename extension
Hiso
Hi All, i need to change the filename extension. For simplicity, we can assume that the extension after '.' is 3 characters only... but the filenames can vary. eg. changing from abc.doc to abc.dxs can i have a oneline command to achieve this... Shell Programming and Scripting
3
Shell Programming and Scripting