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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:24:20+00:00 2026-06-08T08:24:20+00:00

File I want to parse: input Pattern; input SDF; input ABC input Pattern; output

  • 0

File I want to parse:

input Pattern;

input SDF;

input ABC

input Pattern;

output Pattern;

output XYZ;

In perl, usual operation is scan line by line.
I want to check that if
current line has output Pattern; and previous line (or all previous lines)has input Pattern;
then change all the previous lines matches to "input Pattern 2;" and current line to "output Pattern2;".

It is complicated ,I hope I have explained properly.
Is it possible in Perl to scan and change previous lines after they have been read?

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-08T08:24:21+00:00Added an answer on June 8, 2026 at 8:24 am

    If this is your data:

    my $sfile =
    'input Pattern;
    input SDF;
    input ABC
    input Pattern;
    output Pattern;
    output XYZ;' ;
    

    then, the following snippet will read the whole file and change text accordingly:

    open my $fh, '<', \$sfile or die $!;
    local $/ = undef;                # set file input mode to 'slurp'
    my $content = <$fh>;
    close $fh;
    
    $content =~ s{ (                   # open capture group
                    input \s+ (Pattern); # find occurence of input pattern
                    .+?                  # skip some text
                    output \s+ \2        # find same for output
                   )                   # close capture group
                 }
                 {                     # replace by evaluated expression
                  do{                    # within a do block
                     local $_=$1;        # get whole match to $_
                     s/($2)/$1 2/g;      # substitute Pattern by Pattern 2
                     $_                  # return substituted text
                    }                    # close do block
                 }esgx;
    

    Then, you may close your file and check the string:

    print $content;
    

    =>

    input Pattern 2;
    input SDF;
    input ABC
    input Pattern 2;
    output Pattern 2;
    output XYZ;
    

    You may even include a counter $n which will be incremented after each successful match (by code assertion (?{ ... }):

    our $n = 1;
    
    $content =~ s{ (                   # open capture group
                    input \s+ (Pattern); # find occurence of input pattern
                    .+?                  # skip some text
                    output \s+ \2        # find same for output
                    )                  # close capture group
                    (?{ $n++ })        # ! update match count 
                 }
                 {                     # replace by evaluated expression
                  do{                    # within a do block
                     local $_=$1;        # get whole match to $_
                     s/($2)/$1 $n/g;     # substitute Pattern by Pattern and count
                     $_                  # return substituted text
                    }                  # close do block
                 }esgx;
    

    The substitution will now start with input Pattern 2; und increment subsequently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse an input file that has syntax similar to c++ source.
I want to parse a large XML file and I have two options: Perl
I want to parse a line from a CSV(comma separated) file, something like this:
I want to parse the tabular information from a .pdf file,and want to display
I have a JSON file and want to parse value of variable second value
I want to parse the xml file with dynamic content using DOM parser in
I want to parse xml file in utf-8 and sort it by some field.
I want to parse a xml file using xquery in Ruby, I found this
I want to parse out each modified file that is reported during FCIV's verification
I want to parse the html file, pdf file, csv file and text file.Now

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.