Adding text from a variable using sed (Or awk) with punctuation

Hi All,

I would have though this would have been simple, but...

I have text in a variable that I need to insert into a bunch of other files... The text is simple:

text='-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
This content is or could contain personal information. Please ensure that you are authorized to see this data! BLA BLA BLA!!!
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+'

Then , I do a simple:

sed "/##body/a $text" account_request_WHATEVER.vm

This searches for the ##body tag in the file and adds the variable text right after it... or so I thought...

sed: -e expression #1, char 475: unknown command: `-'

For some reason, its reading the first dash (-) in the text file EVEN THOUGH I have used single "Ticks" around them.

awk does something similar:

awk: /##body/{print;print "-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
awk:                      ^ unterminated string

Any ideas!

Thanks!
Joe

Try escaping the <new line> chars when assigning the text variable:

text='-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+\
This content is or could contain personal information.\
-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+'

Please be aware that some sed versions NEED a \<new line> combination after the a command.

The awk doesn't do "something similar" - it's just seeing an unterminated string in your post.

1 Like

Hi RudiC,

This works GREAT! However, it also removes all the lines after adding the new lines...

I need to keep the lines that exist after inserting the text... For example, ther are ##endbody and some other formatting commands afterwards.

Thanks again!

---------- Post updated at 10:20 AM ---------- Previous update was at 10:09 AM ----------

There another issue that I solved using this:

sed "0,/##body/s//##body\n$text/"

there were multiple ##body tags LOL... So, I search and replaced the first option.

Thanks again for the help on this!
Joe