嗨,我是编程新手。
在尝试执行下面的脚本时,使用这种形式的输入文件(在本例中它们称为nohd*.txt
文件)
NSGEAPKNFGLDVKITGESENDRDLGTAPGGTLNDIG
IIIIMMMMMOOOOOOOOOOOOOMMMMMMMMMMMIIII
我想计算包含 file 中的文本的列d.txt
,请参见下一步。
D
O
为此,我编写了以下脚本
#!/bin/bash
for z in {1..141}
do
a=0
l=$(tail -1 nohd$z.txt | wc -m)
x=$(cat d.txt)
for ((p; p<=l; p++))
do
if [ "$(cut -c $p nohd$z.txt)" = "$x" ] ; then
a=$((a+1))
p=$((p+1))
fi
done
echo $a
done
我正在运行此脚本,使用./script
它显示此错误:
cut: invalid byte/character position ‘{1..351}’
它仅正确地转换第一个值,对于其余值,它转换为 0 预期输出采用以下形式
5
20
4
请问有人可以帮我吗?感谢你
答案1
#!/usr/bin/perl
use File::Slurp;
# read d.txt and turn into a regular expression.
my $re = '^(' . join ('|',read_file('./d.txt', chomp => 1)) . ')$';
# read input files and print location of matches.
while (<>) {
for (my $i=0;$i <= length; $i++) {
printf "%s\n", $i+1 if (substr($_,$i,1) =~ m/$re/o);
}
}
此 perl 脚本打印输入行中每个“D”和“O”字符的位置。
另存为,例如islem.pl
,使其可执行chmod +x islem.pl
,然后像这样运行:
$ ./islem.pl nohd*.txt
12
22
24
35
10
11
12
13
14
15
16
17
18
19
20
21
22
上述输出是将示例数据保存为nohd1.txt
.
我想我误解了你的问题。看起来你想要数数一行中的匹配项,而不是位置每场比赛。如果这就是您想要的,请尝试以下操作:
#!/usr/bin/perl -l
use File::Slurp;
# read d.txt and turn into a regular expression.
my $re = '^(' . join ('|',read_file('./d.txt', chomp => 1)) . ')$';
# read input files and print location of matches.
while (<>) {
my $count=0;
for (my $i=0;$i <= length; $i++) {
$count++ if (substr($_,$i,1) =~ m/$re/o);
};
print $count;
}
示例输出:
$ ./islem2.pl nohd*
4
13
这很容易适应许多变化。例如,如果您想以:
分隔格式打印文件名和行号以及相应的计数:
#!/usr/bin/perl -l
use File::Slurp;
# read d.txt and turn into a regular expression.
my $re = '^(' . join ('|',read_file('./d.txt', chomp => 1)) . ')$';
# read input files and print location of matches.
while (<>) {
my $count=0;
for (my $i=0;$i <= length; $i++) {
$count++ if (substr($_,$i,1) =~ m/$re/o);
};
print "$ARGV:$.:$count";
} continue {
# explicitly close the current file on eof to reset the line counter
# after each input file because perl only has a cumulative line
# counter ($.) rather than both FR and FNR like awk.
close ARGV if eof;
}
示例输出:
$ ./islem3.pl nohd*.txt
nohd1.txt:1:4
nohd1.txt:2:13