LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-19-2007, 06:44 AM   #16
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116

Additionally, don't .cpp files usually get compiled into .out files? You sure you weren't asked to use grep to find this out?
 
Old 10-19-2007, 08:59 AM   #17
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729Reputation: 729
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
And I told you that grep and expr are not used for changing things!!!

Again, what problem are you trying to solve? ie WHY do you want to do something the hard way? Someone suggested homework......
 
Old 10-19-2007, 10:41 AM   #18
makyo
Member
 
Registered: Aug 2006
Location: Saint Paul, MN, USA
Distribution: {Free,Open}BSD, CentOS, Debian, Fedora, Solaris, SuSE
Posts: 719

Rep: Reputation: 72
Hi.

This is really meta-help: I think what he is saying that he is using (or wants to use) egrep, etc., to help him do the transformation of the name from the old to the new form, not to do the actual renaming.

In any event, we can all think of ways to essentially rename a file without using mv: we could use any of a number of commands to transfer data to a new file name, then use the new name -- cp, cat, etc. -- all could be pressed into service for that.

I think at this point, however, it would be useful for the OP to explain a bit more about his motives ... cheers, makyo
 
Old 10-19-2007, 12:08 PM   #19
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Code:
rename cpp out me.cpp
ta0kira
 
Old 10-19-2007, 12:23 PM   #20
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Thanks Ta0kira, I didn't know that until today.
PAix
 
Old 10-19-2007, 05:18 PM   #21
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Smile

This is not a homework question. This is a question that I have been trying to figure out from other textbooks! I am just trying to figure things out. That it all.
 
Old 10-19-2007, 05:21 PM   #22
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Why everybody assuming it's a homework question. If it was a homework question, I would post the whole problem. I'm asking for a specific thing. The reference books that I have does not explain text substitution/manipulation. I want to incorporate expr or egrep in the code. I know that I can use rename and mv but that is not what I'm asking. I want to change a file me.cpp to me.out using expr or egrep to find the file and changing it.
 
Old 10-19-2007, 06:23 PM   #23
acwbrat
LQ Newbie
 
Registered: Oct 2007
Posts: 22

Original Poster
Rep: Reputation: 15
Here is my code and I am still having problems getting it to work.


#!/bin/bash
#
# Replaces .cpp with .out
# of the current directory.

i=0

for filename in *
do
if [ `expr index "$filename" ".cpp "` -ne 0 ]
then
fname=$filename
n=${fname// /.out}
mv "$fname" "$n"
((i++))
fi
done

echo "$i file(s), done."
exit 0
 
Old 10-19-2007, 08:34 PM   #24
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by acwbrat View Post
Here is my code and I am still having problems getting it to work.


#!/bin/bash
#
# Replaces .cpp with .out
# of the current directory.

i=0

for filename in *
do
if [ `expr index "$filename" ".cpp "` -ne 0 ]
then
fname=$filename
n=${fname// /.out}
mv "$fname" "$n"
((i++))
fi
done

echo "$i file(s), done."
exit 0
Code:
i=$(($i+1))
 
Old 10-19-2007, 09:01 PM   #25
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
Code:
rename \.cpp \.out *\.cpp       # it will affect every file that matches in the directory.
Three arguments to the rename command, the first is the pattern to change
second it the pattern that it is to change to
third is the filename to change.

I have escaped the dot, knowing that your interest seems to be in three character filename extensions characterised by their dot. The inclusion of the dot and the escaping of it should save any accidents in case the pattern cpp should appear in the filename other than after the dot.

I suggest that you practice your skills on individual commands and mechanisms, quietly getting an idea for how they work and explore them. But when you want to perform a useful function, I think you are obliged to respect the fact that most people don't set out to create Heath Robinson code, but to do the job cleanly, efficiently and with as few keystrokes as possible so as not to spill our beer/coke/coffee/tea into the keyboard.

The advice that the Gurus give you here probably reflects best practice so you should heed it and not try to produce laborious code to inefficiently do things that can be done with half a dozen keystrokes. Get into the habit of doing things as efficiently as you are able. Your mechanisms are your own personal programming toys and like personal habits are probably best kept private unless you need help because something has come off in your hand.

 
Old 10-19-2007, 11:19 PM   #26
angrybanana
Member
 
Registered: Oct 2003
Distribution: Archlinux
Posts: 147

Rep: Reputation: 21
Quote:
Originally Posted by acwbrat View Post
Here is my code and I am still having problems getting it to work.
...
mv "$fname" "$n"
...
Ok I'm officially confused on what you want to do. Your code uses the 'mv' command. I think we've given you every (or almost every) sane answer there is. egrep and expr do not rename files. That's not the function of those programs (as far as I know). My advice is to just not dwel on this too much, since it seems like a hypothetical situation (from a textbook). Even if you figure out some weird way to pull this off with egrep and expr, you'll never do it that way again (at least I would hope).
 
  


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 08:21 AM.

Main Menu
Advertisement
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