LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to use sed to append C opening /* and close */ to code (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-use-sed-to-append-c-opening-%2A-and-close-%2A-to-code-102546/)

deoren 10-10-2003 02:55 PM

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!

iainr 10-10-2003 03:17 PM

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

Iain.

deoren 10-10-2003 03:43 PM

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!

deoren 10-10-2003 04:43 PM

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?

iainr 10-10-2003 04:53 PM

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.

deoren 10-10-2003 05:02 PM

Awesome! Thx so much!

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


All times are GMT -5. The time now is 01:18 PM.