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.
|
I suspect that you aren't going to have a mass of files, so just try
Code:
mv me.cpp me.out |
I have tried the mv command. However, I wanted to steer away from using that and just want to try egrep or expr commands.
|
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) |
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. |
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.
|
Actually, 'mv' IS the simplest ....
|
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 |
Quote:
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. |
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
|
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" 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 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! |
Quote:
Code:
$ sed 's/\(.*\)\.cpp/\1.out/' <<<"me.cpp" Code:
$ val=$(expr "me.cpp" : "\(.*\.\)cpp")out |
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" 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. |
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 |
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. |