Deleting lines from a file with binary pattern strings

  • A+
Category:Languages

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 tab
  • grep '^1' - filters that lines that start with 1
  • cut -f2- - extracts the content that we need

Both 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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: