Remove lines with specific line number specified in a file

Andrej

I have a text file A which contains line numbers which I want to remove from text file B. For example, file A.txt contains lines

1
4
5

and file B.txt contains lines

A
B
C
D
E

The resulting file should be:

B
C

Of course, this can be done manually with

sed '1d;4d;5d' B.txt

but I wonder how to do it without specifying line numbers manually.

αғsнιη

You can use awk as well:

awk 'NR==FNR { nums[$0]; next } !(FNR in nums)' linenum infile

in specific case when 'linenum' file might empty, awk will skip it, so it won't print whole 'infile' lines then, to fix that, use below command:

awk 'NR==FNR && FILENAME==ARGV[1]{ nums[$0]; next } !(FNR in nums)' linenum infile

or even better (thanks to Stéphane Chazelas):

awk '!firstfile_proceed { nums[$0]; next } 
     !(FNR in nums)' linenum firstfile_proceed=1 infile

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Replace with lines number in specific line of a text file

How to delete (remove) a specific line of a text file based on the line number?

How to sort a file with string lines by the number of a specific symbol in the line?

Remove lines by line number in python

Locate a specific line in a file based on user input then delete a specific number of lines

How can I remove a specific line from a file only if there are other lines in the file?

sed remove end of line for specific lines

How to remove lines with specific pattern and next line

Remove all lines after a specific line in Python

How to extract specific lines from a huge text file (>16GB) where each line starts with a string specified in another input file?

Apache Camel: Remove specific lines from a file without checking each line

Function to open a file and navigate to a specified line number

Run .py file until specified line number

How to get a number from a text file after a specific string in one of the lines somewhere on the line?

Bash - How to input lines in rst file after specific line number in bash script

How to remove specific number of line from existing file and create new text file

How to remove specific lines from a text file?

Remove specific lines in json file using python

remove lines that contain a specific text in a file

python remove specific lines from file

How to remove specific lines from a text file?

How to remove specific last two lines of a file?

How to remove specific lines from a file?

remove 0 number lines in txt file with python

Display all the lines and line number of the given file

Permanently delete a specific last number lines of a file

How to check if a specific number is present in the lines of a file?

Remove lines of a file after the N firsts lines but before to integer number

How to fill a file with a stream from /dev/urandom with a specified number of lines ?