在 Mac 上循环遍历 PERL 中文本文件中的行,无法在 while 循环中打印行
Looping through lines in text file in PERL on Mac, unable to print lines in while loop
有很多关于在 PERL 上循环文件的文档,但我 运行 遇到了一个问题,我还没有找到解决方案。我还没有得到任何循环遍历文本文件中行的示例。
首先,我的环境:我在使用 macOS Sierra 版本 10.12.6 的 MacBook 上。我正在用 TextWrangler 版本 5.5.2 运行 编写脚本。
我想在下面的脚本中编写子 ProcessFile 来对目录中文本文件的每一行进行字符串解析,但这根本不是 运行。 TextWrangler 的 Unix 脚本中没有输出 Output.log.
#!/usr/bin/env perl
use strict;
use warnings;
my $dirName = "/Users/me/Documents/examples";
chdir $dirName or die "Cannot chdir $dirName: $!";
opendir ( my $dir, $dirName ) or die "Cannot open directory $dirName: $!";
my @files = readdir $dir;
# Put in variable for debugging with less than the full directory
#my $numberOfFiles = scalar( @files );
my $numberOfFiles = 10;
for ( my $iFiles = 0; $iFiles < $numberOfFiles; $iFiles++ )
{
# Check if .txt file
if ( $files[$iFiles]=~/\.txt$/ )
{
my $fileName = "$files[$iFiles]";
ProcessFile ( $fileName );
}
}
closedir $dir;
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
while (my $line = <$inputFile>)
{
print "$line";
#Add parsing to gather metrics on the number of instances of different patterns
}
}
但是,如果我将 ProcessFile 更改为以下内容,它会打印文件的前两行:
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
my $line = <$inputFile>;
print "$line";
my $line2 = <$inputFile>;
print "$line2";
}
此外,如果我将 ProcessFile 更改为下面的内容,它会为文件中的每一行打印一行:
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
my $i = 0;
while (my $line = <$inputFile>)
{
print "$i\n";
$i++;
}
}
此时我不确定下一步应该做什么才能遍历文件的每一行并将其解析为字符串。最终我也想将匹配的字符串输出到文件中,但我需要先通过这个障碍。
一定是editor/IDE问题。您的原始代码工作正常。我用 3 个 txt 文件创建了目录,运行 你的代码,这里是输出:
[18:04:40][filip@lap2:~]$ more /Users/me/Documents/examples/*
::::::::::::::
/Users/me/Documents/examples/a.txt
::::::::::::::
a
a
a
::::::::::::::
/Users/me/Documents/examples/b.txt
::::::::::::::
b
b
b
::::::::::::::
/Users/me/Documents/examples/c.txt
::::::::::::::
c
c
c
[18:04:48][filip@lap2:~]$ perl 49779605.pl
b.txt
b
b
b
c.txt
c
c
c
a.txt
a
a
a
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
有很多关于在 PERL 上循环文件的文档,但我 运行 遇到了一个问题,我还没有找到解决方案。我还没有得到任何循环遍历文本文件中行的示例。
首先,我的环境:我在使用 macOS Sierra 版本 10.12.6 的 MacBook 上。我正在用 TextWrangler 版本 5.5.2 运行 编写脚本。
我想在下面的脚本中编写子 ProcessFile 来对目录中文本文件的每一行进行字符串解析,但这根本不是 运行。 TextWrangler 的 Unix 脚本中没有输出 Output.log.
#!/usr/bin/env perl
use strict;
use warnings;
my $dirName = "/Users/me/Documents/examples";
chdir $dirName or die "Cannot chdir $dirName: $!";
opendir ( my $dir, $dirName ) or die "Cannot open directory $dirName: $!";
my @files = readdir $dir;
# Put in variable for debugging with less than the full directory
#my $numberOfFiles = scalar( @files );
my $numberOfFiles = 10;
for ( my $iFiles = 0; $iFiles < $numberOfFiles; $iFiles++ )
{
# Check if .txt file
if ( $files[$iFiles]=~/\.txt$/ )
{
my $fileName = "$files[$iFiles]";
ProcessFile ( $fileName );
}
}
closedir $dir;
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
while (my $line = <$inputFile>)
{
print "$line";
#Add parsing to gather metrics on the number of instances of different patterns
}
}
但是,如果我将 ProcessFile 更改为以下内容,它会打印文件的前两行:
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
my $line = <$inputFile>;
print "$line";
my $line2 = <$inputFile>;
print "$line2";
}
此外,如果我将 ProcessFile 更改为下面的内容,它会为文件中的每一行打印一行:
sub ProcessFile
{
my ( $fileName ) = @_;
print $fileName; print "\n";
open(my $inputFile, "<$fileName" ) or die "Cannot open file $fileName: $!";
my $i = 0;
while (my $line = <$inputFile>)
{
print "$i\n";
$i++;
}
}
此时我不确定下一步应该做什么才能遍历文件的每一行并将其解析为字符串。最终我也想将匹配的字符串输出到文件中,但我需要先通过这个障碍。
一定是editor/IDE问题。您的原始代码工作正常。我用 3 个 txt 文件创建了目录,运行 你的代码,这里是输出:
[18:04:40][filip@lap2:~]$ more /Users/me/Documents/examples/*
::::::::::::::
/Users/me/Documents/examples/a.txt
::::::::::::::
a
a
a
::::::::::::::
/Users/me/Documents/examples/b.txt
::::::::::::::
b
b
b
::::::::::::::
/Users/me/Documents/examples/c.txt
::::::::::::::
c
c
c
[18:04:48][filip@lap2:~]$ perl 49779605.pl
b.txt
b
b
b
c.txt
c
c
c
a.txt
a
a
a
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.
Use of uninitialized value within @files in pattern match (m//) at 49779605.pl line 23.