LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-03-2008, 01:44 AM   #1
horacioemilio
Member
 
Registered: Dec 2007
Posts: 61

Rep: Reputation: 15
Substitution of a long string


Hi,

when modifying text strings on files, I usually want to change for instance

sed -e "s/CENTERY/$CY/g" ${f1} > ${f2}

so there is no problem.

But now I have to look on files and modify a "long" string with spaces like:

Use only orig.conf.? [1 (yes) or 0 (no)]: 1

to

Use only orig.conf.? [1 (yes) or 0 (no)]: 0

I tried to use

string="Use only orig.conf.? [1 (yes) or 0 (no)]: 1"
stringout="Use only orig.conf.? [1 (yes) or 0 (no)]: 1"

sed -e "s/${string}/${stringout}/g" ${f1} > ${f2}

but it does not work, any suggestions ?

Thanks in advance.
 
Old 04-03-2008, 04:47 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
Please post code in [code] tags. This preserves whitespace for indentation, uses a fixed width font and puts the code in a highlighted box. All this really improves readability, and with something like python actually changes the code's meaning (as indentation has meaning).

sed's substitution command (s/pattern/replacement/options) does not do a simple string replace... the pattern is a regular expression, which has many special characters. If those in your search pattern, these are special characters: . [ ] ?

For all these special characters, you must escape them. Usually this would mean prefixing them with a backslash, like this:
Code:
echo '[1 (yes) or 0 (no)]: 0' | sed 's/\[1 (yes) or 0 (no)\]: 0/replacement done/g'
If you paste this onto the command line, you'll see it works from the output.

There is one slight complication in your script. You are using double quotes, and you cannot pass backslashes so easily in double quotes, as the shell uses it as an escape character. See the differences in output of these two commands:
Code:
$ echo '\[ \] \" \$'
\[ \] \" \$
$ echo "\[ \] \" \$"
\[ \] " \$
Double quotes pass the backslash sometimes and not others... So I recommend when setting the value of string and stringout use single quotes. Make sure you don't get confused with the backtick and the single quote... the backtick has an altogether different usage in shell scripting.
 
Old 04-03-2008, 03:30 PM   #3
prad77
Member
 
Registered: Mar 2008
Posts: 101

Rep: Reputation: 15
This thread had a similar situation and might help..
http://www.linuxquestions.org/questi...tution-620931/

Gentoo

Last edited by prad77; 04-17-2008 at 03:40 AM.
 
Old 04-04-2008, 12:55 AM   #4
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
i'm not sure bout this but why is ur string and stringout the same?

(edit) of course even though you make the in string and out string different, it won't make any difference unless you change the syntax of the strings

aside from sed, if you have replace, you can also do:
Code:
s1="Use only orig.conf.? [1 (yes) or 0 (no)]: 0"
s2="Use only orig.conf.? [1 (yes) or 0 (no)]: 1"
replace "${s1}" ${s2}" -- ${f1} > ${f2}

Last edited by konsolebox; 04-04-2008 at 12:59 AM.
 
Old 04-04-2008, 04:56 AM   #5
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
What provides replace? I don't see it on my ubuntu system.
 
Old 04-07-2008, 09:21 PM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

Rep: Reputation: 233Reputation: 233Reputation: 233
In gentoo and slackware, it's mysql but i don't know if mysql is the real provider of the program.
 
  


Reply


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
Complicated string substitution horacioemilio Programming 5 02-13-2008 06:40 PM
hex string to long in c rubadub Programming 5 07-26-2007 12:48 PM
Converting time from string to long format Deepak Inbasekaran Programming 6 07-21-2006 05:00 PM
convert string to long/int alaios Programming 10 09-15-2005 09:01 AM
Can 't compile because long string is involved Linh Programming 3 06-11-2003 05:52 PM


All times are GMT -5. The time now is 10:00 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration