对一个文件进行行和列的对齐,但是要跳过某些行的方法,在Perl语言中可以使用以下代码:
#!/usr/bin/perl
my $in_file = "input.txt";
my $out_file = "output.txt";
# Skipped lines
my @skip_lines = (2, 5);
# Open input file for reading
open(my $in_fh, '<', $in_file) or die "Cannot open $in_file: $!";
# Open output file for writing
open(my $out_fh, '>', $out_file) or die "Cannot open $out_file: $!";
# Read input file line by line
while (my $line = <$in_fh>) {
# Check if line is not in skipped lines
if (!grep(/^$.|$/, @skip_lines)) {
# Split line into columns
my @columns = split(/\s+/, $line);
# Align columns using printf()
printf($out_fh "%-10s %-10s %-10s\n", $columns[0], $columns[1], $columns[2]);
}
}
# Close input and output files
close($in_fh);
close($out_fh);
以上代码从输入文件中逐行读取数据,并在跳过指定行之后对其进行分隔符拆分。拆分后,使用 printf() 函数将数据按指定格式对齐输出到输出文件中。
需要对 @skip_lines 数组进行修改来定义需要跳过的行。