Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
Regex PERL:命令行上的多行替换,无需读取文件_Regex_Perl_Command Line_Applescript - Fatal编程技术网

Regex PERL:命令行上的多行替换,无需读取文件

Regex PERL:命令行上的多行替换,无需读取文件,regex,perl,command-line,applescript,Regex,Perl,Command Line,Applescript,我有以下多行输入: /Users/myuser/doc/myfiles/somefile1.txt /Users/myuser/doc/myfiles/someotherfile.txt /Users/myuser/doc/myfiles/otherfoler/otherthing.jpg 我需要以下输出(也可以全部在一行上,我不需要\n): 以下是我尝试使用但未成功的内容: | perl -le 'm@(doc.*)$@mg; print ${^MATCH}; ' 我之所以尝试在没有文件的

我有以下多行输入:

/Users/myuser/doc/myfiles/somefile1.txt
/Users/myuser/doc/myfiles/someotherfile.txt
/Users/myuser/doc/myfiles/otherfoler/otherthing.jpg
我需要以下输出(也可以全部在一行上,我不需要\n):

以下是我尝试使用但未成功的内容:

| perl -le 'm@(doc.*)$@mg; print ${^MATCH}; '
我之所以尝试在没有文件的情况下执行此操作,是因为它实际上是apple脚本的一部分:

set myOtherVar to the clipboard
set thecommandstring to "echo \"" & myOtherVar & "\" | perl -le 'm@(doc.*)$@mg; print  ${^MATCH}; '" as string
set sedResult to do shell script thecommandstring
set the clipboard to sedResult

它获取剪贴板并将其发送到perl命令。然后将结果放回剪贴板。

您可以使用这个perl命令

$ perl -pe 's/^.*doc/doc/g' file
doc/myfiles/somefile1.txt
doc/myfiles/someotherfile.txt
doc/myfiles/otherfoler/otherthing.jpg

试试这个:

| perl -pe "s/.*?\/doc\//\/doc\//g"

我测试了您的脚本,这里也有同样的问题,答案中的perl命令只返回最后一行,但是这些命令不使用剪贴板
命令

do shell script "pbpaste -Prefer txt | perl -pe 's:^.*/doc/:doc/:' | LC_CTYPE=UTF-8 pbcopy" -- paste the clipboard, substitute text and copy STDOUT to the clipboard
第一种解决方案:使用剪贴板作为«类utf8»

set myText to the clipboard as «class utf8»
do shell script "perl -pe 's:^.*/doc/:doc/:' <<< " & (quoted form of myText) & " | LC_CTYPE=UTF-8 pbcopy" --> copy STDOUT to the clipboard

第一个只给我:
doc/myfiles/otherfoler/otherthing.jpg
。第二个给了我:
doc/myfiles/somefile1.txt/Users/myuser/doc/myfiles/someotherfile.txt/Users/myuser/doc/myfiles/otherfoler/otherthing.jpg
try
perl-ple's/^(?!doc)。)*//g'file
get:
doc/myfiles/somefile1.txt/Users/myuser/doc/myfiles/someotherfile.txt/Users/myuser/doc/myfiles/otherthing.jpg
这一个结果是:/doc//doc doc//doc/myfiles/otherfoler/otherthing.jpgrely!!但我不这么认为,我他妈的肯定你做错了什么,不管怎么说,我在我的windows机器上,但稍后,我肯定会说到这一点,它应该可以工作,但由于一些奇怪的原因它不能。新线之后的任何东西似乎都不起作用。下面是我使用的苹果脚本:将myOtherVar设置为剪贴板将commandstring设置为“echo\”&myOtherVar&“perl-pe”s@.*“?/doc/@/doc/@g'”作为字符串设置sedResult以执行shell脚本命令字符串将剪贴板设置为sedResult如果后面的原因是新行,则只需更改一点,使用
|perl-pe"s@.*?/doc/@/doc/@sg“
,刚刚在最后一个正斜杠后添加了s。如果仍然不起作用,请告诉我。
set myText to the clipboard as «class utf8»
do shell script "perl -pe 's:^.*/doc/:doc/:' <<< " & (quoted form of myText) & " | LC_CTYPE=UTF-8 pbcopy" --> copy STDOUT to the clipboard
do shell script "pbpaste -Prefer txt | perl -pe 's:^.*/doc/:doc/:' | LC_CTYPE=UTF-8 pbcopy" -- paste the clipboard, substitute text and copy STDOUT to the clipboard