The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 05-30-2006
Registered User
 

Join Date: May 2006
Posts: 32
sed question

Hi

I am trying to replace an extension in a file name using sed as follows:

echo $filename | sed 's/.txt/.doc/'

My objective is to replace any extension with let's say a .doc extension. Right now, my input may have two extensions; .txt and .csv. I have to replace both with a .doc extension. If I use the following code, it does not seem to work. Any idea what might be the probable cause?

echo $filename | sed 's/.txt|.csv/.doc'

Does the above usage of regular expression not work with sed or am i missing some quotes somewhere?

Any pointers would be appreciated.

Thanks
Vikas.
Reply With Quote
Forum Sponsor
  #2  
Old 05-30-2006
Playing with Ubuntu Now!
 

Join Date: Oct 2005
Location: Chennai
Posts: 364
use this

Code:
a=myfile.csv
b=${a%.*}.doc
echo $b  
Variable 'a' can contain any extension.
Reply With Quote
  #3  
Old 05-31-2006
Registered User
 

Join Date: May 2006
Posts: 32
isn't it feasible thru sed?

Thanks for ur reply. However, i am still interested to know whether it is possible thru sed or not. The reason is, if it contains anything else than a .txt or .csv, i dont want to convert it into .doc.

So, as of now, i just want to convert a .txt or a .csv into .doc, otherwise i want to raise an error.

TIA
Vikas.
Reply With Quote
  #4  
Old 05-31-2006
vino's Avatar
Supporter (in vino veritas)
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,698
Quote:
Originally Posted by Vikas Sood
echo $filename | sed 's/.txt|.csv/.doc'
The above becomes

Code:
echo $filename | sed -e "s/\.txt/\.doc/" -e "s/\.csv/\.doc/"
Mind you, the above will not change the actual file name. You will have to capture the modified name and then do a rename on the file.
Reply With Quote
  #5  
Old 05-31-2006
Registered User
 

Join Date: May 2006
Posts: 32
Thanks

Thanks Vino. It worked for me. Is it right to assume that the regular expression '|' does not work with sed and probably it is just meant to work with awk or grep?

Anyways, thanks again. It worked just fine.
Reply With Quote
  #6  
Old 05-31-2006
Technorati Master
 

Join Date: Mar 2005
Location: Large scale systems...
Posts: 2,610
Quote:
Originally Posted by Vikas Sood
Thanks Vino. It worked for me. Is it right to assume that the regular expression '|' does not work with sed and probably it is just meant to work with awk or grep?

Anyways, thanks again. It worked just fine.
No, its not so...

could you please post an example where do u find this controversy ?
Reply With Quote
  #7  
Old 05-31-2006
Registered User
 

Join Date: May 2006
Posts: 32
Example

Hi

I started the thread with the example. To make it clearer, below is the real life example :

I receive a file with a timestamp appended to it from source system. If it is a compressed file using gzip utility, source (a mainframe system) could send it in all the following possible formats:

filename.gz.timestamp
filename.GZ.timestamp
filename.Gz.timestamp
filename.gZ.timesstamp

I have to replace .gz., .Gz., .gZ., .GZ. to a single dot to make the above names as filename.timestamp. Once i am done with this, I will append a .gz suffix at the end. So at the end of this operation, i will have following gzip file

filename.timestamp.gz

I can now go ahead and unzip it. I was trying to use the following form of sed earlier using regular expression but it did not work for me..

echo $filename|sed 's/.gz.|.Gz.|.gZ.|.GZ././'

With what Vino suggested later on, i could resolve the issue but was just wondering why I could not use the above form of regular expression with sed.

Hope that makes it clearer.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
regex, regular expressions

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0