LinuxQuestions.org
Visit Jeremy's Blog.
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 06-29-2012, 11:49 AM   #1
pomico
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Rep: Reputation: Disabled
Bash Shell Scripting rename files


Hi, I'm trying to write a script that reads in files with a .dat extension, runs them through a program I've already written, and outputs them with a .txt extension.

So far I have:
for file in *dat; do
echo "Reddening $file"
./redden.out < $file > $file.txt
done

which of course leaves me with files appended with .dat.txt.
How can I either get the script to output .txt extensions in the first place, or rename .dat.txt to just .txt?

I have found examples using basename but I'm not sure how to get that to work in this instance.

Since I am completely new to bash, could you please also explain in answers what the terms you use actually do? That's another problem I'm having by looking at similar questions - I don't know what things actually mean so can't figure out how to apply them to my problem.

Thanks a lot!
 
Old 06-29-2012, 12:08 PM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Ubuntu, PopOS, Raspbian
Posts: 1,899
Blog Entries: 36

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
man bash

See the section on Parameter Expansion.

Code:
file=somefile.dat
echo ${file/.dat/.txt}
 
1 members found this post helpful.
Old 06-29-2012, 12:16 PM   #3
ShadowCat8
Member
 
Registered: Nov 2004
Location: San Bernardino, CA
Distribution: Gentoo, Arch, (RedHat4.x-9.x, FedoraCore 1.x-4.x, Debian Potato-Sarge, LFS 6.0, etc.)
Posts: 261

Rep: Reputation: 52
Greetings,

Well, there's a couple ways you can do this... First that comes to mind is as follows:
Code:
#!/bin/bash
#
#

# We should be launching this from the directory the files are in.
# 
for files in `ls -1 *dat`
     do
     echo "Reddening ${files}"
     filename="`echo ${files} | cut -d. -f1`"
     redden.out < ${filename}.dat > ${filename}.txt
     done

#
## EOF
Only real difference is I am grabbing the name of the file, without the extension, in filename using the 'cut' command, using the period ('.') as a delimiter and grabbing the first field. Then use that for the name for both the input to and output from your other program.

Last edited by ShadowCat8; 06-29-2012 at 12:28 PM. Reason: Forgot he's putting it through another program.
 
1 members found this post helpful.
Old 06-29-2012, 08:59 PM   #4
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
Code:
do
  if [ -f "$filename" ]
    then

	 CAPTION=${filename%.*}
Then write it out to "$CAPTION.txt"
 
Old 06-29-2012, 09:01 PM   #5
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
Let me know what grade your teacher gives me.
 
Old 06-30-2012, 10:02 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by ShadowCat8 View Post
for files in `ls -1 *dat`
Don't parse ls. The OP had for file in *dat which was perfectly fine (although you may want *.dat).

Quote:
Originally Posted by pomico
I have found examples using basename but I'm not sure how to get that to work in this instance.
basename is for removing directories from a path, not file extensions.
 
1 members found this post helpful.
Old 07-02-2012, 04:05 AM   #7
pomico
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Dafydd View Post
Let me know what grade your teacher gives me.
Sorry, was that directed at me?
I'm not taking a class, I am a postgraduate physics student and have been running a series of jobs on the group's supercomputer. I have a couple of programs to run on the data I get out at the end that I have written myself and since there are a number of different data files I thought I would use the opportunity to learn some scripting.
I really do appreciate any help and understand that people use these forums for assignments, but I would appreciate it if you did not jump to conclusions when people such as myself come with genuine beginners problems.

Thanks to everyone for your suggestions, I'll give them a go.
 
Old 07-02-2012, 04:42 AM   #8
pomico
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by ShadowCat8 View Post
Greetings,

Well, there's a couple ways you can do this... First that comes to mind is as follows:
Code:
#!/bin/bash
#
#

# We should be launching this from the directory the files are in.
# 
for files in `ls -1 *dat`
     do
     echo "Reddening ${files}"
     filename="`echo ${files} | cut -d. -f1`"
     redden.out < ${filename}.dat > ${filename}.txt
     done

#
## EOF
Only real difference is I am grabbing the name of the file, without the extension, in filename using the 'cut' command, using the period ('.') as a delimiter and grabbing the first field. Then use that for the name for both the input to and output from your other program.
I just tried this, exactly as above, but I get a couple of errors, saying "cut: [-cf] list: illegal list value" followed by " .dat: No such file or directory". From the latter presumably the first field isn't being grabbed?
 
Old 07-02-2012, 04:46 AM   #9
pomico
LQ Newbie
 
Registered: Jun 2012
Posts: 16

Original Poster
Rep: Reputation: Disabled
Edit: All working now, the problem was down to me (as I expected it would be...)

Thanks again!

Last edited by pomico; 07-02-2012 at 05:00 AM. Reason: Original post turned out to be a load of rubbish
 
Old 07-02-2012, 01:32 PM   #10
Dafydd
Member
 
Registered: Oct 2008
Posts: 344

Rep: Reputation: 29
Quote:
Originally Posted by pomico View Post
Sorry, was that directed at me?
I owe you an apology. The fact that this was a first post plus the nature of you question, looked very much like a lot of others who were wanting us to do their home work.

I am sorry.
Dave
 
  


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
Rename multiple files in Bash damianpfister Programming 5 11-15-2009 08:30 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 08:00 AM
rename files using bash? ferradura Linux - General 4 09-14-2007 03:35 AM
rename several files in bash or python xeon123 Linux - Newbie 2 04-02-2007 09:08 AM
Bash shell scripting using if else nautz Linux - General 10 11-07-2006 09:47 AM

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

All times are GMT -5. The time now is 11:09 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