小编per*_*ser的帖子

grep -f压缩文件夹中的文件

我有一个问题我希望有人能帮助...

我正在我的一个程序中使用以下命令在压缩文件夹上执行递归fgrep/grep -f搜索:

我正在使用的命令

grep -r -i -z -I -f /path/to/pattern/file /home/folder/TestZipFolder.zip
Run Code Online (Sandbox Code Playgroud)

在模式文件中是我试图搜索的字符串"狗".

在压缩文件夹中有许多包含字符串"Dog"的文本文件.

grep -f命令在压缩文件夹内的3个文件中成功找到包含字符串"Dog"的文本文件,但它在一行上打印输出,最后出现一些奇怪的字符,即PK(如下所示).当我尝试将输出打印到我的程序中的文件时,其他字符出现在最后,例如^B^T^@

grep -f命令的输出:

TestZipFolder/test.txtThis is a file containing the string DogPKtest1.txtDog, is found again in this file.PKTestZipFolder/another.txtDog is written in this file.PK 
Run Code Online (Sandbox Code Playgroud)

我如何获得已经找到字符串"Dog"的每个文件在新行上打印,这样它们就不像现在一样在一行上组合在一起?还有"PK"和输出中出现的其他奇怪字符在哪里,我如何防止它们出现?

期望的输出

TestZipFolder/test.txt:This is a file containing the string Dog
TestZipFolder/test1.txt:Dog, is found again in this file
TestZipFolder/another.txt:Dog is written in this file
Run Code Online (Sandbox Code Playgroud)

沿着这些方向的东西,用户可以看到文件中可以找到字符串的位置(如果在不是zip文件的文件上运行grep命令,实际上可以获得此格式的输出).

非常感谢你对此的帮助,谢谢

linux zip grep

8
推荐指数
1
解决办法
2万
查看次数

Perl Catalyst - 从AJAX POST请求中获取JSON数据

我试图获取在homescreenCatalyst应用程序的页面上单击按钮时通过AJAX POST发送到服务器的JSON数据:

将JSON数据发送到服务器的AJAX POST:

$("#saveCanvasStates").click(function () {
// button to save canvas states to a database

// Serialize the states array
var JsonStringForTransport = JSON.stringify({stateForUserNumber7: states});

// POST the JSON to the server
var thePost = $.ajax({
    url: 'homescreen',
    type: 'POST',
    dataType: 'json',
    data: JsonStringForTransport,
    contentType: 'application/json; charset=utf-8'
});
Run Code Online (Sandbox Code Playgroud)

我还有以下Catalyst Controller用于homescreen发送AJAX POST的按钮所在的页面:

Catalyst控制器:

package MyProject::Controller::Homescreen;

use strict;
use warnings;
use parent 'Catalyst::Controller';
use Data::Dumper;

__PACKAGE__->config->{namespace} = '';

sub homescreen :Path('/homescreen') :Args(0)  {

        my ( $self, $c …
Run Code Online (Sandbox Code Playgroud)

ajax perl jquery json catalyst

5
推荐指数
1
解决办法
3479
查看次数

Perl遍历文件中的每一行并附加到另一个文件中每行的末尾

我有两个包含以下内容的文本文件:

FILE1.TXT

dog
cat
antelope
Run Code Online (Sandbox Code Playgroud)

FILE2.TXT

1
2
Barry
Run Code Online (Sandbox Code Playgroud)

我想要实现的输出如下:

dog1
dog2
dogBarry
cat1
cat2
catBarry
antelope1
antelope2
antelopeBarry
Run Code Online (Sandbox Code Playgroud)

他们这样做了我:

    open (FILE1, "<File1.txt") || die $!;
    open (FILE2, "<File2.txt") || die $!;

    my @animals = (<FILE1>);  #each line of the file into an array
    my @otherStrings = (<FILE2>);   #each line of the file into an array

    close FILE1 || die $!;
    close FILE2 || die $!;

    my @bothTogether;
    foreach my $animal (@animals) {
    chomp $animal;
            foreach my $otherString (@otherStrings) { …
Run Code Online (Sandbox Code Playgroud)

perl hash text file

4
推荐指数
1
解决办法
1万
查看次数

Perl - Regex只提取以逗号分隔的字符串

我有一个问题,我希望有人可以帮助......

我有一个包含网页内容的变量(使用WWW :: Mechanize抓取).

该变量包含以下数据:

$var = "ewrfs sdfdsf cat_dog,horse,rabbit,chicken-pig"
$var = "fdsf iiukui aawwe dffg elephant,MOUSE_RAT,spider,lion-tiger hdsfds jdlkf sdf"
$var = "dsadp poids pewqwe ANTELOPE-GIRAFFE,frOG,fish,crab,kangaROO-KOALA sdfdsf hkew"
Run Code Online (Sandbox Code Playgroud)

我从上面的例子中感兴趣的唯一一点是:

@array = ("cat_dog","horse","rabbit","chicken-pig")
@array = ("elephant","MOUSE_RAT","spider","lion-tiger") 
@array = ("ANTELOPE-GIRAFFE","frOG","fish","crab","kangaROO-KOALA")
Run Code Online (Sandbox Code Playgroud)

我遇到的问题:

我试图只从变量中提取逗号分隔的字符串,然后将它们存储在一个数组中供以后使用.

但是,确保我在逗号分隔动物列表的开头(即cat_dog)和结尾(即鸡 - 猪)获得字符串的最佳方法是什么,因为它们没有前缀/后缀逗号.

此外,由于变量将包含网页内容,因此不可避免的是,可能还存在逗号立即由空格和另一个单词继承的情况,因为这是在段落和句子中使用逗号的正确方法...

例如:

Saturn was long thought to be the only ringed planet, however, this is now known not to be the case. 
                                                     ^        ^
                                                     |        |
                                    note the spaces here and here
Run Code Online (Sandbox Code Playgroud)

我对逗号后跟空格的任何情况都不感兴趣(如上所示).

我只对逗号之后没有空格的情况感兴趣(即cat_dog,horse,rabbit,chicken-pig) …

regex perl split comma www-mechanize

4
推荐指数
1
解决办法
7755
查看次数

如何从http响应获取301/302网站重定向位置并关注它?

我一直在尝试使用perl Mechanize(WWW :: Mechanize)从http响应中获取301/302重定向位置,但是在使用诸如response-> header之类的东西从响应中提取它时遇到了问题.

任何人都可以帮助从使用301或302重定向的网站的http响应中提取重定向位置吗?

我知道我想要做什么,以及如果我有这个重定向位置URL怎么做,因为我之前使用Mechanize完成了更复杂的事情,但我只是遇到了从获取位置(或任何其他响应字段)的真正问题http响应.

非常感谢您的帮助,非常感谢,CM

perl redirect mechanize lwp-useragent

2
推荐指数
1
解决办法
2393
查看次数

Perl访问散列/散列引用数据结构中的元素

我有一个问题,我希望你可以帮忙,因为我是哈希和哈希参考资料的新手?

我有以下数据结构:

$VAR1 = {
    'http://www.superuser.com/' => {
        'difference' => {
            'http://www.superuser.com/questions' => '10735',
            'http://www.superuser.com/faq' => '13095'
        },
        'equal' => {
            'http://www.superuser.com/ ' => '20892'
        }
    },
    'http://www.stackoverflow.com/' => {
        'difference' => {
            'http://www.stackoverflow.com/faq' => '13015',
            'http://www.stackoverflow.com/questions' => '10506'
        },
        'equal' => {
            'http://www.stackoverflow.com/ ' => '33362'
        }
    }
Run Code Online (Sandbox Code Playgroud)

如果我想访问密钥中的所有URL,'difference'那么我可以对URL执行一些其他操作,访问这些元素的正确或首选方法是什么?

例如,我最终将得到以下URL,然后我可以在foreach循环中执行以下操作:

http://www.superuser.com/questions
http://www.superuser.com/faq
http://www.stackoverflow.com/faq
http://www.stackoverflow.com/questions
Run Code Online (Sandbox Code Playgroud)

- - - 编辑 - - -

用于访问上面显示的数据结构的元素的代码:

my @urls;
foreach my $key1 ( keys( %{$VAR1} ) ) {
    print( "$key1\n" …
Run Code Online (Sandbox Code Playgroud)

perl hash www-mechanize hash-of-hashes hashref

2
推荐指数
1
解决办法
5132
查看次数

Perl - 检查每个不同数组中的任何元素是否与变量匹配

我有一个问题,我希望有人可以帮忙(为了解释我想要做的事情,大大简化了)...

我有三个不同的数组:

my @array1 =  ("DOG","CAT","HAMSTER");
my @array2 =  ("DONKEY","FOX","PIG", "HORSE");
my @array3 =  ("RHINO","LION","ELEPHANT");
Run Code Online (Sandbox Code Playgroud)

我还有一个包含网页内容的变量(使用WWW :: Mechanize):

my $variable = $r->content;
Run Code Online (Sandbox Code Playgroud)

我现在想看看每个数组中的任何元素是否都在变量中找到,如果是,那么它来自哪个数组:

例如

if ($variable =~ (any of the elements in @array1)) {
     print "FOUND IN ARRAY1";
} elsif ($variable =~ (any of the elements in @array2)) { 
     print "FOUND IN ARRAY2";
} elsif ($variable =~ (any of the elements in @array3)) {
     print "FOUND IN ARRAY3";
}
Run Code Online (Sandbox Code Playgroud)

使用数组并迭代数组中的每个元素的最佳方法是什么?有没有更好的方法可以做到这一点?

非常感谢您的帮助,谢谢

arrays perl loops if-statement www-mechanize

2
推荐指数
1
解决办法
2699
查看次数

操纵单个和多个单词字符串

我有一个包含许多名称的数组:

Fred Smith
Dave Davidson
John
Andy Wood
Robin van Persie

foreach my $name ( @arrayOfNames ) {
     my ($first, $last) = $name =~ /(.*)\s+(.).*/;
     print "$first$last";
}
Run Code Online (Sandbox Code Playgroud)

使用上面显示的foreach循环,它应该打印以下内容:

FredS
DavidD
John
AndyW
RobinvP
Run Code Online (Sandbox Code Playgroud)

但是,它没有正确处理一个单词名称(John)或两个以上的单词名称(Robin van Persie):

对于一个单词名称(约翰),我得到错误,如下所示

Use of uninitialized value $first in concatenation...
Use of uninitialized value $last in concatenation...
Run Code Online (Sandbox Code Playgroud)

对于两个以上的单词名称(Robin van Persie),它打印Robin vanP而不是RobinvP

如何更改以满足这一个单词和两个以上的单词名称?是应该将一个单词和两个以上的单词名称移动到一个新的数组中,然后再进行处理,还是可以更改正则表达式以满足此要求?

regex perl foreach

2
推荐指数
1
解决办法
64
查看次数

Perl匿名子例程/函数错误

我有以下代码:(为了这个问题的目的非常简化,但完美地说明了我遇到的问题)

#!/usr/bin/perl

use strict;
use warnings;

&outer;
my $connected_sub;

sub outer {
    print "HELLO\n";

    &$connected_sub;
    $connected_sub = sub {
        print "GOODBYE\n";
    }
}
Run Code Online (Sandbox Code Playgroud)

运行时程序会给出此输出和错误:

HELLO
Use of uninitialized value in subroutine entry at subTesting line 13.
Can't use string ("") as a subroutine ref while "strict refs" in use at subTesting.pl line 13.
Run Code Online (Sandbox Code Playgroud)

我完全忽略了什么吗?我无法理解或弄清楚这是什么问题.

perl nested anonymous anonymous-function subroutine

1
推荐指数
2
解决办法
225
查看次数

Perl在序列中的递增点处插入

我有一个问题,我希望有人可以提供帮助.

使用以下......

 my @sequence = (1..9);
 my $newSequence = join " - ", @sequence;
Run Code Online (Sandbox Code Playgroud)

...我可以打印连字符分隔的数字序列 1 - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9

我遇到问题,并且不太了解解决的最佳逻辑方法,是在每次迭代时增加数字序列中变量字符串位置的循环(以产生下面显示的输出类型).

my $varString = "DOG"
Run Code Online (Sandbox Code Playgroud)

我希望实现的输出:

DOG - 2 - 3 - 4 - 5 - 6 - 7 - 8 - 9
1 - DOG - 3 - 4 - 5 - 6 - 7 - 8 - 9
1 - 2 - DOG - 4 - …
Run Code Online (Sandbox Code Playgroud)

perl loops increment

1
推荐指数
1
解决办法
98
查看次数

Perl - 如何在不重复代码的情况下执行两个不同的Foreach循环

我有一个问题,我希望有人可以阐明......

我有一个数组散列和一个普通数组,具体取决于具体情况(即用户在程序运行时选择的选项),只定义其中一个.

用于演示问题的示例代码:

my %hashofarrays;
my @array;

#...
#Some code between here where either %hashofarrays or @array gets defined
#...

if (defined @array) {

    foreach my $var1 (@array) {
        print "var1 is: $var1\n";
        call_subroutine($var1);
        print "Something else is printed";
        call_anothersubroutine($var1);
        call_differentsubroutine($var1);
    }

} 

if (defined %hashofarrays) {

    foreach my $key (keys %hashofarrays) {
        print "the key is: $key\n";
        foreach my $var1 (@{$hashofarrays{$key}}) {
            call_subroutine($var1);
            print "Something else is printed";
            call_anothersubroutine($var1);
            call_differentsubroutine($var1);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

正如您在上面的代码中所看到的,取决于是否@array已定义或是否%hashofarrays已定义,它将运行相应的if …

perl foreach code-duplication

1
推荐指数
1
解决办法
221
查看次数

Perl Getopt :: Long赋值变量然后转到子例程

我有以下代码

my $use = "Use: " . basename($0) . " [options]";
my $version = "Version: 0.1 \n";
my $variableA;
my $variableB;

GetOptions(
'a=s'           => \$variableA,
'help'          => sub { print $use; exit 0 },
'version'       => sub { print $version; exit 0 },
'b'             => sub { \$variableB, &this_subroutine; goto NOWGOHERE; },
 );

die "Incorrect use. \n" unless (defined $variableA || defined $variableB);

sub this_subroutine {

     print "$variableB\n";

}

NOWGOHERE: print "HELLO I'M NOW HERE\n";
Run Code Online (Sandbox Code Playgroud)

我想要做的是设置$variableB,然后做&this_subroutine和, …

perl getopt getopt-long

0
推荐指数
1
解决办法
325
查看次数

Perl - 停止命令行输出中出现的错误

我在做什么:

我有以下反引号命令,该命令在简单的 foreach 循环中执行并将命令输出保存到变量,然后对该变量执行字符串匹配操作:

$ciphertestoutput = `echo -n | openssl s_client -cipher $tlsCipher -connect $ipaddress:443 2>/dev/null`;
Run Code Online (Sandbox Code Playgroud)

问题:

但是,当我在输出中运行脚本时,我收到一条错误消息,提示我似乎无法停止出现。我并不担心错误的发生,但我不希望错误显示在我所做的漂亮命令行输出的中间。

我的输出和错误:

EXP-DES-CBC-SHA CIPHER IS SUPPORTED on 192.168.1.22:443

EXP-EDH-DSS-DES-CBC-SHA CIPHER IS NOT SUPPORTED on 192.168.1.22.443

EXP-RC2-CBC-MD5 CIPHER IS NOT SUPPORTED on 192.168.1.22:443
connect: Connection refused          <--- the error I cant get rid of
connect:errno=111                    <--- the error I cant get rid of

EXP-RC4-MD5 CIPHER IS NOT SUPPORTED on 192.168.1.22:443
Run Code Online (Sandbox Code Playgroud)

我尝试过的:

我已经尝试并尝试了我所知道的各种方法来抑制输出中的错误消息,但我尝试的任何方法都无法阻止此错误的出现。我过去做过很多类似的事情,但从未遇到过这个带有反引号的问题。我在这里缺少什么明显的东西吗?

error-handling bash perl backticks output

0
推荐指数
1
解决办法
1065
查看次数