• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 199
  • Last Modified:

File Operation

I have a file like this
test.txt
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxx
-------My String-------------------
Some text @@ Some text
Some text @@ Some text
...

What  I am trying to do is  find the "My String"  and store reset of lines after "my string" in to one array .

another one is after doing the above operation i want separate the  before , After the string "@@" in to two arrays.


I unable to find do the first operation. can any body respond to me.



0
Test 2010
Asked:
Test 2010
1 Solution
 
sqd5597Commented:
The code below should do what you want...although it is by no means clean or elegant

use strict;
my $file;
my $line;
my $text;
my $found=0;
my $i=0;
my @data;
my @before;
my @after;

open FILE, "file1.txt";
$text="MY String";
while ($found==0){
  $line=<FILE>;
  if ($text =~ /$line/){
    $found=1;
  }
}
while ($line=<FILE>){
  chomp $line;
  $data[$i++]=$line;
}
for $i (@data) {
  ($before[$i],$after[$i])=split /\@\@/,$data[$i];
}
0
 
Adam314Commented:

open(my $In, "<file.txt") or die "file: $!\n";
local $/;
my $all=<$In>;
close($In);
 
$all =~ s/.*My String.*?$//sm;
my @lines = split(/\n/, $all);
 
#Then to seperate
my ($before, $after) = split(/\@\@/, $lines[1]);   #or whatever line, or loop through lines

Open in new window

0
 
ozoCommented:
@ARGV=("test.txt");
(/My String/..0) > 1 && push @array,$_ while <>;
/(.*)\@\@(.*)/ and push @before,$1 and push @after,$2 for @array;
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Test 2010Author Commented:
Hi
I am getting the following error while using the above code.
0
 
Test 2010Author Commented:
"Reference to nonexistent group in regex; marked by <-- HERE"
0
 
ozoCommented:
what was the part of the error that shows the regex marked by <-- HERE
0
 
Test 2010Author Commented:
Reference to nonexistent group in regex; marked by <-- HERE in m/.\lost+found\te
st.txt.d242129cd9f549e4b872d325ddf4ac03@@\main\testing\1 <-- HERE
/ at C:\Documents and Settings\pmokkarala\Desktop\Label.pl line 48, <FILE> line
21.

This is the exact error
0
 
ozoCommented:
why did you have
/.\lost+found\te
st.txt.d242129cd9f549e4b872d325ddf4ac03@@\main\testing\1/
in a regular expression?
None of the suggestions given had 48 lines.
what was your code?
0
 
Test 2010Author Commented:
use strict;
use warnings;
my $file;
my $line;
my $text;
my $found=0;
my $i=0;
my @data;
my @before;
my @after;

open FILE, "codechanges_tes.txt";
$text="Code Changes";
while ($found==0){
   $line=<FILE>;
  if ($text =~ /$line/){
    $found=1;
  }
}
while ($line=<FILE>){
  chomp $line;
   $data[$i++]=$line;
}
for $i (@data) {
        ($before[$i],$after[$i])=split /\@\@/,$data[$i];
}

i am using this  the error is hsowing at line  "if ($text =~ /$line/)" .

That is part of the file which i am using /.\lost+found\te
st.txt.d242129cd9f549e4b872d325ddf4ac03@@\main\testing\1/
0
 
ozoCommented:
sqd5597 probably meant to say
$line =~ /$text/

did you try http:#a21879009
0
 
Test 2010Author Commented:
ozo:
Can you explain the concept of your program.

@ARGV=("test.txt");
(/My String/..0) > 1 && push @array,$_ while <>;
/(.*)\@\@(.*)/ and push @before,$1 and push @after,$2 for @array;
0
 
ozoCommented:
@ARGV=("codechanges_tes.txt"); #file to read
(/Code Changes/..0) > 1 && push @array,$_ while <>; # store reset of lines after "Code Changes" into one @array
/(.*)\@\@(.*)/ and push @before,$1 and push @after,$2 for @array; # separate the  before , After the string "@@" into two arrays.
0
 
Test 2010Author Commented:
I understand what the program is doing and which statement is what. But what i want to know is what that push operation  statement exactly doing.
1. I understand with this (/Code Changes/..0) > 1 ur serching for the line contains the "code changes"  after that what that push operation
2.
0
 
ozoCommented:
perldoc -f push
       push ARRAY,LIST
               Treats ARRAY as a stack, and pushes the values of LIST onto the
               end of ARRAY.  The length of ARRAY increases by the length of
               LIST.  Has the same effect as

                   for $value (LIST) {
                       $ARRAY[++$#ARRAY] = $value;
                   }

               but is more efficient.  Returns the number of elements in the
               array following the completed "push".
so push @array,$_ adds the line to @array
0
 
Test 2010Author Commented:
I know push operation.

But i aksed about the logic you have written.
Ok.   $_ is contains the all the lines in the file. When it find's the "code changes" string in the line that particular line will move in to array.( I am assuming @array is initially emprty until string found.)
but  I didn't understand how the rest of lines are stroing in the array.

This is the my main concern i am asking from my first post. may be i am communicating properly sorry for that,.
0
 
Test 2010Author Commented:
may be i am not communicating properly, sorry for that,.
0
 
ozoCommented:
perl -MYAPE::Regex::Explain -e "print YAPE::Regex::Explain->new(qr/(.*)\@\@(.*)/)->explain"
The regular expression:

(?-imsx:(.*)\@\@(.*))

matches as follows:
 
NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  \@                       '@'
----------------------------------------------------------------------
  \@                       '@'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
----------------------------------------------------------------------
  )                        end of \2
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

so when it matches, the push @before,$1 adds the string before '@@' to the @before array, and the push @after,$2 adds the string after the @@ to the @after array
0
 
ozoCommented:
if you were wondering about scalar
/My String/..0
it becomes true when /My String/ becomes true, and stays true until after $.==0 becomes true (which is never, so the .. stays true for the rest of the file)

see
perldoc perlop
0
 
Test 2010Author Commented:
ozo:

Can i know how to keep the error checking for you code. Like If i didn't find the string i have to rise some error flag right?...
0
 
ozoCommented:
can you check whether there's anything in @before or @after?
0
 
Test 2010Author Commented:
i didn't get what you are asking . are you asking about your code.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now