LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-13-2009, 04:13 PM   #1
sudkool
LQ Newbie
 
Registered: Jul 2004
Location: Manhattan, KS
Posts: 4

Rep: Reputation: 0
Update/Rename filename


Hi,

This is the first time I am working on Linux Scripting and have a questions from the linux gurus.

I need to create a script which will replace a predefined text in given set of files with a varible.

example: File Name: Total_Sales_COM.PDF

In my script, first I am creating a varaible, lets call it $Company_Cd, and assigning company code, e.g. 512, to that variable.

The second task I have to perform is rename the above mentioned file name, Total_Sales_COM.PDF, as Total_Sales_512.PDF.

I tried to search on different forum, but so for no luck. I will really appreicate if someone could help me with this issue.

Thank you in advance.

Last edited by sudkool; 05-13-2009 at 04:14 PM.
 
Old 05-13-2009, 04:25 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Code:
export Company_Cd=512
echo Total_Sales_COM.PDF|sed "s/COM/"$Company_Cd"/"
Total_Sales_512.PDF


Cheers,
Tink
 
Old 05-13-2009, 04:40 PM   #3
sudkool
LQ Newbie
 
Registered: Jul 2004
Location: Manhattan, KS
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Tinkster for your prompt reply. As I said in my initial post, I am totally new in Scripting world. When I try to put your snippet in my script and execute it, it display the file names in desired fashion, but does not rename the actual file names. How could I do that?

I am trying to put that in loop which will read each file and rename it by replacing the COM and the $company_cd

Thank you once again for your help.
 
Old 05-13-2009, 05:22 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
You just need to capture the output of the sed in another
variable, and then do a mv with the names ...

Code:
export Company_Cd=512
export MyFile=Total_Sales_COM.PDF
export NewFile=$( echo $MyFile|sed "s/COM/"$Company_Cd"/" )
mv "$MyFile"  "$NewFile"

Cheers,
Tink
 
Old 05-13-2009, 05:38 PM   #5
sudkool
LQ Newbie
 
Registered: Jul 2004
Location: Manhattan, KS
Posts: 4

Original Poster
Rep: Reputation: 0
Thanks Tink.
 
  


Reply


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
Change name of backup file in ext3 from filename~ to .filename~ Libu Linux - General 2 07-21-2008 09:29 PM
Convert static library (Filename.a) to dynamic shared object (filename.so) afx2029 Linux - Software 4 08-17-2007 06:07 AM
Filename rename jarod_123 Programming 4 03-21-2006 02:40 PM
change uploaded files from filename.avi to filename.avi.html like www.rapidshare.de latheesan Linux - Newbie 3 06-16-2005 04:33 AM
filename- and filename~ files? slinky2004 Linux - Newbie 5 10-17-2004 10:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:19 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration