How to find lines beginning with particular characters

Hi Experts,

How do I check if a line starts with, say for example, character 'A'.

In the following three lines, only the 2nd should pass this check

Banana republic
Arctic ocean
Canberra kangaroo


if( line begins with 'A')    <---- I want the perl equivalent to do this.
{


}

ambuliAsked:
Who is Participating?
 
farzanjConnect With a Mentor Commented:
Here is the complete code just to show this
#! /usr/bin/perl

use strict;
use warnings;

my $file = 'data.txt';

open IN, $file or die "Could not open $!";
my @lines = <IN>;
close IN;

foreach my $line (@lines)
{
        if ( $line =~ "^A")
        {
                print $line;
        }
}

Open in new window

0
 
farzanjConnect With a Mentor Commented:
Short answer:

Matching with

if ($line =~ "^A")
{

}

Need full code?
0
 
ambuliAuthor Commented:
Thank you.  I will ask another question.  I want to check if the third character is numeric in a line.  I increased the points too :-)


S 13:08:30
   ^
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
farzanjCommented:
Third character is a number

if ($line =~ "^..\d")
0
 
farzanjConnect With a Mentor Commented:
Sorry use this

if ($line =~ /^..\d/)
0
 
farzanjCommented:
A better way would be


if ( $line =~ /^.{2}\d/ )
        {
                print $line;
        }
}

Explanation: ^ start of line
.{2} Any two characters
\d a numeric character.
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.

All Courses

From novice to tech pro — start learning today.