A
A
Azat Kiberov2018-12-28 20:43:19
cmd/bat
Azat Kiberov, 2018-12-28 20:43:19

How to quickly replace words in different files using a pattern from a text file?

There are the following files:
1.php
10.php
30.php
Inside them the text is the same:
One at home ate pate.
There are also words to replace the word "pate" which is in 33.txt
Jam.
Honey.
Cake.
The task is this.
Open the file 1.php replace the pate with the word from txt this Jam save close.
Open the file 10.php replace the pate with the word from txt this honey save close.
Open the file 30.php replace the pate with the word from txt this is Cake save close.
And there can be hundreds of files in which you need to replace a word.
As it is desirable through bat with one click to replace all files. Or notepad ++.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2018-12-28
@res2001

Write a replacement script in the same php or use the sed utility.
sed is a Linux utility, but its Windows version is included with msys2, git bash, cygwin, etc. packages.
sed calls can be wrapped in a batch file.
Directly using the bat script for a direct replacement most likely will not work, because. php files usually contain characters that are special to bat (such as: <>&| ...), because of them, the bat script will fail and do not work.
But sed calls can be wrapped in a batch file, because sed will do the replacement directly, not the batch file.

S
sergey, 2018-12-30
kuzmin @sergueik

I didn't understand where catch ///// ?
```
1..3 | foreach-object {echo 'The text inside them is the same meat' | out-file -filepath $_$_".txt" }
$plan = @{
'11.txt' = @{old ='meat';
new = 'red caviar';}
'22.txt' = @{old ='meat';
new = 'black bread';}
'33.txt' = @{old ='meat';
new = 'something inedible';}
}
$plan.keys | foreach-object {
$datafile = $_
$text = get-content -path (resolve-path $datafile)
$new_text = $text -replace $plan[$datafile]['old'], $plan[$datafile][ 'new'
] write-output $new_text | out-file -filepath (resolve-path $datafile)
$new_text = get-content -path (resolve-path $datafile)
write-output ('{0} got {1}' -f $datafile, $new_text)
}
```

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question