11-08-2019
@Reduic : Please find the below explanation on my requirements for scenario-1.
Input parameter:
********************
Scenario-1:
STRING="user::r--,user::ourfrd:r--"
Data available in File:
**************************
# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::rwx
user:myfrd:rwx
user:hisfrd:r-x
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x
Expected Results - Modified File data
**********************************************
For Scenario-1:
# file: /local/Desktop/myfile
# owner: me
# group: mygroup
user::r--
user:myfrd:rwx
user:hisfrd:r-x
user:ourfrd:r--
group::r-x
group:mygroup:rwx
mask::rwx
other::r-x
Note: In the above result, the string "user:ourfrd:r--" is added newly to the file since it is not available already in file. Only the partially matching strings to be replaced like how we have "user::rwx" exists in file and the first few characters "user::" is getting matched with new input parameter "user::r--". so "user::r--" needs to be updated in file. This is what im trying for, hope now my requirement is clear to you ..
9 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi All,
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
15 Replies
2. UNIX for Dummies Questions & Answers
Hi All,
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
27 Replies
3. Shell Programming and Scripting
Hello: I have another question. Please consider the following two sample, tab-delimited files:
File_1:
Abf1 YKL112w
Abf1 YAL054c
Abf1 YGL234w
Ace2 YKL150w
Ace2 YNL328c
Cup9 YDR441c
Cup9 YDR442w
Cup9 YEL040w
...
File 2:
...
ABF1 YKL112W
ACE2 YLR131C (9 Replies)
Discussion started by: gstuart
9 Replies
4. Shell Programming and Scripting
Search and Replace a string pattern with empty in an xml file in unix:
My xml file would be like this :
<Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts>
... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies
5. UNIX for Dummies Questions & Answers
Hi
I am looking for a particular string in a file.If the string exists, then I want to replace another string with some other text.Once replaced, search for the same text after that character position in the file. :wall:
E.g: Actual File content:
Hello
Name: Nitin Raj
Welcome to Unix... (4 Replies)
Discussion started by: dashing201
4 Replies
6. Shell Programming and Scripting
I have a list of strings in file:
10 10 AAA
120 13 BBBBB
23 11 CCCCC
11 32 DDDDDD
I want to replace first column of the text such as: 10, 129, 23, 11 with 11, 22, 33, 44.
I can do line by line, but just not sure how to replace partial string without... (1 Reply)
Discussion started by: ford99
1 Replies
7. Shell Programming and Scripting
I have file t1.log
Contents of t1.log file
Number of records processed:
Number of records rejected:
Error :
xyz ..........
abc ..........
aaa _]
start time :
end time :
Please let me know how i can remove the contents highlighted in red in the t1.log file.
Thanks
Sam (3 Replies)
Discussion started by: sam777
3 Replies
8. Shell Programming and Scripting
Hi Guys,
I need replace part of string in a file.
for example:
ABC=123
CDE=122
DEF=456
ABC=123
DED=333
ABC=123
I need replace the value after ABC=, highlighted in red. I want to get following result;
ABC=456
CDE=122
DEF=456
ABC=456
DED=333
ABC=456
Anybody can help me this.
... (8 Replies)
Discussion started by: ken6503
8 Replies
9. UNIX for Dummies Questions & Answers
Hi Everyone,
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
10 Replies
LEARN ABOUT PHP
substr_replace
SUBSTR_REPLACE(3) 1 SUBSTR_REPLACE(3)
substr_replace - Replace text within a portion of a string
SYNOPSIS
mixed substr_replace (mixed $string, mixed $replacement, mixed $start, [mixed $length])
DESCRIPTION
substr_replace(3) replaces a copy of $string delimited by the $start and (optionally) $length parameters with the string given in $replace-
ment.
PARAMETERS
o $string
- The input string. An array of strings can be provided, in which case the replacements will occur on each string in turn. In
this case, the $replacement, $start and $length parameters may be provided either as scalar values to be applied to each input
string in turn, or as arrays, in which case the corresponding array element will be used for each input string.
o $replacement
- The replacement string.
o $start
- If $start is positive, the replacing will begin at the $start'th offset into $string. If $start is negative, the replacing will
begin at the $start'th character from the end of $string.
o $length
- If given and is positive, it represents the length of the portion of $string which is to be replaced. If it is negative, it rep-
resents the number of characters from the end of $string at which to stop replacing. If it is not given, then it will default to
strlen( $string ); i.e. end the replacing at the end of $string. Of course, if $length is zero then this function will have the
effect of inserting $replacement into $string at the given $start offset.
RETURN VALUES
The result string is returned. If $string is an array then array is returned.
EXAMPLES
Example #1
Simple substr_replace(3) examples
<?php
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />
";
/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . "<br />
";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />
";
/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />
";
/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />
";
echo substr_replace($var, 'bob', -7, -1) . "<br />
";
/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />
";
?>
Example #2
Using substr_replace(3) to replace multiple strings at once
<?php
$input = array('A: XXX', 'B: XXX', 'C: XXX');
// A simple case: replace XXX in each string with YYY.
echo implode('; ', substr_replace($input, 'YYY', 3, 3))."
";
// A more complicated case where each replacement is different.
$replace = array('AAA', 'BBB', 'CCC');
echo implode('; ', substr_replace($input, $replace, 3, 3))."
";
// Replace a different number of characters each time.
$length = array(1, 2, 3);
echo implode('; ', substr_replace($input, $replace, 3, $length))."
";
?>
The above example will output:
A: YYY; B: YYY; C: YYY
A: AAA; B: BBB; C: CCC
A: AAAXX; B: BBBX; C: CCC
NOTES
Note
This function is binary-safe.
SEE ALSO
str_replace(3), substr(3), String access and modification by character.
PHP Documentation Group SUBSTR_REPLACE(3)