LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-10-2003, 02:55 PM   #1
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 216

Rep: Reputation: 30
Question Trying to use sed to append C opening /* and close */ to code


Hi,

First of all, I don't know much about sed other than it looks incredibly powerful and it has the ability to replace text based on a set of parameters.

I've tried researching the method needed by searching the forums here (TONS of information came up, but couldn't spot what I was looking for) and elsewhere via google.

I want to replace

Code:
#define HAVE_GMTIME_R 1
with

Code:
/* #define HAVE_GMTIME_R 1 */

I appreciate any feedback and suggestions you could give me.



Thanks!
 
Old 10-10-2003, 03:17 PM   #2
iainr
Member
 
Registered: Nov 2002
Location: England
Distribution: Ubuntu 9.04
Posts: 631

Rep: Reputation: 30
sed 's!#define HAVE_GMTIME_R 1!/* & */!g' < infile >outfile

Iain.
 
Old 10-10-2003, 03:43 PM   #3
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 216

Original Poster
Rep: Reputation: 30
Works great!

Only I figured out that I need to have that command affect two different sections of code, not one like I originally asked about.

I need to enclose each of these

Code:
#define HAVE_GMTIME_R 1
#define HAVE_LOCALTIME_R 1
in opening and closing braces so they look like this:

Code:
/* #define HAVE_GMTIME_R 1 */
/* #define HAVE_LOCALTIME_R 1 */
Thanks for your help iainr!
 
Old 10-10-2003, 04:43 PM   #4
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 216

Original Poster
Rep: Reputation: 30
Okay, here is the fix for appending open & closing comment sections for just one part, but not for both:

sed 's!#define HAVE_GMTIME_R 1!/* & */!g' < infile >outfile

What does all of this mean?

sed = program

' = opens statement?
s = substitute option?
! = ?
#define HAVE_GMTIME_R 1 = text we're appending open & closing comments to
! = ?
/* = opening section (I forget what it's called ... ) for C comment
& = ?
*/ = closing section for C comment
! = ?
g = ?
' = closes statement?
 
Old 10-10-2003, 04:53 PM   #5
iainr
Member
 
Registered: Nov 2002
Location: England
Distribution: Ubuntu 9.04
Posts: 631

Rep: Reputation: 30
sed 's!#define HAVE_.*TIME_R 1!/* & */!g' < infile >outfile

OK

We put the sed statement in single quotes '...' so it is executed by sed and not by the shell.

s = substitute (a sed subcommand - there are others such as d for delete)

! = seperator. In sed you can use anything as a separator. / is commonly used, but when the text I am trying to replace or the new text contains a / I find it easier to use something else like !. In this case, sed assumes the separator is the character immediately following the s. You could get really confusing and use a letter and have something like this.
sed 'sO#define HAVE_.*TIME_R 1O/* & */Og' < infile >outfile
but that's just nasty.

& = whatever I substituted in the first place. Very useful if you are using wildcards (as in the revised version above). For example, to comment out an entire shell script with hashes
sed 's/.*/# &/g'

g = do the replace globally - not just for the first occurance on each line (don't worry about this; 99% of the time putting in g works fine and does what you want).

Iain.
 
Old 10-10-2003, 05:02 PM   #6
deoren
Member
 
Registered: Oct 2003
Location: USA
Distribution: Ubuntu
Posts: 216

Original Poster
Rep: Reputation: 30
Awesome! Thx so much!

Thanks for taking the time to explain my questions ;-)
 
  


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
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Windows close immediatly after opening! Help! thebasilisk Linux - Distributions 1 06-04-2004 01:23 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM
A source close to SCO: "linux code has been copied in to system V" qanopus General 4 06-12-2003 01:02 AM

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

All times are GMT -5. The time now is 12:38 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