Remove occurrences of string in text file
sed -i -e 's/goodbye//g' filename
To delete multiple words:
sed -i -e 's/\(goodbye\|hello\|test\|download\)//g' filename
How to remove specific string occurences from file?
Try this:
sed -i -e 's| \* Running on http://0\.0\.0\.0:80/ (Press CTRL+C to quit)||' filename
And usually when you are toying with this, don't add -i
yet, so just:
sed -e 's| \* Running on http://0\.0\.0\.0:80/ (Press CTRL+C to quit)||' filename
When it's working, then add the -i
to change file(s) inplace.
The mechanism is substitute by regex
.
And many characters have special meanings in regex
so you have to escape it.
The character after s
will act as the separator, s#From#to#
like this.
And you might want to add g
flag to replace multiple occurances. s$from$to$g
or so.
And in your case there're many /
so might as well change it to another, |
in above examples.
How to remove a particular string in a text file using java?
First, you'll need to replace the line with the new string List item
not an empty string. You can do that using line = line.replace(delete, "List item");
but since you want to replace end
only when it is the only string on a line you'll have to use something like this:
line = line.replaceAll("^"+delete+"$", "List item");
Based on your edits it seems that you indeed what to replace the line that contains end
with an empty string. You can do that using something like this:
line = line.replaceAll("^"+delete+"$", "");
Here, the first parameter of replaceAll
is a regular expression, ^
means the start of the string and $
the end. This will replace end
only if it is the only thing on that line.
You can also check if the current line is the line you want to delete and just write an empty line to the file.
Eg:
if(line.equals(delete)){
writer.println();
}else{
writer.println(line);
}
And to do this process for multiple strings you can use something like this:
Set<String> toDelete = new HashSet<>();
toDelete.add("end");
toDelete.add("something");
toDelete.add("another thing");
if(toDelete.contains(line)){
writer.println();
}else{
writer.println(line);
}
Here I'm using a set of strings I want to delete and then check if the current line is one of those strings.
How to replace all instances of a string in text file with Python 3?
Try this:
# Make sure this is the valid path to your file
file = input("Enter a filename: ")
remove = input("Enter the string to be removed: ")
# Read in the file
with open(file, "r") as file:
filedata = file.read()
# Replace the target string
filedata = filedata.replace(remove, "")
# Write the file out again
with open(file, "w") as file:
file.write(filedata)
Note: You might want to use with open
syntax, the benefit is elaborated in this answer by Jason Sundram.
Remove all occurrences in a text file with batch script
@ECHO OFF
SETLOCAL
:: establish a zero-byte output file
SET "newfile=u:\newfile.txt"
COPY /y nul "%newfile%" 2>NUL >nul
FOR /f "delims=" %%a IN (q33645665.txt) DO (
FINDSTR /x /i /l /c:"%%a" "%newfile%" >NUL
IF ERRORLEVEL 1 >>"%newfile%" ECHO %%a
)
TYPE "%newfile%"
GOTO :EOF
I used a file named q33645665.txt
containing your data for my testing.
Produces u:\newfile.txt
FINDSTR
will set errorlevel
to 0
if a string matching %%a
is found in the new file. /x
forces the match to be on the entire line, /i
specifies that the match be case-insensitive. /l
makes the match literal, not a regular expression and /c:
preceding a string specifies that the string provided is one element, not a set of strings so that a string containing separators liek spaces can be matched.
Remove string from txt file
Read the text file get the content in a variable then use String.replace
For example
var stringContent="Here goes the content of the textFile after read"
var res=stringContent.replace('string3','');
//write back res to the file or do what ever
Related Topics
Why Can't We Use C Standard Library Functions in Kernel Development
Redirecting Man Page Output to File Results in Double Letters in Words
Rename Multiple Directories Matching Pattern
Output the 2Nd Column of a File
Determine Usb Device File Path
Is a Core Dump Executable by Itself
Object-Oriented Shell for Linux
How to Redirect Nohup Output to a Specified File
How to Write-Protect Every Page in the Address Space of a Linux Process
Go Http Server Testing Ab VS Wrk So Much Difference in Result
Shell Script Calls Sudo; How to Suppress the Password Prompt
Arm Linux Atags VS Device Tree
Install.Packages("Devtools") on R 3.0.2 Fails in Ubuntu 14.04
Shell Script to Copy and Prepend Folder Name to Files from Multiple Subdirectories
Start Rails Server Automatically When Ever I Start My Ubuntu MAChine