Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

skipping blank lines in file

Status
Not open for further replies.

jerijeri

Programmer
Sep 11, 2002
33
CA
HI,

I have a large number of files in a directory. I need to read the names of each file and then print a listing.

For example
/files/AKanchorage
/files/TXDallas.

I want to read each file and then print data like this:

anchorage:Line 1
anchorage:Line 2
Dallas:Line 1
Dallas:Line 2


There are two problems,

1: I forget how to get all about the first 2 characters of a variable. ie. convert AKanchorage to anchorage
2: I'm not sure how to check for a blank line. I don't want to print a line like anchorage:

Any help greatly appreciated. Thanks.

Jer
 
Hi jerijeri!
I think the problem with the blank lines could be solved if you have a look at thread219-126765 , there are a few examples given, I think the one raider2001 gives should be working for you.
For I'm no native English speaker I don't really get what you need to do for your first question, but I think you should have a look at the FAQ Some Basics on Regular Expressions written by goBoating where a lot of good examples for Regex are given.
You probably need to work with something like
$variable =~ s/<your regex>//g $yourvariable=$variable.
Hope this can help,
busche
 
#!/usr/local/bin/perl

#
# 1 Getting city
#
$data = 'WImadison';
$x = substr($data, 2);
print &quot;$x\n&quot;;

# OR get state and city
# using RegExp

($city, $state) = $data =~ /(\w{2})(\w+)/;
print &quot;* $city:$state\n&quot;;

#
# 2 Testing for blank line
# Assuming &quot;blank line&quot; is just a CR.
#
$test1 = &quot;\n&quot;;
$test2 = &quot;stuff\n&quot;;

foreach ($test1, $test2) {
print &quot;* &quot;, ++$i ,&quot;:&quot;; # Just print formatting
if (/^\n$/) { # Here's the test
print &quot;nothing\n&quot;;
} else {
print &quot;Something\n&quot;;
}
}

Andrew Markiewicz
 
Thank you both for your help.

I remembered substr after I posted, but

# using RegExp

($city, $state) = $data =~ /(\w{2})(\w+)/;
is actually better for what I need to do.

Thanks again.

Jer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top