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.

Introduction to Linux - A Hands on Guide

This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.

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.

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.

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.

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.