LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-10-2007, 11:49 AM   #1
nesta
Member
 
Registered: Aug 2006
Posts: 100

Rep: Reputation: 15
replacing .c by .o


hi all,

i have a variable var where

Code:
 var=mycfile.c
and i want to change it to be

Code:
mycfile.o
i tried the following command:

Code:
echo $var | sed 's/.c/.o/'
but i got the following result:

Code:
m.ofile.o
i.e whenever it finds c letter it works and absolutely it overrides the character before the c character or the character after it
 
Old 02-10-2007, 11:53 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Try:
Code:
sed 's/\.c/.o/'
"." means any character in regular expressions, so you need to escape it.
 
Old 02-10-2007, 12:18 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Another solution could be
Code:
echo $var | sed 's/c$/o/'
The $ sign match only the character at the end of the string.
 
Old 02-10-2007, 12:20 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by jlliagre
Try:
Code:
sed 's/\.c/.o/'
Or even better,
Code:
sed 's/\.c$/.o/'
Where the dollar sign indicates the end-of-line (in case your filename is something like “mycfile.is.cool.c”).
 
Old 02-11-2007, 06:33 PM   #5
jr1
LQ Newbie
 
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14

Rep: Reputation: 0
If you're using bash, this can easily be accomplished without sed.
Code:
echo "${var/%.c/.o}"
You can check this page of the bash reference manual if that doesn't make sense to you.

(Edit: This post originally had "${var/%.c$/.o}" instead of "${var/%.c/.o}")
.

Last edited by jr1; 02-12-2007 at 01:16 AM.
 
Old 02-11-2007, 07:10 PM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
..or also using bash like this (the ${var%pattern} expression returns the value of var with the shortest suffix matching pattern removed):
Code:
cfile=myfile.c
ofile=${cfile%.c}.o
If you're tying to write a script to build a program, you'd be better off using make. That's what it's for.
 
Old 02-11-2007, 07:32 PM   #7
jr1
LQ Newbie
 
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by matthewg42
..or also using bash like this (the ${var%pattern} expression returns the value of var with the shortest suffix matching pattern removed):
Code:
cfile=myfile.c
ofile=${cfile%.c}.o
The only problem with that is that it will append ".o" to strings that don't end ".c". My earlier example won't alter strings unless they end in ".c".
 
Old 02-11-2007, 08:05 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Quote:
Originally Posted by jr1
The only problem with that is that it will append ".o" to strings that don't end ".c". My earlier example won't alter strings unless they end in ".c".
Ah yes, very true - could be important or irrelevant depending on how he's getting the original value.
 
Old 02-11-2007, 08:52 PM   #9
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Code:
# That doesn't work for me in Bash2? ;-p
var=mycfile.c
echo "${var/%.c$/.o}"
mycfile.c

# This does
echo "${var/%.c/.o}"
mycfile.o
 
Old 02-12-2007, 01:10 AM   #10
jr1
LQ Newbie
 
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14

Rep: Reputation: 0
Quote:
Originally Posted by unSpawn
Code:
# That doesn't work for me in Bash2? ;-p
var=mycfile.c
echo "${var/%.c$/.o}"
mycfile.c

# This does
echo "${var/%.c/.o}"
mycfile.o
Doh. I must've had sed on my mind. The $ was a typo and, of course, it has no special meaning in bash's shell parameter expansion (it was looking for ".c$" at the end of the string).

I'll correct the original post.
 
  


Reply



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
replacing firefox 1.5.0.7 radiodee1 Debian 10 01-30-2007 01:40 PM
Replacing bootsplash smoaky Linux - Newbie 26 01-29-2006 01:53 PM
replacing one distro with another joshknape Linux - Newbie 1 09-04-2005 10:29 PM
Replacing Novell 4.11 teeno Linux - General 3 06-29-2005 09:43 AM
replacing Zackdude Linux - General 1 09-08-2003 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:05 PM.

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
Open Source Consulting | Domain Registration