LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-08-2009, 08:55 AM   #1
beazizo
LQ Newbie
 
Registered: Oct 2009
Posts: 1

Rep: Reputation: 0
Replacing line with unescaped text


I'm struggling to find a solution to this problem. I want the script below to output foo="\${var}/test" but it outputs foo="${var}/test". It seems sed (or shell) unescapes $prop and removes the backslash in front of ${var}/test.

The value of prop is fixed meaning it can't be double slashed.

I don't want to use awk, only sed.

Am I missing something obvious?

This script is just a trimmed down version of what my script really does to show the problem I'm trying to solve.

#!/bin/bash

prop='foo="\${var}/test"'
prop_name="${prop%%=*}"

echo $prop # Outputs correctly

echo 'foo="should-get-replaced"' | sed 's|^[ \t]*'"$prop_name"'[ \t]*=.*$|'"$prop"'|'

# Outputs: foo="${var}/test"
# Needs to Output: foo="\${var}/test"
 
Old 10-08-2009, 10:55 AM   #2
vonbiber
Member
 
Registered: Apr 2009
Distribution: slackware 14.1 64-bit, slackware 14.2 64-bit, SystemRescueCD
Posts: 533

Rep: Reputation: 129Reputation: 129
Code:
!/bin/bash

prop='foo="\${var}/test"'
prop_name="${prop%%=*}"

echo $prop # Outputs correctly

echo 'foo="should-get-replaced"' | \
        sed 's|^[ \t]*'"$prop_name"'[ \t]*=.*$|'"$prop"'|'

echo 'foo="should-get-replaced"' | \
        sed 's|^[ \t]*'"$prop_name"'[ \t]*=.*$|'"$prop"'|' | \
        sed 's?^\([^"]*"\)?\1\\?'
should output

Code:
foo="\${var}/test"
foo="${var}/test"
foo="\${var}/test"
 
  


Reply



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
Sed append text to end of line if line contains specific text? How can this be done? helptonewbie Linux - Newbie 4 10-23-2013 01:48 PM
replacing text graziano1968 Linux - General 1 03-12-2009 07:14 AM
Replacing a line in a text file jeriryan Linux - Newbie 5 06-23-2008 03:56 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 08:22 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

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