10 More Discussions You Might Find Interesting
1. UNIX for Beginners Questions & Answers
I have the below string which i need to compare with a file and replace this string in the file which matches closely. Can anyone help me on this.
string(Scenario 1)- user::r--,user::ourfrd:r--
String(Scenario 2)- user::r--
# file: /local/Desktop/myfile
# owner: me
# group:... (6 Replies)
Discussion started by: sarathy_a35
2. Shell Programming and Scripting
Sorry for the long/weird title but I'm stuck on a problem I have. I have this XML file:
... (9 Replies)
Discussion started by: cozzin
3. UNIX for Dummies Questions & Answers
I have a requirement in ksh where i have a set of files in a directory. I need to search each and every file if a particular string is present in the file, delete that line and replace that line with another string expression in the same file.
I am very new to unix. Kindly help... (10 Replies)
Discussion started by: Pradhikshan
4. Shell Programming and Scripting
Here's my dilemma.
I need to replace the string Sept_2012 to Oct_2012 in all *config.py files within the current directory and below directories
Is this possible?
Also I am trying to find all instances of the string Sept_2012 within files in the current directory and below
I have... (13 Replies)
Discussion started by: pure_jax
5. Shell Programming and Scripting
here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb
INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
6. Shell Programming and Scripting
Can someone tell me how I can do this?
a=$(echo -e wert trewt ertert ertert ertert erttert
How do i replace the STRING with $a?
I try this:
sed -i 's/STRING/'"$a"'/g' filename.ext
but this don' t work (2 Replies)
Discussion started by: jforce
7. Shell Programming and Scripting
i call my shell like:
my_shell "my project name"
sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp
cp temp test_config_doxy
the following error occurres:
sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
8. Shell Programming and Scripting
Hello: I have another question. Please consider the following two sample, tab-delimited files:
ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9. UNIX for Dummies Questions & Answers
My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found in the same file..The... (27 Replies)
Discussion started by: ganesh_248
10. Shell Programming and Scripting
My requisite is to search for the string "0108"(which is the year and has come in the wrong year format) in a particular column say 4th column in a tab delimited file and then replace it with 2008(the correct year format) in the same position where 0108 was found..The issue is the last... (15 Replies)
Discussion started by: ganesh_248
STR_REPLACE(3) 1 STR_REPLACE(3)
str_replace - Replace all occurrences of the search string with the replacement string
mixed str_replace (mixed $search, mixed $replace, mixed $subject, [int &$count])
This function returns a string or an array with all occurrences of $search in $subject replaced with the given $replace value.
If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of preg_replace(3).
If $search and $replace are arrays, then str_replace(3) takes a value from each array and uses them to search and replace on $subject. If
$replace has fewer values than $search, then an empty string is used for the rest of replacement values. If $search is an array and
$replace is a string, then this replacement string is used for every value of $search. The converse would not make sense, though.
If $search or $replace are arrays, their elements are processed first to last.
- The value being searched for, otherwise known as the needle. An array may be used to designate multiple needles.
- The replacement value that replaces found $search values. An array may be used to designate multiple replacements.
- The string or array being searched and replaced on, otherwise known as the haystack. If $subject is an array, then the search
and replace is performed with every entry of $subject, and the return value is an array as well.
- If passed, this will be set to the number of replacements performed.
This function returns a string or an array with the replaced values.
Basic str_replace(3) examples
// Provides: <body text='black'>
$bodytag = str_replace("%body%", "black", "<body text='%body%'>");
// Provides: Hll Wrld f PHP
$vowels = array("a", "e", "i", "o", "u", "A", "E", "I", "O", "U");
$onlyconsonants = str_replace($vowels, "", "Hello World of PHP");
// Provides: You should eat pizza, beer, and ice cream every day
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream");
$newphrase = str_replace($healthy, $yummy, $phrase);
// Provides: 2
$str = str_replace("ll", "", "good golly miss molly!", $count);
Examples of potential str_replace(3) gotchas
// Order of replacement
$str = "Line 1
$order = array("
$replace = '<br />';
's first so they aren't converted twice.
$newstr = str_replace($order, $replace, $str);
// Outputs F because A is replaced with B, then B is replaced with C, and so on...
// Finally E is replaced with F, because of left to right replacements.
$search = array('A', 'B', 'C', 'D', 'E');
$replace = array('B', 'C', 'D', 'E', 'F');
$subject = 'A';
echo str_replace($search, $replace, $subject);
// Outputs: apearpearle pear
// For the same reason mentioned above
$letters = array('a', 'p');
$fruit = array('apple', 'pear');
$text = 'a p';
$output = str_replace($letters, $fruit, $text);
This function is binary-safe.
Replacement order gotcha
Because str_replace(3) replaces left to right, it might replace a previously inserted value when doing multiple replacements. See
also the examples in this document.
This function is case-sensitive. Use str_ireplace(3) for case-insensitive replace.
str_ireplace(3), substr_replace(3), preg_replace(3), strtr(3).
PHP Documentation Group STR_REPLACE(3)