How to Find and Replace Text in a File

How to search and replace text in a file?

fileinput already supports inplace editing. It redirects stdout to the file in this case:

#!/usr/bin/env python3
import fileinput

with fileinput.FileInput(filename, inplace=True, backup='.bak') as file:
for line in file:
print(line.replace(text_to_search, replacement_text), end='')

How to find and replace text in a file

Read all file content. Make a replacement with String.Replace. Write content back to file.

string text = File.ReadAllText("test.txt");
text = text.Replace("some text", "new value");
File.WriteAllText("test.txt", text);

How can you find and replace text in a file using the Windows command-line environment?

A lot of the answers here helped point me in the right direction, however none were suitable for me, so I am posting my solution.

I have Windows 7, which comes with PowerShell built-in. Here is the script I used to find/replace all instances of text in a file:

powershell -Command "(gc myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt"

To explain it:

  • powershell starts up powershell.exe, which is included in Windows 7
  • -Command "... " is a command line arg for powershell.exe containing the command to run
  • (gc myFile.txt) reads the content of myFile.txt (gc is short for the Get-Content command)
  • -replace 'foo', 'bar' simply runs the replace command to replace foo with bar
  • | Out-File myFile.txt pipes the output to the file myFile.txt
  • -encoding ASCII prevents transcribing the output file to unicode, as the comments point out

Powershell.exe should be part of your PATH statement already, but if not you can add it. The location of it on my machine is C:\WINDOWS\system32\WindowsPowerShell\v1.0

Update
Apparently modern windows systems have PowerShell built in allowing you to access this directly using

(Get-Content myFile.txt) -replace 'foo', 'bar' | Out-File -encoding ASCII myFile.txt

Several find and replace from text file

Using the regex supplied by jay.sf, you could use str_replace_all from the stringr package to do it with a named vector.

library(stringr)

new_tx <- str_replace_all(tx,
c("\\sis taken on\\s" = " ",
"\\b\\sat\\s" = "\t",
"\\b\\sp\\b" = ","))

cat(new_tx)

Result

-The data 1 Aug, 2009    UBC
and is significant with, value <0.01

-The data 2 Sep, 2012 SFU
and is not significant with, value > 0.06

how to Find and replace text with in a file from terminal using vim editor

You could use sed:

sed -ie "s/>/\">\"/g" /path/to/file
  • -i: edit in place
  • -e: run the following command
  • "s/text/replacement/g": replace every occurrence of text with replacement

How to find and replace text in text file using php?

There is an error in your code.

$lines = file($myFile, FILE_IGNORE_NEW_LINES);

should be

$lines = file($myfile, FILE_IGNORE_NEW_LINES);

because you're declaring
$myfile = 'test.txt';

Note the usage of smaller case f in file name.

Now, coming to your question.

You want to replace some lines in the file with user input.

Let's assume that you want to replace the text THIS SHOULD BE REPLACED.
For that, you could use the code something line below:

<?PHP
$myfile = './test.txt';
$lines = file($myfile, FILE_IGNORE_NEW_LINES);

$textToBeReplaced = "THIS SHOULD BE REPLACED";

$index = array_search($textToBeReplaced, $lines);
if (false !== $index) {
$lines[$index] = $_POST['title'] ?? '';
}
file_put_contents($myFile , implode("\n", $lines));
?>

replacing text in a file with Python

This should do it

replacements = {'zero':'0', 'temp':'bob', 'garbage':'nothing'}

with open('path/to/input/file') as infile, open('path/to/output/file', 'w') as outfile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
outfile.write(line)

EDIT: To address Eildosa's comment, if you wanted to do this without writing to another file, then you'll end up having to read your entire source file into memory:

lines = []
with open('path/to/input/file') as infile:
for line in infile:
for src, target in replacements.items():
line = line.replace(src, target)
lines.append(line)
with open('path/to/input/file', 'w') as outfile:
for line in lines:
outfile.write(line)

Edit: If you are using Python 2.x, use replacements.iteritems() instead of replacements.items()

Find and replace text in all files rstudio

This is now possible with the 1.3 version of RStudio. You can learn more at https://blog.rstudio.com/2020/03/17/rstudio-1-3-the-little-things/.



Related Topics



Leave a reply



Submit