saro has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am novice to Perl. I have below usecase and i need to print certain specific lines from the file.
DATA.TXT ============== ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELEVE 12-MAR-2020 /net/slcnas22/scratch/domain_name +http://testurl/home username_pwd THIRTEEN FOURTEEN FIFTEEN SIXTEEN SEVENTEEN EIGHTEEN NINETEEN TWENTY

I have above file - data.txt. In that file, i need to find the string name "NINE" and need to print that string along with its next subsequent 8 strings.

Expected output:NINE TEN ELEVEN TWELEVE 12-MAR-2020 /net/slcnas22/scratch/domain_name http://testurl/home username_pwd

Replies are listed 'Best First'.
Re: find the string in the file and print its subsequent strings
by Corion (Patriarch) on Apr 20, 2020 at 08:30 UTC

    To better help you, it helps us to see your code.

    Can you please post the code you have and tell us how that code fails to do what you need?

Re: find the string in the file and print its subsequent strings
by Discipulus (Canon) on Apr 20, 2020 at 15:37 UTC
    Hello saro and welcome to the monastery and to the wonderful world of Perl!

    to make a program you must have a plain solution to then translate it into a program. Let's see:

    open a file named DATA.TXT read all lines of this file if the string 'NINE' is found save the whole string for later use -or (not clear in your question)- if the string 'NINE' is found split this string into 8 parts close the file print the result

    useful reads:

    Try some code on your own and post your code and questions here.

    Dont forget to start your program with:

    use strict; use warnings;

    You can also add use diagnostics; after the above lines to get more help from Perl itself

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

      Thanks for this inputs. I will try my own code and learn from there

Re: find the string in the file and print its subsequent strings
by kcott (Archbishop) on Apr 21, 2020 at 06:38 UTC

    G'day saro,

    Welcome to the Monastery.

    I suspect there's more to this than you're telling us; however, based on what you have provided, I'd probably aim for a technique such as this:

    #!/usr/bin/env perl use strict; use warnings; my $flag = 'NINE'; while (<DATA>) { print if (split)[0] eq $flag; } __DATA__ DATA.TXT ============== ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE TEN ELEVEN TWELEVE 12-MAR-2020 /net/slcnas22/scratch/domain_name +http://testurl/home username_pwd THIRTEEN FOURTEEN FIFTEEN SIXTEEN SEVENTEEN EIGHTEEN NINETEEN TWENTY

    Output:

    NINE TEN ELEVEN TWELEVE 12-MAR-2020 /net/slcnas22/scratch/domain_name +http://testurl/home username_pwd

    I see that ++Discipulus has provided a number of good documentation links; although, this technique does not require chomp nor does it use regexes. I also suggest you look at the autodie pragma: I highly recommend it.

    — Ken

Re: find the string in the file and print its subsequent strings
by AnomalousMonk (Archbishop) on Apr 20, 2020 at 21:01 UTC

    To further clarify, what should the output be from lines like

    NINETY ALANINE one two three four five six seven xx yy NINE one two three four five six seven xx yy NINE one NINE three four five six seven NINE one two three four five six seven eight nueve ten

    Also, I tend to think of a line read from a file as a single string. You seem to be referring to the non-whitespace substrings of your example strings as "strings". Do you want the non-whitespace substrings of a line containing your target string to be split out as an array or list of individual strings, or is it sufficient to simply return the whole line?

    In addition to the documentation links already given, please see perlre, perlretut, and perlrequick, and Short, Self-Contained, Correct Example and maybe How to ask better questions using Test::More and sample data.


    Give a man a fish:  <%-{-{-{-<

Re: find the string in the file and print its subsequent strings
by BillKSmith (Monsignor) on Apr 20, 2020 at 14:31 UTC
    Hint: Slurp the whole file into a string (See $/ in Special_Variables). Extract all your data with a single regex. (note: your example shows only seven fields, not eight) If you are still stuck, show us what you have.
    Bill