Replace a Whole Line Where a Particular Word Is Found in a Text File

Search the word, and replace the whole line containing the word in a file in Python using fileinput

I realized that I was wrong by just an indentation. In the code piece 1 mentioned in the question, if I am bringing the 'print line,' from the scope of if i.e. if i outdent it, then this is solved...

As this line was inside the scope of if, hence, only this new_text was being written to the file, and other lines were not being written, and hence the file was left with only the new_text. So, the code piece should be as follow :-

text = "mov9 = "   # if any line contains this text, I want to modify the whole line.
new_text = "mov9 = Alice in Wonderland"
x = fileinput.input(files="C:\Users\Admin\Desktop\DeletedMovies.txt", inplace=1)
for line in x:
if text in line:
line = new_text
print line,
x.close()

Also, the second solution given by Rolf of Saxony & the first solution by Padraic Cunningham is somehow similar.

Replace a whole line where a particular word is found in a text file

One approach that you can use on smaller files that can fit into your memory twice:

$data = file('myfile'); // reads an array of lines
function replace_a_line($data) {
if (stristr($data, 'certain word')) {
return "replacement line!\n";
}
return $data;
}
$data = array_map('replace_a_line', $data);
file_put_contents('myfile', $data);

A quick note, PHP > 5.3.0 supports lambda functions so you can remove the named function declaration and shorten the map to:

$data = array_map(function($data) {
return stristr($data,'certain word') ? "replacement line\n" : $data;
}, $data);

You could theoretically make this a single (harder to follow) php statement:

file_put_contents('myfile', implode('', 
array_map(function($data) {
return stristr($data,'certain word') ? "replacement line\n" : $data;
}, file('myfile'))
));

Another (less memory intensive) approach that you should use for larger files:

$reading = fopen('myfile', 'r');
$writing = fopen('myfile.tmp', 'w');

$replaced = false;

while (!feof($reading)) {
$line = fgets($reading);
if (stristr($line,'certain word')) {
$line = "replacement line!\n";
$replaced = true;
}
fputs($writing, $line);
}
fclose($reading); fclose($writing);
// might as well not overwrite the file if we didn't replace anything
if ($replaced)
{
rename('myfile.tmp', 'myfile');
} else {
unlink('myfile.tmp');
}

Is there a way to remove a whole line in a text file if the word is found in python

Use a condition to check if itemRemove is in line or not

with open('CurrentStock.txt', "w") as f:
for line in lines:
if itemRemove not in line:
f.write(line)

How to replace an entire line in a text file by line number

Not the greatest, but this should work:

sed -i 'Ns/.*/replacement-line/' file.txt

where N should be replaced by your target line number. This replaces the line in the original file. To save the changed text in a different file, drop the -i option:

sed 'Ns/.*/replacement-line/' file.txt > new_file.txt

Find a word and replacing entire line in a text file

Assuming that your output filename is not the same as your input filename

If you are replacing the whole line, then you can simply just write text_to_replace to the file with a newline character.

char buffer[512];
while (fgets(buffer, sizeof(buffer), input) != NULL)
{
static const char text_to_find[] = "Applicat:";
static const char text_to_replace[] = "Applicat: ft_link\n"; // Added newline
char *pos = strstr(buffer, text_to_find);
if (pos != NULL)
{
fputs(text_to_replace, output);
}
}

Replace whole line containing a string using Sed

You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed:

sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo

How to find a character and replace the entire line in a text file

First you need to read each line of the text file, like this:

For Each line As String In System.IO.File.ReadAllLines("PathToYourTextFile.txt")

Next

Next, you need to search for the string you want to match; if found, then replace it with replacement value, like this:

Dim outputLines As New List(Of String)()
Dim stringToMatch As String = "ValueToMatch"
Dim replacementString As String = "ReplacementValue"

For Each line As String In System.IO.File.ReadAllLines("PathToYourTextFile.txt")
Dim matchFound As Boolean
matchFound = line.Contains(stringToMatch)

If matchFound Then
' Replace line with string
outputLines.Add(replacementString)
Else
outputLines.Add(line)
End If
Next

Finally, write data back to a file, like this:

System.IO.File.WriteAllLines("PathToYourOutputFile.txt", outputLines.ToArray(), Encoding.UTF8)


Related Topics



Leave a reply



Submit