Delete Line from Text File with Line Numbers from Another File

Delete line from text file with line numbers from another file

awk oneliner should work for you, see test below:

kent$  head lines.txt doc.txt 
==> lines.txt <==
1
3
5
7

==> doc.txt <==
a
b
c
d
e
f
g
h

kent$ awk 'NR==FNR{l[$0];next;} !(FNR in l)' lines.txt doc.txt
b
d
f
h

as Levon suggested, I add some explanation:

awk                     # the awk command
'NR==FNR{l[$0];next;} # process the first file(lines.txt),save each line(the line# you want to delete) into an array "l"

!(FNR in l)' #now come to the 2nd file(doc.txt), if line number not in "l",print the line out
lines.txt # 1st argument, file:lines.txt
docs.txt # 2nd argument, file:doc.txt

Delete specific line number(s) from a text file using sed?

If you want to delete lines from 5 through 10 and line 12th:

sed -e '5,10d;12d' file

This will print the results to the screen. If you want to save the results to the same file:

sed -i.bak -e '5,10d;12d' file

This will store the unmodified file as file.bak, and delete the given lines.

Note: Line numbers start at 1. The first line of the file is 1, not 0.

How to delete from a text file, all lines that contain a specific string?

To remove the line and print the output to standard out:

sed '/pattern to match/d' ./infile

To directly modify the file – does not work with BSD sed:

sed -i '/pattern to match/d' ./infile

Same, but for BSD sed (Mac OS X and FreeBSD) – does not work with GNU sed:

sed -i '' '/pattern to match/d' ./infile

To directly modify the file (and create a backup) – works with BSD and GNU sed:

sed -i.bak '/pattern to match/d' ./infile

Deleting lines from one file which are in another file

grep -v -x -f f2 f1 should do the trick.

Explanation:

  • -v to select non-matching lines
  • -x to match whole lines only
  • -f f2 to get patterns from f2

One can instead use grep -F or fgrep to match fixed strings from f2 rather than patterns (in case you want remove the lines in a "what you see if what you get" manner rather than treating the lines in f2 as regex patterns).

How to remove the lines which appear on file B from another file A?

If the files are sorted (they are in your example):

comm -23 file1 file2

-23 suppresses the lines that are in both files, or only in file 2. If the files are not sorted, pipe them through sort first...

See the man page here

Deleting a specific number of lines from text file using Python

EDIT: iterating directly over f

based in @Kenny's comment and @chepner's suggestion

with open("your_file.txt", "r") as f:
new_lines = []
for idx, line in enumerate(f):
if idx in [x for x in range(2,5)]: #[2,3,4]
new_lines.append(line)

with open("your_new_file.txt", "w") as f:
for line in new_lines:
f.write(line)

How to delete a specifil line by line number in a file?

One possible simple solution to remove/replace 5th line of file. This solution should be fine as long as the file is not too large:

fn = 'prova.js'
newdata = "mynewstring"
with open(fn, 'r') as f:
lines = f.read().split('\n')
#to delete line use "del lines[4]"
#to replace line:
lines[4] = newdata
with open(fn,'w') as f:
f.write('\n'.join(lines))

Delete all lines between two specific line numbers

It's easier than expected.

dellist = [3, 4, 7] #numbers of the lines to be deleted

with open('data.txt') as inpf:
with open('out.txt', 'w') as of:
for i, line in enumerate(inpf):
if i+1 not in dellist: #i+1 because i starts from 0
of.write(line)

You read each line, and if the line number is not in the list of prohibited lines, the line is written to another file.

So assuming your original input, the code above gives:

1
2
5
6
8
9
10

Note: here I called the file data.txt, it's better to use the .py extension for files with python code inside only.

sed delete lines from a logfile that respect numbers in another file

I think, what you really need is sed -i '/pattern/d' filename.

But to answer your question:

How to delete lines matching the line numbers from another file:

(Assuming that there are no special characters in the line_numbers file, just numbers one per line...)

awk 'NR==FNR{a[$0]=1; next}; !(FNR in a)' line_numbers input.log


Related Topics



Leave a reply



Submit