I got a sample file like this.
$ cat test
12|13|100|s
12|13|100|s
100|13|100|s
12|13|100|s
I want to replace all 100 by 2000 only in 3rd field using "awk"
This is replacing all 100's :-(
$ awk -F "|" '{gsub( /100/,"2000");print}' test
12|13|2000|s
12|13|2000|s
2000|13|2000|s... (5 Replies)
Greetings all.
I have web site that has long option and switch lists. When I insert something new into these files, the lists need to be reordered. IE:
1 => apple
2 => pear
3 => bannana
4 => orange
---------------------
Add grape as #2
1 => apple
2 => grape
3 => pear
4 =>... (2 Replies)
Hi,
I have a Line input for awk as follows
DROP MATERIALIZED VIEW MCR.COMM_STACK;
CREATE MATERIALIZED VIEW "MCR"."COMM_STACK"
ON PREBUILT TABLE WITHOUT REDUCED PRECISION
USING INDEX
REFRESH FAST ON DEMAND START WITH sysdate+0 NEXT SYSDATE + 7
WITH PRIMARY KEY USING DEFAULT... (3 Replies)
I am trying to write a find and replace script with AWK and I can't seem to get it to work. I need it to find this exact string *P*: and replace the P with a T or just replcare the whole thing with *T*:.
this is what I have tried
awk 'BEGIN {gsub(/\*P*:/,"\*T*:"); print}' ${INFILE} >... (4 Replies)
Hi All,
point 1. I have n number of environment variable files in different folders. All file names are ending with one thing common ie, *envset.sh
point 2. All these contains Varilables and some other information like following
*envset.sh ===>>
... (3 Replies)
Hello Experts,
I have a input file that I need to replace a value only if the file contains the number 6 in column 1. I would like to use AWK in a shell script (ksh on a AIX platform). I need all rows written out, but only change 2 fields when the first column contains a numer 6.
Input... (2 Replies)
All,
I have thousands of lines in a file with following format
DATA=_ONE_XXX_YYY_CCC_HHHG_
DATA1=_GGG_JJJJ_HHH_UUU_JJJJ_HHHH_LLL_
DATA3=_MMM_GG_NN_QQQQ_FFF_III_
I want to replace _ with . by ignoring the first (=_) and last (_)
So that out put should looks like... (4 Replies)
Friends,
I have more the thousand lines like this.
check.cloud1.port=342
check.cloud2.port=5456
check.cloud3.port-4564
But we need to transfer it to
_CHECK.CLOUD1.PORT_=342
_CHECK.CLOUD2.PORT_=5456
_CHECK.CLOUD3.PORT_=4564
Any one could pls help of this. Thanks in Advance
... (1 Reply)
Hello,
I have a requirement to replace the whole string with first occurence of value of a key-value pair.
I have to do this inside awk, as the data I need to work is inside an awk loop.
value of my variable(say LogText) looks like:
LogText=
date=04Mar message=hello1 name=caq... (4 Replies)
Discussion started by: cool.aquarian
4 Replies
LEARN ABOUT PHP
preg_replace_callback
PREG_REPLACE_CALLBACK(3) 1 PREG_REPLACE_CALLBACK(3)preg_replace_callback - Perform a regular expression search and replace using a callbackSYNOPSIS
mixed preg_replace_callback (mixed $pattern, callable $callback, mixed $subject, [int $limit = -1], [int &$count])
DESCRIPTION
The behavior of this function is almost identical to preg_replace(3), except for the fact that instead of $replacement parameter, one
should specify a $callback.
PARAMETERS
o $pattern
- The pattern to search for. It can be either a string or an array with strings.
o $callback
- A callback that will be called and passed an array of matched elements in the $subject string. The callback should return the
replacement string. This is the callback signature:
string handler (array $matches) You'll often need the $callback function for a preg_replace_callback(3) in just one place. In
this case you can use an anonymous function to declare the callback within the call to preg_replace_callback(3). By doing it this
way you have all information for the call in one place and do not clutter the function namespace with a callback function's name
not used anywhere else.
Example #1
preg_replace_callback(3) and anonymous function
<?php
/* a unix-style command line filter to convert uppercase
* letters at the beginning of paragraphs to lowercase */
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
$line = fgets($fp);
$line = preg_replace_callback(
'|<p>s*w|',
function ($matches) {
return strtolower($matches[0]);
},
$line
);
echo $line;
}
fclose($fp);
?>
o $subject
- The string or an array with strings to search and replace.
o $limit
- The maximum possible replacements for each pattern in each $subject string. Defaults to -1 (no limit).
o $count
- If specified, this variable will be filled with the number of replacements done.
RETURN VALUES preg_replace_callback(3) returns an array if the $subject parameter is an array, or a string otherwise. On errors the return value is NULL
If matches are found, the new subject will be returned, otherwise $subject will be returned unchanged.
CHANGELOG
+--------+---------------------------------+
|Version | |
| | |
| | Description |
| | |
+--------+---------------------------------+
| 5.1.0 | |
| | |
| | The $count parameter was added |
| | |
+--------+---------------------------------+
EXAMPLES
Example #2
preg_replace_callback(3) example
<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text = "April fools day is 04/01/2002
";
$text.= "Last christmas was 12/24/2001
";
// the callback function
function next_year($matches)
{
// as usual: $matches[0] is the complete match
// $matches[1] the match for the first subpattern
// enclosed in '(...)' and so on
return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
"|(d{2}/d{2}/)(d{4})|",
"next_year",
$text);
?>
The above example will output:
April fools day is 04/01/2003
Last christmas was 12/24/2002
Example #3
preg_replace_callback(3) using recursive structure to handle encapsulated BB code
<?php
$input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";
function parseTagsRecursive($input)
{
$regex = '#[indent]((?:[^[]|[(?!/?indent])|(?R))+)[/indent]#';
if (is_array($input)) {
$input = '<div style="margin-left: 10px">'.$input[1].'</div>';
}
return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}
$output = parseTagsRecursive($input);
echo $output;
?>
SEE ALSO
PCRE Patterns, preg_quote(3), preg_replace(3), preg_last_error(3), Anonymous functions, information about the callback type.
PHP Documentation Group PREG_REPLACE_CALLBACK(3)