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 612013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:50:49+00:00 2026-05-13T17:50:49+00:00

This question has 2 sections one for single line match and one for multi

  • 0

This question has 2 sections one for “single line match” and one for “multi line region matching” Also, I have a semi working solution, I want to find more robustness and elegance in my solution.

  1. Single Line Match:
    I would like to duplicate each line of an input file such that the second line was a regex modification of the first:
    E.G.

File.txt

YY BANANA, YYZ, ABC YHZ YY1
YY APPLE , YYZ, ABC YHZ YY1
YY ORANGE, YYZ, ABC YHZ YY1
YZ GRAPE , YZZ, ABC YHZ YZ1

Would BECOME:

YY BANANA, YYZ, ABC YHZ YY1
XY BANANA, XYZ, ABC YHZ XY1
YY APPLE , YYZ, ABC YHZ YY1
XY APPLE , XYZ, ABC YHZ XY1
YY ORANGE, YYZ, ABC YHZ YY1
XY ORANGE, XYZ, ABC YHZ XY1
YZ GRAPE , YZZ, ABC YHZ YZ1
XZ GRAPE , XZZ, ABC YHZ XZ1

Keep in mind the real file is large, and The example of YY ->XY and YZ ->XZ
is exactly correct In other words in my file case YY, YH, YZ, Y1, Y2, Y3 are the
symbols that I would like to change to XY, XH, XZ, X1, X2, X3.

I have done something in PERL that is very raw ( will create a link to it as
as starting point to show What I was thinking)
But the perl script I wrote is not elegant or general and requires multiple
passes over the file.

My Raw Stab…. IN PERL.
http://www.quantprinciple.com/invest/index.php/docs/tipsandtricks/perl-sed-awk/conditional-duplicate/

Usage of my raw stab:

MatchDuplicate.pl  INPUT.txt YY XY > INPUT2.txt
MatchDuplicate.pl  INPUT2.txt YH XH > INPUT3.txt
MatchDuplicate.pl  INPUT3.txt Y1 X1 > INPUT4.txt
MatchDuplicate.pl  INPUT4.txt Y2 X2 > INPUT5.txt

INPUT5.txt is used…

  1. Multi Line Match
    Exactly the same as above, but each “record” of the input will match multiple lines:

File.txt

< some starting marker...startRecord:>
data
data
YY data
YY BANANA, YYZ, ABC YHZ YY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
YY data
YY APPLE , YYZ, ABC YHZ YY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
YY data
YY ORANGE, YYZ, ABC YHZ YY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
YZ data
YZ GRAPE , YZZ, ABC YHZ YZ1
<some ending record marker>

Would BECOME:

< some starting marker...startRecord:>
data
data
YY data
YY BANANA, YYZ, ABC YHZ YY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
XY data
XY BANANA, XYZ, ABC YHZ XY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
YY data
YY APPLE , YYZ, ABC YHZ YY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
XY data
XY APPLE , XYZ, ABC YHZ XY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
YY data
YY ORANGE, YYZ, ABC YHZ YY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
XY data
XY ORANGE, XYZ, ABC YHZ XY1
<some ending record marker>
< some starting marker...startRecord:>
data
data
YZ data
YZ GRAPE , YZZ, ABC YHZ YZ1
<some ending record marker>
< some starting marker...startRecord:>
data
data
XZ data
XZ GRAPE , XZZ, ABC YHZ XZ1
<some ending record marker>

My Raw Stab:
http://www.quantprinciple.com/invest/index.php/docs/tipsandtricks/perl-sed-awk/multi-line-conditional-duplicate/

  • 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-05-13T17:50:50+00:00Added an answer on May 13, 2026 at 5:50 pm

    For 1:

    while(<>) {
        say $_;
        say $_ if s/$pattern/$replacement/;
    }
    

    Add file handles and other boilerplate as appropriate.

    EDIT: Let’s go for something a bit more general then.

    First, we’ll parse out our command-line arguments, and put our replacements into a hash:

    $filename = shift @ARGV;
    %patterns = ();
    while (scalar @ARGV) {
        my $pattern = shift @ARGV;
        my $replacement = shift @ARGV;
        $patterns{$pattern} = $replacement
    }
    

    Then for each line in the file, we’ll output the line verbatim, and then see if it matches any of our patterns.

    while (<>) {
        say $_;
        while (my ($pattern, $replacement) = each %patterns) {
            s/$pattern/$replacement/g and say $_ if /^$pattern/;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my question. I have this model one [Event] has multiple [Day] and
This problem has kept me busy for the last hours. I have two sections
This question: How to de-interleave bits (UnMortonizing?) has a good answer for extracting one
This question has developed off an answer here . My question therefore is what
This question has two parts. Part 1. Yesterday I had some code which would
This question has been asked before but i still don't understand it fully so
this question has probably been asked before, but i'm stuck and i've tried a
This question has baffled myself and my cohorts. In the program I had written
This question has disturbed me for a long time. Sorry if it is a
This question has been bugging me for a long time now but essentially I'm

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.