user3616128
user3616128

Reputation: 377

Read the line from the particular index till the end in a file in perl

I have the log file with the following content:

(8092) "DEFECT_AUDIT_INTTEST_FRI_JAN_02_2015_07_05_09" (3 of 4)
(7992) ---$ FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
(8007) ---$ FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
(7994) ---$ FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

I want to read each file and store the content which follows the follwoing criteria i.e. a. Line has --- b. Content that starts from --- followed by a special character $ and space . For eg. here I want the array which will be of size 3 and has following content: FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM My current code is:

if($_ =~/---$/){
    my ($CsDescription) = /"---$ "/;
    push @CSArray , $CsDescription;
}

Upvotes: 0

Views: 135

Answers (2)

7stud
7stud

Reputation: 48599

...the following metacharacters have [special] meanings:

\        Quote the next metacharacter
^        Match the beginning of the line
.        Match any character (except newline)
$        Match the end of the string (or before newline at the end
         of the string)
|        Alternation
()       Grouping
[]       Bracketed Character class 

http://perldoc.perl.org/perlre.html

Special Characters Inside a Bracketed Character Class
Most characters that are meta characters in regular expressions (that is, characters that carry a special meaning like ., * , or () lose their special meaning and can be used inside a character class without the need to escape them. For instance, [()] matches either an opening parenthesis, or a closing parenthesis, and the parens inside the character class don't group or capture.

Characters that may carry a special meaning inside a character class are: \ , ^, - , [ and ], and are discussed below. They can be escaped with a backslash, although this is sometimes not needed, in which case the backslash may be omitted.

http://perldoc.perl.org/perlrecharclass.html#Bracketed-Character-Classes

use strict;
use warnings;
use 5.016;

my @lines;

my $regex = qr{
    .*?     #Match any character, 0 or more times, non-greedy, followed by...
    -{3}    #a dash, 3 times, followed by...
    \$      #a dollar sign, followed by...
    [ ]     #a space, followed by...
    (.*)    #any character, 0 or more times, captured in $1
}xms;


for my $line (<DATA>) {
    if ($line =~ $regex) {
        push @lines, $1;
    }
}

print for @lines;

__DATA__
(8092) "DEFECT_AUDIT_INTTEST_FRI_JAN_02_2015_07_05_09" (3 of 4)
(7992) ---$ FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
(8007) ---$ FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
(7994) ---$ FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

Output:

FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

Most characters that are meta characters in regular expressions (that is, characters that carry a special meaning like ., * , or () lose their special meaning and can be used inside a character class without the need to escape them. For instance, [()] matches either an opening parenthesis, or a closing parenthesis, and the parens inside the character class don't group or capture.

Characters that may carry a special meaning inside a character class are: \ , ^, - , [ and ], and are discussed below. They can be escaped with a backslash, although this is sometimes not needed, in which case the backslash may be omitted.

http://perldoc.perl.org/perlrecharclass.html#Bracketed-Character-Classes

WHAT ABOUT $??!

Upvotes: 1

Kalanidhi
Kalanidhi

Reputation: 5092

You can literal the $ because you are using $ perl taken as as line end And removed the content until $ symbol found

if(/---\$/){
    my $CsDescription=$_;                                                    
    $CsDescription =~s/.*\$// ;
    push @CSArray , $CsDescription;
}


print "@CSArray" ;

Output :

FirstName1 Surname1 "Comment number 1" 02-Jan-2015 01:53 AM
FirstName2 Surname2 "Comment number 2" 19-Dec-2014 06:20 AM
FirstName3 Surname3 "Comment number 3" 19-Dec-2014 06:46 AM

Upvotes: 0

Related Questions