ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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 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.
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"
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!
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.
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 12:44 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?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.