Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 在行中搜索特定的字符串,然后在PERL中此时执行substr_String_Perl_Indexing_Substr - Fatal编程技术网

String 在行中搜索特定的字符串,然后在PERL中此时执行substr

String 在行中搜索特定的字符串,然后在PERL中此时执行substr,string,perl,indexing,substr,String,Perl,Indexing,Substr,我想在一行中搜索字符串。在我找到这个字符串之后,我想在其中做一个substr,以获取ID。下面是一个示例 字符串Testname测试文件21345 Anfragekennziffer 4519349测试字符串 因此,在本例中,我希望脚本捕获字符串“Anfragekennziffer”并获取ID,即:4519349 到目前为止,我写了一个脚本,这让我得到了ID,但前提是“Anfragekennziffer”在一行的开头。例如: line 1 "Anfragekennziffer4586268"

我想在一行中搜索字符串。在我找到这个字符串之后,我想在其中做一个substr,以获取ID。下面是一个示例

字符串
Testname测试文件21345 Anfragekennziffer 4519349测试字符串

  • 因此,在本例中,我希望脚本捕获字符串“Anfragekennziffer”并获取ID,即:
    4519349
到目前为止,我写了一个脚本,这让我得到了ID,但前提是“Anfragekennziffer”在一行的开头。例如:

line 1 "Anfragekennziffer4586268" result: "4586268"
line 2 "Anfragekennziffer5686797" result: "5686797"
例如

输入文件可以包含以下内容:

da adad Anfragekennziffer 6797456
dadad  Anfragekennziffer6453914
dasdssss Anfragekennziffer6797433
dddsaaa Anfragekennziffer6609572
aaa Anfragekennziffer2435282
Anfragekennziffer 1234567
Anfragekennziffer21415666
我的脚本(见下文)将得到以下结果:

'nnziffer6','nziffer645','ennziffer6','nnziffer66','ffer243528','1234567','21415666'
所以,我希望脚本从“Anfragekennziffer”开始,让我知道Anfragekennziffer在队伍中的位置

以下是我目前的代码:

#!/usr/bin/perl
use strict;
use warnings;
no warnings 'uninitialized';

my $Source = "Anfragekennziffer.txt";
my $SQL1 = "SQL_Confluence_Tabelle.txt";
my $SQL2 = "SQL_Provider_Antwort.txt";
my $time = localtime;

print "\n";
print "Local date and time: $time\n";
print "\n";
print "IDs wurden übertragen.\n";
print "Folgende SQL-Statements wurden erstellt:\n";
print "\n";
print "'SQL_Confluence_Tabelle'\n";
print "'SQL_Provider_Antwort'\n";
print "\n";

open(QUELLE, "$Source") or die "Open failed:$!";
open(ZIEL1, ">$SQL1") or die "Open failed:$!";
open(ZIEL2, ">$SQL2") or die "Open failed:$!";

my $ids;
my $weg;

while(my $row = <QUELLE>)
{
    if ($row =~m/Anfragekennziffer/)
    {
        $row=substr($row,17,10); #gebe nur IDs aus
        $row=~ s/^\s+|\s+$//g; #löscht Leerzeichen
        $ids=$ids."'".$row."',"; #kumuliere IDs
        $ids=~ s/\r|\n//g;
    }
}
$weg=chop($ids);

print ZIEL1 "$ids";

print ZIEL2 "$ids";
close(QUELLE); 
close(ZIEL1); 
close(ZIEL2);
#/usr/bin/perl
严格使用;
使用警告;
没有“未初始化”的警告;
my$Source=“Anfragekennziffer.txt”;
my$SQL1=“SQL\u Confluence\u Tabelle.txt”;
my$SQL2=“SQL\u Provider\u Antwort.txt”;
my$time=localtime;
打印“\n”;
打印“本地日期和时间:$time\n”;
打印“\n”;
打印“IDs wurdenübertragen.\n”;
打印“文件夹SQL语句wurden erstellt:\n”;
打印“\n”;
打印“'SQL\u Confluence\u Tabelle'\n”;
打印“'SQL\u Provider\u Antwort'\n”;
打印“\n”;
打开(QUELLE,“$Source”)或死“打开失败:$!”;
打开(ZIEL1,“>$SQL1”)或死“打开失败:$!”;
打开(ZIEL2,“>$SQL2”)或死“打开失败:$!”;
我的$ID;
我的$weg;
while(my$row=)
{
如果($row=~m/Anfragekennziffer/)
{
$row=substr($row,17,10)#gebe nur IDs aus
$row=~s/^\s+\s+$//g;#löscht Leerzeichen
$ids=$ids.“'”、“$row.”、“#kumuliere ids
$ids=~s/\r |\n//g;
}
}
$weg=印章($ID);
打印ZIEL1“$ids”;
打印ZIEL2“$ids”;
关闭(QUELLE);
关闭(ZIEL1);
关闭(ZIEL2);
如果有人能想出主意,那就太好了


谢谢

在关键字
Anfragekennziffer
后查找数字是最简单的方法,可以使用与数字匹配并捕获数字的正则表达式:

my @ids;
while(my $row = <QUELLE>)
{
    if ($row =~ m/Anfragekennziffer\s*(\d+)/)
    {
        push @ids, $1;
    }
}
print join ",", @ids;
这种方法比直接匹配数字的方法要脆弱得多


在这两种情况下,我没有构建字符串,而是将信息累积到一个数组(
@ids
)中,然后在最后使用函数打印该数组。将列表保留为数组有助于您的代码保持灵活,以防以后需要对数据执行其他操作。

所以您有数据库转储,并希望对其执行文本搜索?为什么?这与datbase转储无关。收到别人的电子邮件。因此,我编写这个脚本是为了更快地获取ID(例如,在SQL查询中)。因此,我不想把“名单”排成一行,因为“安弗拉基肯齐弗”排在第一行。我愿意搜索它,而不是做一个substr。e、 g.“yszadszadjadkak Anfragekennziffer 123458 yaidgqfoiqhfoqo”-因此我希望这里的ID是123458,因为我找到了“Anfragekennziffer”。完成。也许更好理解。太好了,很好用。非常感谢你!现在,我尝试将ID聚合如下:例如:'59092'、'1491915',11851'到目前为止尝试了此操作(见下文),但它只返回一个ID(最后一个)
my@ids;while(my$row=){if($row=~m/Anfragekennziffer\s*(\d+/){push@ids,$1;@ids=“”””,“;}}}print join“,”,@ids;打印ZIEL1“@ids”;打印ZIEL2“@ids”
No-
@ids
已经有ID列表。不要用它进行字符串连接。只需
项目推到该列表上,最后,
打印
加入
ed列表到您的文件中。不要打印“@ids”,除非您希望列表以空格分隔。谢谢!明天就试试看!:)
my @ids;
while(my $row = <QUELLE>)
{
    if (my $pos = index('Anfragekennziffer', $row))
    {
        push @ids, substr( $row, $pos + length('Anfragekennziffer'), 10 );
    }
}
print join ",", @ids;