sed error: unescaped newline


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed error: unescaped newline
# 1  
Old 06-03-2010
sed error: unescaped newline

Hi,
i need to make a script bash that takes in input three parameters: a text file that contains an old license, a file that contains a new license, and a directory that contains source files and i have to substitute to these file the old license with the new one.

So, i think tu use sed, in this way:
Code:
$old_license= 'cat old_license.txt'
$new_license= 'cat new_license.txt'

sed -i -e "s/$old_license/$new_license/g" $file

but i got this error "unescaped newline inside substitute pattern"..
How can i do to solve my problem?

thanks to all

Last edited by Franklin52; 06-03-2010 at 06:41 AM.. Reason: Please use code tags!
# 2  
Old 06-03-2010
Try
Quote:
Originally Posted by nolanofra
Code:
old_license=`cat old_license.txt`
new_license=`cat new_license.txt`

sed -i -e "s/$old_license/$new_license/g" $file

or better
Code:
old_license=$(cat old_license.txt)
new_license=$(cat new_license.txt)

sed -i -e "s/$old_license/$new_license/g" $file

If this doesn't work for you, run following in the shell: cat old_license.txt cat new_license.txt and copy the output from the shell and paste it here in codetags.
# 3  
Old 06-03-2010
You cannot have an unescaped newline in sed replacement text ($new_license in your example). sed scripting uses the newline just like the shell does, to terminate a command. If you need a newline in the replacement text, you need to precede it with a backslash. Alternatively, if you're using gnu sed and do not care about portability to other sed implementations, you can replace a newline with a \n, in the replacement text (all posix-compliant seds allow this in the regular expression, but not in the replacement ... it is a gnu extension).

This approach is also vulnerable to sed metacharacters in both the regular expression and the replacement text. If old_license and new_license can contain characters that are special to sed, you need to rethink what you are doing. Perhaps use awk's string functions to accomplish the replacement without using regular expressions, or use perl, whose syntax supports quoting interpolated values in regular expressions.

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Why SED can't see the last newline character?

Removed. My question does not make sense. and SED does see the last newline character. But I still have a question: How to remove the last newline character(the newline character at the end of last line) using SED? ---------- Post updated 05-01-11 at 10:51 AM ---------- Previous update was... (7 Replies)
Discussion started by: kevintse
7 Replies

2. Shell Programming and Scripting

Sed with unescaped input

a shell script variable from user input, via read, will typically have special characters in it - i.e. sip:29384902834!someServer.com@someOtherServer.com this value has to stay formatted like this because it being used by curl in a post to php. but it is also being used by sed to replace the... (4 Replies)
Discussion started by: Gripp
4 Replies

3. Shell Programming and Scripting

sed insert text without newline

Hi, I use sed to insert text at beginning of a file. But sed inserts a newline after my text that I do not need. For example, I want to insert "foo" at the beginning of my file: > cat myfile This is first line. > sed -i '1i\foo' myfile > cat myfile foo This is first line. ... (5 Replies)
Discussion started by: tdw
5 Replies

4. Shell Programming and Scripting

sed newline

Hi everyone, I'd like to use the script validatehtml which returns either the given url is HTML strict or not, using http:// validator . w3 . org . sh validatehtml #!/bin/bash wget -q http:// validator . w3 .org / check?uri=$1 cat check\?uri\=$1 | sed -n '/h2/ p' | sed 's/ */ /g' | sed... (2 Replies)
Discussion started by: azertyazerty
2 Replies

5. Shell Programming and Scripting

sed newline to tab ,problem

Input: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly sed 's/\n/\t/g' infile It's not working. Output should be: gstreamer-plugins-good gstreamer-plugins-bad gstreamer-plugins-ugly (5 Replies)
Discussion started by: cola
5 Replies

6. Shell Programming and Scripting

Using sed I want to replace space by newline

Input: -------------------------- 123asd 456sdasda 789a ------------------------- output wanted: --------------------- 123asd 456sdasda 789a ---------------------- I want this by sed in simple way please help (I know by: tr ' ' '\n' < inputfile )I want it by sed only (5 Replies)
Discussion started by: RahulJoshi
5 Replies

7. Shell Programming and Scripting

Errore Sed: unescaped newline

Salve, mi occorre realizzare uno script che prenda in ingresso tre parametri: un file di testo contenente una vecchia licenza, un file contenente una nuova licenza e una directory, contenente dei sorgenti c a cui sostituire la vecchia licenza con la nuova. Pensavo di utilizzare sed, con... (1 Reply)
Discussion started by: nolanofra
1 Replies

8. Shell Programming and Scripting

SED to convert ~ in a file to newline

Hi, I have a .txt file which has a tilde(~) in it. All that I want is to break into a newline whenever there is an occurence of '~'. I have tried SED to do that but I could not succeed. I would appreciate if I can get a shell script(ksh) for this problem real quick. Thanks in advance. ... (5 Replies)
Discussion started by: ntekupal
5 Replies

9. Shell Programming and Scripting

SED: how to remove newline after pattern?

Hi, I have the following XML not well-indented code: <hallo >this is a line </hallo> So I need to remove the newline. This syntax finds what I need to correct, but I don't know how to remove the newline after my pattern: sed 's/<.*$/&/' How can I subtract the newline after my... (1 Reply)
Discussion started by: nico.ben
1 Replies

10. Shell Programming and Scripting

Sed append newline character

Hi All, I am new to Shell scripting.. I have a task to parse the text file into csv format. more then half the things has done. But the problem is when I use the sed command in shell script. it appends newline character at the end of the line. and so when I open the file in CSV it's format... (3 Replies)
Discussion started by: Gaurang033
3 Replies
Login or Register to Ask a Question