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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Adding text from a variable using sed (Or awk) with punctuation
# 1  
Old 04-17-2017
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:

Code:
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:

Code:
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...

Code:
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:

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

Any ideas!

Thanks!
Joe
# 2  
Old 04-17-2017
Try escaping the <new line> chars when assigning the text variable:

Code:
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.
This User Gave Thanks to RudiC For This Post:
# 3  
Old 04-17-2017
Quote:
Originally Posted by RudiC
Try escaping the <new line> chars when assigning the text variable:

Code:
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.

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:

Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing punctuation marks with the help of sed

#!/bin/bash a=(*.pdf) punct=((~`!@#$%^&*()_-+=|\{};':",./<>?)) for (( i =0; i < ${#a}; i++ )) do sed -ri 's/$punct//g' ${a} done I cannot use the above code, can you help me in removing all punctuation marks from file name except file extension. The idea is that once all... (9 Replies)
Discussion started by: ambijat
9 Replies

2. UNIX for Dummies Questions & Answers

[Help] Adding text to a variable line in a file

Hey guys, I need to write a script that will add a specific text at the end of a specific line (of a text file). but the line is a variable this is my text file : device_2 ansible_ssh_host=127.0.0.1 ansible_ssh_port=30000 ansible_ssh_user='root' device_2 ansible_ssh_host=127.0.0.1... (1 Reply)
Discussion started by: OdedOvdat
1 Replies

3. Shell Programming and Scripting

Adding more text to a variable

Is there a more simple way to add text to a variable I havevar="blue" and like to add green to getecho $var blue green Here is how I do it. var=$(echo "${var} green") ---------- Post updated at 13:23 ---------- Previous update was at 13:12 ---------- :o Uff Not always easy to see the... (2 Replies)
Discussion started by: Jotne
2 Replies

4. Shell Programming and Scripting

adding a number with sed or awk.

Hi.. I have this delicate problem..:wall: I have this huge ldif file with entry's like this example below.. And I need to change the following entrys. telephoneNumber: emNotifNumber: billingnumber= BillingNumber: Al these entrys has a number like 012345678 and it needs to add one more... (15 Replies)
Discussion started by: pelama
15 Replies

5. Shell Programming and Scripting

adding text with sed

sed 's/<\/body>/<A HREF='"$index"'>'"$description"'<\/A>\ <\/body>/' "$index" This will work from the command prompt, but not from my ksh script. Why not? sed: command garbled: s/<\/body>/<A HREF=/accounts/students/b/bmwg6c/public_html/index.html>I would like to call it white... (4 Replies)
Discussion started by: robin_simple
4 Replies

6. Linux

Adding a prefix to a column using awk/sed commands

Hello, I am a newbie to linux and struggling to find a better way to append a column in a text file. Here is the file i want to modify: It has 8 columns (and thousands of rows). I want to append the first column by adding "chr" infront of the numbers. Some rows have a string in the first... (4 Replies)
Discussion started by: bjorngill
4 Replies

7. Emergency UNIX and Linux Support

Adding carriage returns to file using sed/awk

Hello, I need help adding carriage returns at specific intervals (say 692 characters) to a text file that's one continous string. I'm working in AIX5.3. Any quick help is appreciated. Thanks! (2 Replies)
Discussion started by: bd_joy
2 Replies

8. Shell Programming and Scripting

sed/awk-adding numeric to a column

I have a txt file as follows Code: Oct 1 file1 4144 Oct 1 file23 5170 Oct 2 file5 3434 Oct 21 file56 2343 I need to add a new column by marking the right log file from current directory. For example populate like this. Please not in the second columt for "1" it has... (2 Replies)
Discussion started by: gubbu
2 Replies

9. UNIX for Dummies Questions & Answers

sed to replace text with a variable

I'm trying to replace text in a file with text from a variable I have the following in my script, but its not working: #!/bin/ksh echo "Enter the path to load scripts" read x echo "updating the templates" sed "s/CHANGE_ME_TO_LOAD_PATH/"$x"/g" LoadFiles.sh > LoadFiles2.sh I thought... (1 Reply)
Discussion started by: orahi001
1 Replies

10. UNIX for Dummies Questions & Answers

Adding a new field using sed or awk?

I have a bcp file that contains 10 fields. These fields are separated by a tab. How can I add my name as a new field in the 8th position for every record? I've been playing w/ sed and awk but can't seem to figure this out. (3 Replies)
Discussion started by: sasabune
3 Replies
Login or Register to Ask a Question