I've got two files. File A contains text written in N lines, and File B contains a binary pattern string of 0 and 1 that has N length too.
I want to delete the lines from File A that has the same line number that the one on File B that contains a 0.
I've read that it might be a good idea to do it with awk, but I don't have any idea of how to use it.
Files are very long, like 2000 lines for example (they are video traces)
For example:
File A:
Line 1: 123456
Line 2: 789012
Line 3: 345678
Line 4: 901234
File B:
Line 1: 1
Line 2: 0
Line 3: 0
Line 4: 1
After the execution:
File A:
Line 1: 123456
Line 2: 901234
You can use paste
and cut
for this:
paste fileB fileA | grep '^1' | cut -f2-
paste fileB fileA
- pastes file contents side by side, delimited by a tabgrep '^1'
- filters that lines that start with 1cut -f2-
- extracts the content that we needBoth cut
and paste
use tab as the default delimiter.
This is very similar to Benjamin's solution. A small advantage here is that it would work even if fileA were to have more than one field per line.
Assuming Line 1:
etc don't really exist in your input files all you need is:
awk 'NR==FNR{a[NR]=$0;next} a[FNR]' fileB fileA
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With