Unconfigured Ad

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • John.Mar
    Junior Member
    • Oct 2012
    • 2

    #1

    Compare Fields in 2 Different Files

    Hi all,
    I'm trying to compare between 2 files in the following manner: if a string in file 1 column 1 is found in file 2 columns 2 OR 3, then print the line from file 2 where a match was found. The catch is that I'm not looking for an exact match- I've found many solutions which solve that.
    Example:
    Code:
    file1.txt
    23
    1444
    223
    89
    Code:
    file2.txt
    1,23,34545
    1,3223,343433
    344,4345,989
    12,15,632
    Code:
    expectedoutput.txt
    1,23,34545
    1,3223,343433
    344,4345,899
    The solutions I've found (no good) would yield the following output:
    Code:
    1,23,34545
    A few clarifications:
    1. The fields will always be numbers.
    2. The values in file1 can be of any length.
    3. The values in file2 can be of any length.
    4. The value from file 1 which I'm searching file 2 for can be in any part of column 2 or 3- beginning, middle, or end.
    5. File 1 contains roughly 50,000 values, file 2 can contain 4 million +

    Thanks a lot in advance!
  • rflrob
    Member
    • May 2010
    • 50

    #2
    In python, I think I'd do something like the following:

    Code:
    f1_name = 'file1.txt'
    f2_name = 'file2.txt'
    f1s = [line.strip() for line in open(f1_name)]
    
    for line in open(f2_name):
        col2, col3 = line.strip().split(',')[1:3]
        for entry in f1s:
            if entry in col2 or entry in col3:
                print line
                break # breaks out of the inner for loop
    It's not super efficient, but if you're only doing it a handful of times, probably not that bad.

    Comment

    • vmrosario
      Junior Member
      • Oct 2012
      • 5

      #3
      bash is also an option

      while read line
      do
      grep "$line" file2.txt
      done < file1.txt

      Comment

      • John.Mar
        Junior Member
        • Oct 2012
        • 2

        #4
        Hi all,

        Thanks for the replies, I haven't tested them out yet.
        I ended up using fgrep -f file1 file2
        Works perfectly, I'll try the offered solutions in a bit

        Comment

        Latest Articles

        Collapse

        • SEQadmin2
          Beyond CRISPR/Cas9: Understand, Choose, and Use the Right Genome Editing Tool
          by SEQadmin2



          CRISPR/Cas9 sparked the gene editing revolution for both research and therapeutics.1 But this system still showed severe issues that limited its applications. The most prominent were the heavy reliance on PAM sequences, delivery limitations, double-stranded breaks that prompt unintended edits and cell death, and editing inefficiency (both in targeting and in knock-in reliability).

          Despite this, “CRISPR helped turn genome editing from a specialized technique into
          ...
          07-31-2026, 11:01 AM
        • SEQadmin2
          Proteomic Platforms: How to Choose the Right Analytical Strategy to Improve Detection and Clinical Applications
          by SEQadmin2


          Proteomics platforms are evolving rapidly, with advances in mass spectrometry and affinity-based approaches expanding what researchers can detect and at what scale. As the field moves toward deeper proteome coverage and clinical applications, scientists face an increasingly complex landscape of tools. This article will explore how researchers are navigating these choices to find the right platform for their work.

          The systematic characterization of the human proteome has
          ...
          07-20-2026, 11:48 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by SEQadmin2, Yesterday, 10:35 AM
        0 responses
        7 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 08-06-2026, 07:41 AM
        0 responses
        25 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 08-03-2026, 10:13 AM
        0 responses
        45 views
        0 reactions
        Last Post SEQadmin2  
        Started by SEQadmin2, 07-31-2026, 02:55 AM
        0 responses
        48 views
        0 reactions
        Last Post SEQadmin2  
        Working...