LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 06-28-2006, 07:07 PM   #1
jazman
LQ Newbie
 
Registered: May 2006
Posts: 13

Rep: Reputation: 0
perl script


I have this perl code:

my($line) = $_;
chomp($line);
$line =~ tr/[a-z]/[A-Z]/;

I need to test if the first two character of a line in a text file "2:"

What would I use to test such a condition?
 
Old 06-28-2006, 07:21 PM   #2
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
The following just loops through a file and checks whether each line starts with 2: - is that all you wanted it to do?

Code:
open( REPORT, "afile.txt" ) || die "Can't open afile.txt: $!\n";
while ( $line = <REPORT> ) {
  if ( $line =~ m/^2:/ ) {
    print "It matches!\n";
  }
}
close( REPORT ) || die "Can't close afile.txt: $!\n";
 
Old 06-28-2006, 07:22 PM   #3
jazman
LQ Newbie
 
Registered: May 2006
Posts: 13

Original Poster
Rep: Reputation: 0
perfect

Thats great thanks
 
Old 06-28-2006, 07:30 PM   #4
jazman
LQ Newbie
 
Registered: May 2006
Posts: 13

Original Poster
Rep: Reputation: 0
One last thing

What I need to get is

2:06/06/07 15:49 is my $line

is the complete string... I want to test for just the first 2:

if ( $line =~ m/^2:/ )

Will this do that? or do I need to break it down to the first two characters?
 
Old 06-28-2006, 08:08 PM   #5
gilead
Senior Member
 
Registered: Dec 2005
Location: Brisbane, Australia
Distribution: Slackware64 14.0
Posts: 4,141

Rep: Reputation: 168Reputation: 168
I might not be understanding you properly, but...

$line =~ m/^2:/ will test the first 2 characters of $line and does not change $line. If you use $line after matching, it will have the same value as it had before matching. So if $line was 2:06/06/07 15:49 before the match, it is still 2:06/06/07 15:49 after the match.

If you want to know what happens during a match, perl sets several variables you can use after the match statement. Perl sets $` to the string preceding the match, it sets $& to the string that was matched and it sets $' to the string following what was matched. For the example of 2:06/06/07 15:49 this is what they would be:
Code:
$` would be the empty string at the start of the line
$& would be 2:
$' would be 06/06/07 15:49
$line would be 2:06/06/07 15:49
Using these variables can cause a performance hit, so test them if you want to use them with large files.

Hope that helps...
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to run a shell script in the perl script sharad Linux - General 1 05-24-2006 03:23 AM
Converting a Windows Perl script to a Linux Perl script. rubbercash Programming 2 07-19-2004 10:22 AM
Help with Perl Script dannyp Programming 0 07-18-2004 07:36 PM
how to find the pid of a perl script from shell script toovato Linux - General 1 12-19-2003 06:25 PM
Including methods from a perl script into another perl script gene_gEnie Programming 3 01-31-2002 05:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration