-1

I tried searching both google and the website for a similar problem but I couldn't really find what I was looking for. I have 2 files, both are filled only with numbers. I want to know how to compare FileB to the lines in FileA, and if a line in FileB isn't in FileA, simply ignore it. After comparing, I want to print only specific data from FileA. To further explain both are already sorted numerically:

FileA:

103445  -630
103572  -638
107586  -133
109754  -466
111900  -643
112699  -371
112991  -17
113863  -175
114189  -691

FileB:

101191  1136
103184  1322
103445  -210
103572  1424
104618  28
105395  1052

as you can see, lines 1-2 in B aren't in A, so I ignore them completely. I only want to save the lines with first 6 characters in B that are in A. Then I have some other commands to run but I think I know how to do those, just need to squeeze out the relevant data.

I tried using Comm but it didn't really provide the solution I was looking for...

Edit: I also have to keep the other column data, because my next step is to check if the value in col2 is equal or greater than half of the value in the corresponding like on FileB (abs val). I want to keep only those in the end. For example, after all the commands are executed I need it to print only col1 data from FileA that matches these criteria. In the current example above nothing will print out, but in my larger file with 1000's of line stuff will be printed.

  • 2
    Please post the expected output. – Satō Katsura Aug 30 '17 at 9:44
  • Edited to include expected output. Satō Katsura – Mor Elmaliach Aug 30 '17 at 10:12
  • Please post the expected output, so that people can compare results against it. – Satō Katsura Aug 30 '17 at 10:28
3
$ join FileA FileB | awk '{ x = $2; y = $3 } y < 0 { y = -y } x >= y/2 { print $1, $2 }'

Given the example data, the join will result in

103445 -630 -210
103572 -638 1424

where the 1st column is the join field, the 2nd column is from FileA and the last column is from FileB. This step relies on the two files being sorted.

The awk code will then pick out the values from the last two columns as x and y and negate y if it's negative. If x >= y/2 the first two column are outputted (these are from FileA).

The awk bit could be shortened to

awk '$3 < 0 { $3 = -$3 } $2 >= $3/2 { print $1, $2 }'

but that modifies $3 which may be unwanted if you later decide to output this value or $0.

  • 1
    Thanks a lot man! From here I can continue my solution how I intended to... Thanks again. – Mor Elmaliach Aug 30 '17 at 10:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.