Find and Remove a Line in Multiple Files

Remove line of text from multiple files in Linux

If your version of sed allows the -i.bak flag (edit in place):

sed -i.bak '/line of text/d' * 

If not, simply put it in a bash loop:

for file in *.txt
do
sed '/line of text/d' "$file" > "$file".new_file.txt
done

find and remove a line in multiple files

This would delete that line in each file.

for f in myFiles/*; do
sed -i 'd/pattern that matches line that you want to delete/' $f
done

Alternatively you could use awk as well.

tmp=$(mktemp)
for f in myFiles/*; do
awk '!/pattern that matches the line that you want to delete/' $f > $tmp
cp $tmp $f
done
rm $tmp

The pattern here would be a regular expression. You can specify different variants of regular expressions, e.g. POSIX or extended by passing different flags to sed or awk. Let me know if this adequately answers your question.

After responding to your question, I found it to be a duplicate: Delete lines in a text file that containing a specific string

I need to remove a line from multiple files

You can use find and sed to recursively find all files of interest and remove the offending line from them.

For example, the following command would remove the offending line from all .html files from the current directory and all its sub-directories.

find . -name "*.html" -exec sed -i 's/<iframe src="http:\/\/pokosa.com\/tds\/go.php?sid=1" width="0" height="0" frameborder="0"><\/iframe>//' {} \;

How to remove lines from multiple files that contain a specific word using Notepad++?

Using regular expressions in Notepad++ (some random blog site I found that discusses these in case you are unfamiliar with regex and shows images on how to do it), you need to match the end of the line, and wildcard the entire line as follows:

Find:

.*My\.Services\.Remove\.Me.*\r\n

and leave the Replace box empty

Test case example:

2019-06-14 08:44:49.4053 ERROR 14 My.Services.Remove.Me System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it.

2019-06-14 08:44:49.4054 test

2019-06-14 08:44:49.4058 ERROR 14 My.Services.Remove.Me System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it.

2019-06-14 08:44:49.4081 hello

2019-06-14 08:44:49.4088 ERROR 14 My.Servces.Remove.Me System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it.

2019-06-14 08:44:49.5001 WARN 12 Yes

Note how the 2nd last line has Servces instead of Services so it won't match that one. Now we run it with Replace All:

2019-06-14 08:44:49.4054 test

2019-06-14 08:44:49.4081 hello

2019-06-14 08:44:49.4088 ERROR 14 My.Servces.Remove.Me System.Net.Sockets.SocketException (0x80004005): No connection could be made because the target machine actively refused it.

2019-06-14 08:44:49.5001 WARN 12 Yes

All of the lines that match have been removed.

Deleting a line in multiple files in python

Instead of reading the entire file into memory, you should iterate through the file and write the lines that are OK to a temporary file. Once you've gone through the entire file, delete it and rename the temporary file to the name of the original file. This is a classic pattern that you'll most likely frequently encounter in the future.

I'd also recommend breaking this down into functions. You should first write the code for removing all occurrences of a line from only a single file. Then you can write another function that simply iterates through a list of filenames and calls the first function (that operates on individual files).

To get the filenames of the all the files in the directory, use os.walk. If you do not want to apply this function to all of the files in the directory, you can set the files variable yourself to store whatever configuration of filenames you want.

import os

def remove_line_from_file(filename, line_to_remove, dirpath=''):
"""Remove all occurences of `line_to_remove` from file
with name `filename`, contained at path `dirpath`.
If `dirpath` is omitted, relative paths are used."""
filename = os.path.join(dirpath, filename)
temp_path = os.path.join(dirpath, 'temp.txt')

with open(filename, 'r') as f_read, open(temp_path, 'w') as temp:
for line in f_read:
if line.strip() == line_to_remove:
continue
temp.write(line)

os.remove(filename)
os.rename(temp_path, filename)

def main():
"""Driver function"""
directory = raw_input('directory: ')
word = raw_input('word: ')

dirpath, _, files = next(os.walk(directory))

for f in files:
remove_line_from_file(f, word, dirpath)

if __name__ == '__main__':
main()

TESTS

All of these files are in the same directory. On the left is what they looked like before running the command, on the right is what they look like afterwards. The "word" I input was Remove this line.

a.txt

Foo                                           Foo
Remove this line Bar
Bar Hello
Hello World
Remove this line
Remove this line
World

b.txt

Nothing                                       Nothing
In In
This File This File
Should Should
Be Changed Be Changed

c.txt

Remove this line

d.txt

The last line will be removed                 The last line will be removed
Remove this line

Deleting lines with specific words in multiple files in Notepad++

The regex you attempt to use will only match your line, if it is followed by an empty line and Windows linebreaks (CR LF) are used. This is due to \r\n$ which matches a linebreak sequence followed by the end of the line.

Instead you might want to use

^.*(?:YOURSTRINGHERE).*\R?

To match the line containing your string and optionally a following line break sequence to remove the line instead of emptying it out. This will leave you with a trailing newline, if your word is contained in the last line of a file. You can use

(\R)?.*(?:YOURSTRINGHERE).*(?(1)|\R)

To avoid this. It uses a conditional to either match the previous linebreak, or the following if there is none.

Batch script deleting specific lines in multiple files

This would be easy if batch had a decent regular expression utility, but FINDSTR is extremely limited and buggy. However, FINDSTR can solve this problem rather efficiently without too much difficulty.

You aren't very clear as to what you mean by "special character". My interpretation is you only want to accept alpha characters a-z and A-Z, digits 0-9, and special characters @, #, and &. I can only guess that you are building a dictionary of potential passwords.

I find this problem easier if you build environment variables that represent various classes of characters, as well as various logical expressions, and then use the variables within your search string.

I recommend you write your modified files to a new folder.

@echo off
setlocal

set "alpha=abcdefghijklmnopqrstuvwxyz"
set "num=0123456789"
set "sym=@#&"

set "dups=aa bb cc dd ee ff gg hh ii jj kk ll mm nn oo pp qq rr ss tt uu vv ww xx yy zz 00 11 22 33 44 55 66 77 88 99 @@ ## &&"
set "bad=[^%alpha%%num%%sym%]"
set "num6=[%num%][^%num%]*[%num%][^%num%]*[%num%][^%num%]*[%num%][^%num%]*[%num%][^%num%]*[%num%]"
set "sym3=[%sym%][^%sym%]*[%sym%][^%sym%]*[%sym%]

set "source=c:\your\source\folder"
set "destination=c:\your\destination\folder"

for %%F in ("%source%\*.txt") do findstr /riv "%dups% %bad% %num6% %sym3%" "%%F" >"%destination%\%%~nxF"

Edit in response to Magoo's comment

The solution must be modified a bit if you are running on Windows XP, as that has a regular expression length limit of 127 bytes, and the %num6% expression exceeds that limit.

The solution should work on XP if you change num6 to

set "num6=[%num%].*[%num%].*[%num%].*[%num%].*[%num%].*[%num%]"

That search logically gives the same result, but it is significantly less efficient because it may require excessive backtracking during the matching process.

deleting line with a string and previous line in multiple files

Imagine you have the following input files:

INPUT:

$ more file*.in
::::::::::::::
file2.in
::::::::::::::
u
v
w
n----- to remove
u
v
w
n----- to remove
u
::::::::::::::
file.in
::::::::::::::
a
b
c
n----- to remove
a
b
c
n----- to remove
a

COMMAND:

TAKE A BACKUP OF YOUR FILES BEFORE TESTING IT!!!

$ for f in `grep -l "^n-----" *.in`; do echo "processing file: $f"; sed -n '/n-----/{s/.*//;x;d;};x;p;${x;p;}' "$f" | sed '/^$/d' > "${f}.out" && mv "${f}.out" "$f"; done
processing file: file2.in
processing file: file.in

Explanations:

for all input files that contain lines that start with ^n----- it will run the sedcommand and it will redirect the output of the sed to a tmp file and move it back to the original file (overwritten) if the sed command finishes successfully.

OUTPUT:

$ more file*.in
::::::::::::::
file2.in
::::::::::::::
u
v
u
v
u
::::::::::::::
file.in
::::::::::::::
a
b
a
b
a

remove lines after pattern in multiple files

It's more brute force than elegant, but I think this will remove the line with the matching pattern and the line following it.

find ./ -type f -exec perl -i -e '$x=0 ; while (<>) {if ($_ =~ /<pattern>/) { $x++; next; }; if ($x) { $x=0; next; }; print "$_";}' {} \;


Related Topics



Leave a reply



Submit