Seqanswers Leaderboard Ad

Collapse

Announcement

Collapse
No announcement yet.
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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!

  • #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


    • #3
      bash is also an option

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

      Comment


      • #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

        • seqadmin
          Current Approaches to Protein Sequencing
          by seqadmin


          Proteins are often described as the workhorses of the cell, and identifying their sequences is key to understanding their role in biological processes and disease. Currently, the most common technique used to determine protein sequences is mass spectrometry. While still a valuable tool, mass spectrometry faces several limitations and requires a highly experienced scientist familiar with the equipment to operate it. Additionally, other proteomic methods, like affinity assays, are constrained...
          04-04-2024, 04:25 PM
        • seqadmin
          Strategies for Sequencing Challenging Samples
          by seqadmin


          Despite advancements in sequencing platforms and related sample preparation technologies, certain sample types continue to present significant challenges that can compromise sequencing results. Pedro Echave, Senior Manager of the Global Business Segment at Revvity, explained that the success of a sequencing experiment ultimately depends on the amount and integrity of the nucleic acid template (RNA or DNA) obtained from a sample. “The better the quality of the nucleic acid isolated...
          03-22-2024, 06:39 AM

        ad_right_rmr

        Collapse

        News

        Collapse

        Topics Statistics Last Post
        Started by seqadmin, 04-11-2024, 12:08 PM
        0 responses
        18 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 10:19 PM
        0 responses
        22 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-10-2024, 09:21 AM
        0 responses
        16 views
        0 likes
        Last Post seqadmin  
        Started by seqadmin, 04-04-2024, 09:00 AM
        0 responses
        47 views
        0 likes
        Last Post seqadmin  
        Working...
        X