substitution

Best way to substitute variables in plain text using PHP

What's the best way to take some plain text (not PHP code) which contains PHP-style variables, and then substitute in the value of the variable. This is kinda hard to describe, so here's an example. // -- myFile.txt -- Mary had a little $pet. // -- parser.php -- $pet = "lamb"; // open myFile.txt and transform it such that... $newConten...

Why is $1 empty in my substitution?

I am trying to put the stuff within parentheses into the value of a src attribute in an img tag: while(<TOCFILE>) { $toc_line = $_; $toc_line =~ s/<inlineFig.*?(\.\.\/pics\/ch09_inline99_*?\.jpg)*?<\/inlineFig>/<img src="${1}" alt="" \/\>/g; $new_toc_file .= $toc_line; } So I expected to see tags like this in the output: ...

What does the Perl substitution operator act on?

I have been programming in Perl, off and on, for years now, although only sporadically is it my primary language. Because I often go months without writing any perl, I rely heavily on my dog-eared Camel Book to remind me how to do things. However, when I copy recipes verbatim with no understanding, this bothers me. This is one of the mos...

Force Full Path in the Connection String of Linq DataContext

I'm using System.Data.Linq.DataContext file for accessing a mdf Database I want to use the Database from the project directory and not the one created by the debugger in the Debug directory. The problem is when I edit the Connection String and choose the path for AttachDBFilename, VS2008 automaticly substitutes my project directory wit...

How do I replace the values of a hash in an external file?

For my program, I'm attempting to replace the value of a specific hash in an external file with a newly created value. The external file has the value tab-delimited from the key, and I had read the hash in from the external file. I've been looking around online, and this is the closest way I could figure out how to do it, yet it doesn't ...

Vim: Repeat a command with substitution

In Unix the ^ allows you to repeat a command with some text substituted for new text. For example: csh% grep "stuff" file1 >> Results grep "stuff" file1 csh% ^file1^file2^ grep "stuff" file2 csh% Is there a Vim equivalent? There are a lot of times I find myself editing minor things on the command line over and over again. ...

How to use a variable in the replacement side of the Perl substitution operator?

I would like to do the following: $find="start (.*) end"; $replace="foo \1 bar"; $var = "start middle end"; $var =~ s/$find/$replace/; I would expect $var to contain "foo middle bar", but it does not work. Neither does: $replace='foo \1 bar'; Somehow I am missing something regarding the escaping. I fixed the missing 's' ...

How can I substitute regexp matches and map the substitutions in Perl?

I.e.: echo H#97llo | MagicPerlCommand Stdout: Hallo were MagicPerlCommand is something like perl -pnle "s/#(\d+)/chr(\1)/ge" (but that doesn't work). ...

Vi substitution command

Using vi, how do I substitute the current line number somewhere into the current line? For example, if the cursor is on line 10, I would like to put the number 10 somewhere on that line. ...

Why aren't my XSLT variables substituting their values?

I am trying to use XSLT variables and not having much success, hopefully I'm just doing something dumb. I have the following code snippet: <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt" version="1.0"> <xsl:template match="/"> <xsl:variable name="config" select=...

VIM: substitute with contents of register / lines range

I'm using VIM, and I want to substitute some placeholder text with a long string, that spans several lines, which is already written somewhere else in the file. Is it possible to replace a pattern with the contents of a register? Something like :%s/foo/<contents of register A> Otherwise, is it possible to replace with a range of line...

How can I make SQL Developer/SQL+ prompt only once for a substitution variable that occurs multiple times in a single statement?

I have a query roughly like this: select * from A_TABLE where A_COLUMN = '&aVariable' union select * from A_TABLE where B_COLUMN = '&aVariable'; But when I run it, SQL Developer prompts me for the variable twice, even though it's the same variable. If there's a way to make it prompt only once for a variable that is ...

How can I efficiently handle multiple Perl search/replace operations on the same string?

So my Perl script basically takes a string and then tries to clean it up by doing multiple search and replaces on it, like so: $text =~ s/<[^>]+>/ /g; $text =~ s/\s+/ /g; $text =~ s/[\(\{\[]\d+[\(\{\[]/ /g; $text =~ s/\s+[<>]+\s+/\. /g; $text =~ s/\s+/ /g; $text =~ s/\.*\s*[\*|\#]+\s*([A-Z\"])/\. $1/g; # replace . **** Begin or . #### B...

Enumerate substitutions with sed or awk

Given the plain text file with lines bli foo bla abc dfg bli foo bla hik lmn what sed or awk magic transforms it to bli foo_01 bla abc dfg bli foo_02 bla hik lmn so that every occurence of 'foo' is replaced by 'foo_[occurence number]'. ...

Perl: Loop through a file and substitute

Hi! I simply wanna read in a logfile, do a search and replace, and then write out the changes to that same logfile. What's the best practice way of doing this in Perl? ...

Vim substitution of a list of words with another same length list of words?

I need to substitute a list of words with with an equally long list of words. So for example you have: "a","b","c","d","e","f" And you want to replace each word with the uppercase version of each word: "A","B","C","D","E","F" I know how to find each string using the regex: (a\|b\|c\|d\|e\|f) I know you could do a global substitution ...

How can I replace all the text before the match in a Perl substitution?

Hi I am reading each line of an input file (IN) and printing the line read to an output file (OUT) if the line begins with one of the patterns, say "ab", "cd","ef","gh","ij" etc. The line printed is of form "pattern: 100" or form "pattern: 100:200". I need to replace "pattern" with "myPattern", i.e. print the current line to FILE but re...

How can I use the "s///" operator in order to change number of values?

Hello friends, I have a file that contains: foo1 = 1 foo2 = 2 foo3 = 8 . . . I need to replace only the values (1,2,8...) in that file with part of a memory hash values, the ones with the same keys (foo1 -> 33,foo2 -> 44,foo3...) How can I change it using the "s///" operator? If there is other elegant ways to conduct it I'll b...

Convert Pascal-cased setter to an underscore-separated variable name

This is not as simple as it seems. Most of you are likely thinking of the regex /([A-Z])/_$1/ like I have found all over the Internet, but my particular situation is slightly more complicated. My source string contains more content that I don't want to have converted before a portion that I do. Consider a regular setter: public funct...

How to enclose text patterns within xml elements, except when it is already inside a certain xml element?

I have several thousand xml files generated from java properties files prepared for translation in the TTX format. They contain quite a few variables, that I need to protect from the translators, as they often break such things. The variables are in the form of numbers or occasionally text between a pair of curly braces eg. {0}, {this}. ...