We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

How to find lines beginning with particular characters

ambuli
ambuli asked
on
Medium Priority
350 Views
Last Modified: 2012-05-11
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.
{


}

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

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
   ^
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT

Commented:
Third character is a number

if ($line =~ "^..\d")
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
CERTIFIED EXPERT

Commented:
A better way would be


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

Explanation: ^ start of line
.{2} Any two characters
\d a numeric character.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.