![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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. |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
use this
Code:
a=myfile.csv
b=${a%.*}.doc
echo $b
|
|
#3
|
|||
|
|||
|
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. |
|
#4
|
||||
|
||||
|
Quote:
Code:
echo $filename | sed -e "s/\.txt/\.doc/" -e "s/\.csv/\.doc/" |
|
#5
|
|||
|
|||
|
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. |
|
#6
|
|||
|
|||
|
Quote:
could you please post an example where do u find this controversy ? |
|
#7
|
|||
|
|||
|
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. |
|||
| Google The UNIX and Linux Forums |
| Tags |
| regex, regular expressions |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|