Help answer threads with 0 replies.
Go Back > Forums > Linux Forums > Linux - Software
User Name
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.


  Search this Thread
Old 07-18-2011, 07:31 AM   #1
Registered: May 2010
Posts: 133

Rep: Reputation: 16
How to escape single quotes in sed for multi line pattern

I have some code written in VHDL that looks as follows:
foo = "bar";
  foo <= 'bar',

foo : bar
The pattern "foo = "bar";\n foo <= 'bar'," needs to be replaced (but no other patterns in this file). Thus the result is supposed to be:

foo : bar
I tried the following commands:
sed -i ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= 'bar',/foobar/g' myfile
sed -i ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= \'bar\',/foobar/g' myfile
sed -i ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= [\']bar[\'],/foobar/g' myfile
sed -i ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= "'"bar"'",/foobar/g' myfile
sed -ir ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= 'bar',/foobar/g' myfile
sed -ir ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= \'bar\',/foobar/g' myfile
sed -ir ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= [\']bar[\'],/foobar/g' myfile
sed -ir ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= "'"bar"'",/foobar/g' myfile
sed -i ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= /foobar/g' myfile works without problems. The trouble occurs on the first single quote.

Any hints?
Old 07-18-2011, 07:35 AM   #2
Registered: May 2010
Posts: 133

Original Poster
Rep: Reputation: 16
Solved it by myself just seconds later. I need to end the single quoted string, escape the single quote and start the single quoted string again:
sed -i ':a $!{N;ba}; s/foo = "bar";[\n ]*foo <= '\''bar'\'',/foobar/g' myfile


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] sed to display the pattern string, the line above it and the first line of that para rockie321 Linux - Newbie 3 04-03-2011 02:48 PM
[SOLVED] Regx: Multi line pattern matching angel115 Programming 15 01-04-2011 11:43 AM
multiple pattern search in a single file line by line saheervc Linux - Newbie 2 09-01-2010 11:45 PM
replace a pattern with a line using sed/awk lokeshn05 Linux - Newbie 3 05-06-2009 03:01 PM
sed display line after pattern match inonzi_prowler Linux - Software 3 02-19-2007 01:47 PM > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration