Discussion:
end of line
(too old to reply)
Bruce Bowen
2006-02-19 22:32:53 UTC
Permalink
I have a text file with lines of varying length.

000,;,001,WL0,001,001,000,000,000,000
011,@D ,011,000,001,050,050,105,105,004,004,064,255,000,001,116,255,255,255,106,255,255,255,255,116,255,255,255
012,D,038,032,000,002,000,001,000,000
013,@D ,013,000,001,050,050,105,105,004,004,064,255,000,001,091,255,255,255,106,255,255,255,255,091,255,255,255

According to a hex editor each line ends with a hex 0D 0A.

I can index into this file searching for items such as 012,D or 011,@D.
$a = index($data, "011,\@D");

What I need to do once the line is found is to isolate that one line. I can do a substr starting at the index and that eliminates all data in front of the subject line, but then it contains all of the data past, to the end of the file.
$sub1 = substr($data, $a);

My first attempt was to then try and index on "/\n" and that indicated it could not find that character.
$in = index($sub1, "/\n");

I then tried 0x0D could find that and then 0x0A and while it seemed to find that character it wasn't the index was only half way into the line not at the end of the line as I expected.
$in = index($sub1, 0x0D); result was a -1

$in = index($sub1, 0x0A); result was 28 which if used to get a second substr

$sub2 = substr($sub1, 0, $in);

yields 011,@D ,011,000,001,050,050,

Anyone got a clue as to what is going on?

Thanks,
Bruce Bowen
Xavier Noria
2006-02-19 22:59:16 UTC
Permalink
Post by Bruce Bowen
I have a text file with lines of varying length.
000,;,001,WL0,001,001,000,000,000,000
011,000,001,050,050,105,105,004,004,064,255,000,001,116,255,255,255,10
6,255,255,255,255,116,255,255,255
012,D,038,032,000,002,000,001,000,000
013,000,001,050,050,105,105,004,004,064,255,000,001,091,255,255,255,10
6,255,255,255,255,091,255,255,255
According to a hex editor each line ends with a hex 0D 0A.
What I need to do once the line is found is to isolate that one line.
A possible idiom is:

my @lines = <>; # slurp all lines
my @wanted = grep { m'012,D|011,@D' } @lines;

If the file is too big:

my @wanted = ();
while (my $line = <>) {
push @wanted, $line if $line =~ m'012,D|011,@D';
}

You get the idea.

-- fxn

PS: Untested examples.
Owen Cook
2006-02-19 22:56:03 UTC
Permalink
Post by Bruce Bowen
I have a text file with lines of varying length.
000,;,001,WL0,001,001,000,000,000,000
012,D,038,032,000,002,000,001,000,000
According to a hex editor each line ends with a hex 0D 0A.
What I need to do once the line is found is to isolate that one line. I can do a substr starting at the index and that eliminates all data in front of the subject line, but then it contains all of the data past, to the end of the file.
$sub1 = substr($data, $a);
My first attempt was to then try and index on "/\n" and that indicated it could not find that character.
$in = index($sub1, "/\n");
I then tried 0x0D could find that and then 0x0A and while it seemed to find that character it wasn't the index was only half way into the line not at the end of the line as I expected.
$in = index($sub1, 0x0D); result was a -1
$in = index($sub1, 0x0A); result was 28 which if used to get a second substr
$sub2 = substr($sub1, 0, $in);
Anyone got a clue as to what is going on?
You need to look at 'chomp'

while(<FH>){
chomp;
my $line=$_;
#do something with $line
}


Try something along those lines



Owen
John W. Krahn
2006-02-19 23:47:36 UTC
Permalink
Post by Bruce Bowen
I have a text file with lines of varying length.
Most text files are like that.
Post by Bruce Bowen
000,;,001,WL0,001,001,000,000,000,000
012,D,038,032,000,002,000,001,000,000
According to a hex editor each line ends with a hex 0D 0A.
Then set the Input Record Separator to OD OA

$/ = "\x0D\x0A";



John
--
use Perl;
program
fulfillment
Xavier Noria
2006-02-20 07:53:20 UTC
Permalink
*********************************************
I tried
open STATE, "STATEFILE.txt" or die
print "state = ", $state, "\n";
And I get state = 1
No errors but not the output I'm looking for.
Note that there was an array @wanted in my example, that makes a
difference:

open my $st, "STATEFILE.txt" or die $!;
my @lines = <$st>;
close $st;

my @states = grep { /011.,/ } @lines;
print "state = $_" for @states;

That can be done in the command line as well (the example uses Unix
shell quotes, adapt to Windows if needed):

$ perl -ne 'print qq(state = $_) if /011.,/' < STATEFILE.txt

-- fxn

Loading...