LinuxQuestions.org
Register a domain and help support LQ
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 10-18-2007, 07:57 PM   #1
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Rep: Reputation: 15
Angry Changing executable file extension


I am trying to change the file extension me.cpp to me.out. I know that I could use egrep and expr. However, when I have tried to search for codes for the following, I was out of luck.

Last edited by acwbrat; 10-19-2007 at 12:50 AM.
 
Old 10-18-2007, 09:51 PM   #2
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
I suspect that you aren't going to have a mass of files, so just try
Code:
mv me.cpp me.out
No need to make it any more complicated than it needs to be.
 
Old 10-18-2007, 09:55 PM   #3
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
I have tried the mv command. However, I wanted to steer away from using that and just want to try egrep or expr commands.
 
Old 10-18-2007, 10:07 PM   #4
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Here is an example of grep. This file changed the file name but not the extension:

files=$(ls -1 | grep *.mp3)

for x in $files
do
mv $x band_song$x
done


I am looking for the complete opposite. I am looking for the file extension of .cpp changed to .out (or .exe)
 
Old 10-18-2007, 10:13 PM   #5
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Did you make that apparent in your request for assistance? Scope creep doesn't refer to an astronomer friend.

I suddenly realised why most of these guys answer questions and don't ask them. Not that they are exceedingly smart, but they just aren't afraid. . . To pick up a book or Google for the documentation. Lord knows there is oodles of it. Speak nicely and we'll arrange to spend your salary for you too.

The extension is three characters long, it's separated from the basename by a dot. Awk will happily split the file name and extension using the dot as a delimiter and you can tack on the dot and the new extension. Smashing. Either that or you can use substr to copy all of the filename less the last three characters and do much the same sort of thing.

Oh, yes, then you can take the original filename and the newly constructed one and . . .

Use the mv command on it., much as I suggested.

The difference between the AWK and the Dodo? The Dodo died out because no one could be bothered to read the documentation.
 
Old 10-18-2007, 10:17 PM   #6
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
That's interesting. I am going to try awk. But I would rather use egrep or expr. I know that mv is the most common form but I know there's other ways of doing this. I want to be as simple as possible.
 
Old 10-18-2007, 11:28 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,311

Rep: Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040Reputation: 2040
Actually, 'mv' IS the simplest ....
 
Old 10-18-2007, 11:51 PM   #8
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
AGH!!!

I have used the mv command. Great code. However, I want to do the samething but using egrep or expr to change the file extension me.cpp to me.out or me.exe
 
Old 10-19-2007, 12:33 AM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by acwbrat View Post
AGH!!!

I have used the mv command. Great code. However, I want to do the samething but using egrep or expr to change the file extension me.cpp to me.out or me.exe
I am not following every nuance of this and your other posts, but I am afraid that you are flailing. I see you simply repeating things with no apparent understanding of what people are saying to you.

For substance, I will tell you that "grep" is not used for changing things. "expr" is for evaluating expressions. Hard to see how either one will be useful here.

Whatever you do, you will need a command that changes file names. Try "man -k file" to start getting a list of those commands.

It will also help to tell us your end goals---ie what problem are you trying to solve.
 
Old 10-19-2007, 12:50 AM   #10
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
I understand that I must use the mv command to change the file extension. But I am asking about egrep and expr to change me.cpp to me.out . I do not want to use the mv command. The man command haven't given me any valuable information to assist me with coding. Thanks pixellany
 
Old 10-19-2007, 12:53 AM   #11
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Here is a selection of code that aught to keep you happy, but I doubt it will.
BEWARE that in the third method, which doesn't use awk, that colons and braces are prevalent and may not be obviousl if you are typing the code instead of cutting and pasting.

My personal choice is awk. I wrote several lines more than you see here as I like to prove my code as I go, which is why I understand exactly what is going on, because I built the functionality around the awk after I wrote the awk. Please read the brackets from the inside out and try to understand exactly what it is that is happening.

There is a lot of learning that can be taken from the code if you take your time to analyse it and put it to good use in the future. There is no point in trying to get grep to do something that it isn't needed to do. Why did you do
Code:
"ls -1" and then grep for ".mp3", when "ls -1 *\.mp3"
would do the job just fine. Why did I escape the dot just then when it normally works without having to?
What does "*." actually refer to and was it what you first thought? Get into good habits, use the documentation and try for a bit of self help. keep your powder dry for the difficult bits and sort out your questions off-line so that they are joined up and consise when you come to present them to the gurus who you expect to help you. Show that you are putting in as much effort as they are. Knowledge doesn't come overnight, don't test whole scripts, test the individual components. Good Luck. You can change the cp to mv in the code below but I'm sure you can work out that the file will only move once. Remove or fully comment out the routines you think you don't need to do the job. When you have it done, come back and check the code to see how it works. Sorry about the diatribe, but that's the cost you pay me for writing the code.
Unix has no concept of file extensions. Filenames are just stirngs. Linux seems to allow their use to make Windows users happy bunnies. You aren't changing the file extension, you are changing the filename, which as I said is just a special string. Just like everything else appears to be a file.

Code:
#!/bin/sh

# iTL Oct 2007

originalextent="mp3"

myextn1="3pm"
myextn2="cpp"
myextn3="out"

for myfile in $(ls -1 *\.$originalextent)
do
    cp $myfile $(
                     awk -v myawkfile=$myfile -v myextn=$myextn1 ' BEGIN { split(myawkfile, myarray, ".");

                               print (myarray[1] "." myextn) }'
                )

    cp $myfile $(
                     awk -v myawkfile=$myfile -v myextn=$myextn2 ' BEGIN {
                               print (substr(myawkfile, 1, length(myawkfile) -3) myextn) }'

                )

    tmp=$(expr $(expr length $myfile) - 3)
    cp $myfile ${myfile:0:$tmp}$myextn3

done

# This will take files with the original extension and copy them
# to the three declared extentions in the heading.
# The original files can then be deleted with an appropriate line of code.
Thanks Pixellany, chrism01 for your insightful input. God it's like working with a real end-user. They don't know what they want either half the time, I must have become inured to it all without realising it.

PAix

Give a man a loaf and he will eat it and be hungry tomorrow. Give him flour and he will ask you for fire. Give him a big fishing pole and he will make it into an aerial for his transmitter and ask the whole world to feed him - call me cynical!

Last edited by PAix; 10-19-2007 at 12:56 AM.
 
Old 10-19-2007, 01:05 AM   #12
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by acwbrat View Post
I understand that I must use the mv command to change the file extension. But I am asking about egrep and expr to change me.cpp to me.out . I do not want to use the mv command. The man command haven't given me any valuable information to assist me with coding. Thanks pixellany
If you're trying to do text substitution, I'm not sure egrep or expr can help you out. sed on the other hand might be useful.
Code:
$ sed 's/\(.*\)\.cpp/\1.out/' <<<"me.cpp"
me.out
Don't think this is possible with egrep and expr. You could, use egrp or expr to get the name, and then add an "out" to the end.

Code:
$ val=$(expr "me.cpp" : "\(.*\.\)cpp")out
$ echo $val
me.out
 
Old 10-19-2007, 01:39 AM   #13
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
I think the question is very clear. From the commands that acwbrat gave, I assume that he wants something in the shell.

However, I don't think that there's a way to avoid the mv command and I don't see where egrep can come in (in a usefull way).
Code:
ext=".ext"
pos=`expr index $1 "."`
pos=$(($pos-1))
sub=`expr substr $1 1 $pos`
echo in = $1
echo pos = $pos
echo sub = $sub
newname="$sub$ext"
echo newname=$newname
mv $1 newname
This script takes a filename and replaces anything after the first dot by what is defined in ext and renames it. It needs some polishing (it should take the last dot instead of the first one), error checking etc.

I'm not sure if this is what you're looking for, but I enjoyed learning a bit of bash scripting.

Edit: took me 50 minutes to figure this script out and in the meantime others posted better ideas.

Last edited by Wim Sturkenboom; 10-19-2007 at 01:44 AM.
 
Old 10-19-2007, 01:39 AM   #14
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Angrybanana,

Thanks a lot! However, I did the text substitution: $ sed 's/\(.*\)\.cpp/\1.out/' <<<"me.cpp"
me.out

When trying to see if the file changed in my directory, the information presented as me.out
 
Old 10-19-2007, 04:17 AM   #15
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,771

Rep: Reputation: 477Reputation: 477Reputation: 477Reputation: 477Reputation: 477
This exercise sounds like homework to me. Why else would the OP insist on using specific commands, especially when they aren't really suited to the job?

Last edited by gnashley; 10-19-2007 at 04:20 AM.
 
  


Reply

Tags
egrep, mv


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 05:19 PM
Changing extension of multiple files harisund Programming 17 06-01-2006 11:59 AM
Extension for executable file jojojo Linux - Newbie 10 05-29-2006 03:34 PM
Changing the default open action for an executable file nitrambass LinuxQuestions.org Member Intro 1 06-22-2004 04:09 PM
Changing NTFS files from executable Odd_Bloke Linux - Newbie 1 08-29-2003 09:24 PM


All times are GMT -5. The time now is 01:13 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration