Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8932361
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:18:30+00:00 2026-06-15T09:18:30+00:00

I am currently attempting to merge two different text files in a perl script

  • 0

I am currently attempting to merge two different text files in a perl script – However its a bit more complicated than that.

The problem (slightly altered for the sake of easy explanation):

I have two different text files, one named dog1.txt and one named dog2.txt (as shown below).

dog1.txt
    poodle     8888
    jackrussel    5743
    beagle     6784

dog2.txt 
    spaniel    9843
    poodle    3756
    germanshepard    3267
    beagle    3478  

As you can see poodle and beagle are contained in both text files, but with different four digit codes associated with them.

What I want is a new file created that merges these two files together AND if there are any duplicates, such as poodle and beagle, I want the new file to contain the four digit associated with poodle and beagle to come from the dog1.txt file instead of dog2.txt.

The new file would need to look like this (it does not matter about the order of the dog names, its the numbers associated with them that need to be correct):

final_dog.txt
    poodle    8888
    germanshepard    3267
    jackrussel    5743
    beagle    6784
    spaniel    9843

I have tried many varying solutions, but none reliably work the way I need it to.

Any help is greatly appreciated, thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:18:31+00:00Added an answer on June 15, 2026 at 9:18 am

    You basically want to print the first instance you encounter. As such, you can use the standard idiom for removing duplicates.

    perl -lane'print if !$seen{$F[0]}++' dog1.txt dog2.txt >final_dog.txt
    

    This approach uses the least amount of memory. It also starts producing output the earliest possible (useful if you’re piping the output).


    To meet the new requirement, use

    perl -lane'print if @F==2 && $F[1]=~/^\d+\z/ && !$seen{$F[0]}++' \
       dog1.txt dog2.txt >final_dog.txt
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How do I force a 32-bit build of Boost with GCC? Currently attempting by
so currently what I am attempting to do is, remote onto a different server,
I am currently having problems with attempting to style the HTML rich text editor
I'm currently attempting to generate thumbnails of PDFs using Ghostscript (or more specifically GhostscriptSharp
I am currently attempting to create a bash script that will check inside of
I am currently attempting to read in Hex values from a text file. There
I am currently attempting to make a bash script that executes a Java main
I am currently attempting to create a Perl webspider using WWW::Mechanize. What I am
I am currently attempting to write a Script Writer along the lines of FinalDraft
While attempting to find a way to merge existing PDF files and forms, I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.