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

Hi All,

I am beginner in Perl Programming and I need your help to fullfill this requirement Below is the requirement

1. I have extracted some of the data from text file (as mentioned below).
2. I need to get List NAME called CT08LIST name and its group name starts with X (ex:XCT08L1) in another file. (Group name has MAPSETS and TRASACTIONS, it should come in another file)
3. I have below code which will get group name.
LIST NAME: CT08LIST ---------- ELEMENT NAME DATE ADDED ------- ---- ---------- GROUP DCT08L1 04.290 17:05 GROUP CT08CONN 04.290 17:05 GROUP TCLCT08 04.290 17:05 GROUP LSRCT08 04.290 17:05 GROUP FCT08L1 04.290 17:05 GROUP FCT08R1 04.290 17:05 GROUP XCT08L1 04.290 17:05 GROUP PCT08L1 04.290 17:05 GROUP JN22 04.290 17:05 GROUP LN08STAN 04.290 17:05 GROUP LNPCMI08 04.290 17:05 GROUP LN47SBBH 04.290 17:05 GROUP CT08CSMI 04.290 17:05 GROUP CT08 04.290 17:05 GROUP PCT08R1 04.290 17:05 GROUP RCT08L1 04.290 17:05 GROUP CT08NAM 04.290 17:05 GROUP RLSFILES 08.235 15:43 GROUP RLSPMM 08.299 17:35 GROUP ABEND50 11.035 20:41 GROUP CT08MQ 12.287 11:37
LIST NAME: CT09LIST ---------- GROUP NAME: XCT08L1 ----------
MAPSETS: AGCEM1 AGHEM1 AGMMM1 AGONM1 AGPEM1 + AGPUM1 AGREM1 AGRUM1 AGSEM1 AGSUM1 AGZ1M1 AGZ2M1 AGZ3M1 AGZ4M1 + AGZ5M1 TRANSACTIONS: ACBA ACBE ACBL ACBR ACBU + AGMM AGSU AGTO AST5 AST7 AST9 AXTO BAC1 BCA1 + BCA2 BCA3 BCA4 BCA5 BD11 BE1A BE44 BM16 BM17 + BM6A BSL3 BSVL BS09 BS11 BS12 BS13 BS14 BTM2 + BWR2 HMI8 IBVS IM01 IM02 IM03 IM07 IM18 IM21 + IM25 MSOD NPS2 STP1 XB01 XB02 XB03 XB04 XB05 + XB06 XB07 XB08 XB10 XB11 XB12 GROUP NAME: XCT09L1

Replies are listed 'Best First'.
Re: Perl Script to extract pattern and redirect to another file
by 2teez (Vicar) on Oct 09, 2013 at 19:56 UTC

    Hi Raghu1
    Hi All, I am beginner in Perl Programming and I need your help to fullfill this requirement
    Welcome to the Monastery, we are also very willing to help but
    1. Read How do I post a question effectively?
    2. You need to reformat your questions using <code></code> tags

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
      Hi All,

      The below code will extract List Name and associated Group Name(starts with X).

      <code>

      #!/usr/bin/perl

      use strict;

      use warnings;

      # open for reading

      my $CSD_file = "CSD.txt";

      open FD, "<", $CSD_file

      or die "Unable to open '$CSD_file' for reading because $!";

      # open for writing

      my $RES_file = "RES.txt";

      open NW, ">", $RES_file

      or die "Unable to open '$RES_file' for writing because $!";

      my %data;

      {

      local $/ = ''; # read in paragraph modes, in chunks

      while (<FD>) { # read a chunk

      if (/(LIST NAME:\s+(.+)LIST)/) {

      $data{$2} = $1;

      }

      elsif (/(GROUP NAME:\s+X(\w+)/) {

      print NW $data{$2}, "\n";

      print NW "$1\n";

      my $trans = <FD>; # read next 'chunk'

      print NW $trans;

      } } }

      close FD or die $!;

      close NW or die $!;

      I am trying to extract "Group Name" which has 4 to 8 charcters/digit. This Group Name is combinaion of charcters/digits.

      Ex:XCT09L1,XPCPHL2, XDX12DN1

      How can I achieve this in above code snippet

Re: Perl Script to extract pattern and redirect to another file
by parv (Parson) on Oct 09, 2013 at 19:52 UTC

    There is no program|code in your listing; it has only the format of files which itself cannot be easily read.

    What had you tried? Or, got a plan in terms of pseudo code?

      Hi, I am really sorry for inconvenience. I tried uploading the data in correct format, while uploading by default its merging the content. Here is the code I am trying out : In this code when I run the script, It will display the Group name which starts with X.
      my $str = ""; open (FD, "<", "CSD.txt"); my @lines = <FD>; close FD; foreach (@lines) { if(/GROUP NAME: X[A-Z]../) { print STDOUT $_; $str= $str.$_; open (NW, ">", "RES.txt"); print NW $str; close NW; } }
      My requirement is to get the List name which has Group name starts with X. I need to extract Transactions code which comes under Group name as well. The output should be
      List name: CT08LIST GROUP XCT08L1 04.290 17:05 Trasactions ROUP NAME: XCT08L1 ---------- MAPSETS: AGCEM1 AGHEM1 AGMMM1 AGONM1 AGPEM1 + AGPUM1 AGREM1 AGRUM1 AGSEM1 AGSUM1 AGZ1M1 AGZ2M1 AGZ3M1 AGZ4M1 + AGZ5M1 TRANSACTIONS: ACBA ACBE ACBL ACBR ACBU + AGMM AGSU AGTO AST5 AST7 AST9 AXTO BAC1 BCA1 + BCA2 BCA3 BCA4 BCA5 BD11 BE1A BE44 BM16 BM17 + BM6A BSL3 BSVL BS09 BS11 BS12 BS13 BS14 BTM2 + BWR2 HMI8 IBVS IM01 IM02 IM03 IM07 IM18 IM21 + IM25 MSOD NPS2 STP1 XB01 XB02 XB03 XB04 XB05 + XB06 XB07 XB08 XB10 XB11 XB12 GROUP NAME: XCT09L1 ----------
      I can send the input file wherein you can have a look on the file. Kindly let me know your mail id so that I can send the input file/exact requirement

        If you can't be bothered to format your post so that we can read it, why do you think we will be bothered to solve your problem for you?

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Perl Script to extract pattern and redirect to another file (fixed width report)
by Anonymous Monk on Oct 10, 2013 at 01:44 UTC