Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting lines from a file with binary pattern strings

Tags:

grep

bash

shell

awk

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
like image 907
César A Avatar asked May 30 '18 18:05

César A


2 Answers

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.

like image 184
codeforester Avatar answered Oct 22 '22 13:10

codeforester


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
like image 3
Ed Morton Avatar answered Oct 22 '22 13:10

Ed Morton