2

Not GNU sed. I have a collection of makefiles I'm trying to uniformly modify. In each makefile, I have a comment that tells me where to put the code.

sed "/MULTI WORD COMMENT/a\
LINE 1\
LINE 2 $stuff $morestuff" "$file" >> "$file"_NEW

I want to find the comment, then append multiple lines of text after it. Some lines contain shell variables I want to expand. All I could figure out is that I need to use " when I want to do shell expansions and that each line to be appended should get its own line.

I get an error telling me to terminate the "a" command with "\", but it looks like I'm already doing that. What am I doing wrong?

Example input

# MULTI WORD COMMENT
.SUFFIXES: .o .c

Example output

# MULTI WORD COMMENT
LINE 1
LINE 2 ASDF GASIFJ
.SUFFIXES: .o .c

In this case, stuff=ASDF and morestuff=GASIFJ. These are not entirely unlike the variable names in the makefiles I'm working with.

Community
  • 1
  • 1

1 Answers1

2

Since you're using double quotes you need to escape twice, so use:

sed "/MULTI LINE COMMENT/a\\
LINE 1\\
LINE 2 $stuff $morestuff" "$file" >> "$file"_NEW

PS: Your example input has MULTI LINE COMMENT but your command has MULTI WORD COMMENT

anubhava
  • 664,788
  • 59
  • 469
  • 547
  • Note that if either `$stuff` or `$morestuff` itself contains newlines, there are still problems. The OP might then do better to put the extra information into a file and use `r` to read the extra information from its file at the correct point in the main input file. – Jonathan Leffler Jul 22 '15 at 20:24
  • Yes I agree with that. `$stuff` and `$morestuff` should be simple strings without newlines here. – anubhava Jul 22 '15 at 20:27
  • 1
    Fixed comment example. Luckily, those variables are just names of modules, so no newlines in them. Script is working now, thank you. – lkjdsa asodiuf Jul 22 '15 at 20:30