LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Changing executable file extension (https://www.linuxquestions.org/questions/programming-9/changing-executable-file-extension-592875/)

acwbrat 10-18-2007 06:57 PM

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.

PAix 10-18-2007 08:51 PM

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.

acwbrat 10-18-2007 08:55 PM

I have tried the mv command. However, I wanted to steer away from using that and just want to try egrep or expr commands.

acwbrat 10-18-2007 09:07 PM

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)

PAix 10-18-2007 09:13 PM

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.

acwbrat 10-18-2007 09:17 PM

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.

chrism01 10-18-2007 10:28 PM

Actually, 'mv' IS the simplest ....

acwbrat 10-18-2007 10:51 PM

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

pixellany 10-18-2007 11:33 PM

Quote:

Originally Posted by acwbrat (Post 2929220)
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.

acwbrat 10-18-2007 11:50 PM

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

PAix 10-18-2007 11:53 PM

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!

angrybanana 10-19-2007 12:05 AM

Quote:

Originally Posted by acwbrat (Post 2929266)
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


Wim Sturkenboom 10-19-2007 12:39 AM

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.

acwbrat 10-19-2007 12:39 AM

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

gnashley 10-19-2007 03:17 AM

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?


All times are GMT -5. The time now is 08:29 AM.