User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,555 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,483 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 3099 | Replies: 10
Reply
Join Date: Oct 2007
Posts: 13
Reputation: acwbrat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
acwbrat acwbrat is offline Offline
Newbie Poster

Regular Expressions using egrep or expr

  #1  
Oct 19th, 2007
How do I change my file extension from me.cpp to me.out or me.cpp to me.exe using Bash scripting.

Thanks!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,517
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Regular Expressions using egrep or expr

  #2  
Oct 19th, 2007
filename=me.cpp
# If you use ksh or bash
filename=${filename%cpp}out
# if you use sh
filename=`echo $filename | sed -e s/cpp$/out/`
Last edited by masijade : Oct 19th, 2007 at 2:36 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 13
Reputation: acwbrat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
acwbrat acwbrat is offline Offline
Newbie Poster

Re: Regular Expressions using egrep or expr

  #3  
Oct 19th, 2007
I want to use either egrep or expr only.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,517
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Regular Expressions using egrep or expr

  #4  
Oct 19th, 2007
Good for you. Experiment a bit.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 13
Reputation: acwbrat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
acwbrat acwbrat is offline Offline
Newbie Poster

Re: Regular Expressions using egrep or expr

  #5  
Oct 19th, 2007
I have tried using awk but I still don't see another way of doing the same code but using expr.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,517
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Regular Expressions using egrep or expr

  #6  
Oct 19th, 2007
Give a better description of what it is you want to do. Have you even tried those things that were posted?
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 13
Reputation: acwbrat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
acwbrat acwbrat is offline Offline
Newbie Poster

Re: Regular Expressions using egrep or expr

  #7  
Oct 19th, 2007
This is what I have from the example:

#!/bin/bash
#
# Replaces blanks with underscores in all the filenames
# of the current directory.

i=0

for filename in *
do
echo "$filename" | grep -q " "
if [ $? -eq 0 ]
then
fname=$filename
n=`echo $fname | sed -e "s/ /_/g"`
mv "$fname" "$n"
((i++1))
fi
done

echo "$i file(s), done."
exit 0
Reply With Quote  
Join Date: Oct 2007
Posts: 13
Reputation: acwbrat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
acwbrat acwbrat is offline Offline
Newbie Poster

Re: Regular Expressions using egrep or expr

  #8  
Oct 19th, 2007
Yes I did use awk. From one of my reference books that don't give solutions, it said to try using expr or egrep for string manipulation.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,517
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is online now Online
Posting Virtuoso

Re: Regular Expressions using egrep or expr

  #9  
Oct 19th, 2007
Well, it's bash, so as I said in my first post
for filename in *.cpp
do
  mv "${filename}" "${filename%cpp}out"
done

However, .cpp to .out usually means you want to compile the files, which has nothing to do with simply renaming them like this.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 13
Reputation: acwbrat is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
acwbrat acwbrat is offline Offline
Newbie Poster

Re: Regular Expressions using egrep or expr

  #10  
Oct 19th, 2007
I understand the last post. However, I want to perform the same code but using expr in the code.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 5:30 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC