[英]Performing regex capture and then substitute using SED/PERL
I have a data that looks like this (let's call this file submit.txt
): 我有一个看起来像这样的数据(让我们称这个文件为
submit.txt
):
dir1/pmid_5409464.txt
dir1/pmid_5788247.txt
dir1/pmid_4971884.txt
What I want to do is to perform an inline file regex change so that it results in the following 我想要做的是执行内联文件正则表达式更改,以便它导致以下结果
perl mycode.pl /home/neversaint/dir1/pmid_5409464.txt > /home/neversaint/dir1/pmid_5409464.output
perl mycode.pl/home/neversaint/dir1/pmid_5788247.txt > /home/neversaint/dir1/pmid_5788247.output
perl mycode.pl /home/neversaint/dir1/pmid_4971884.txt > /home/neversaint/dir1/pmid_4971884.output
Is there a SED/Perl one liner to do that? 是否有一个SED / Perl衬垫来做到这一点?
My difficulty is in capturing the input file name and then create the output file ( .output
) - for each line - based on that. 我的困难在于捕获输入文件名,然后为每一行创建输出文件(
.output
) - 基于此。 I'm stuck with this: 我坚持这个:
sed 's/^/perl mycode.pl \/home\/neversaint\/dir1\//g' submit.txt |
sed 's/$/ >/'
You can use escaped parenthesis to capture groups, and access the groups with \\1, \\2, etc. 您可以使用转义括号来捕获组,并使用\\ 1,\\ 2等访问组。
sed 's/^\(.*\).txt$/perl mycode.pl \/home\/neversaint\/\1\.txt > \/home\/neversaint\/\1.output/' submit.sh
output: 输出:
perl mycode.pl /home/neversaint/dir1/pmid_5409464.txt > /home/neversaint/dir1/pmid_5409464.output
perl mycode.pl /home/neversaint/dir1/pmid_5788247.txt > /home/neversaint/dir1/pmid_5788247.output
perl mycode.pl /home/neversaint/dir1/pmid_4971884.txt > /home/neversaint/dir1/pmid_4971884.output
edit: it doesn't look like sed has a built-in in place file editing (GNU sed has the -i option). 编辑:它看起来不像sed有一个内置的文件编辑(GNU sed有-i选项)。 It still possible to do but this solution just prints to standard out.
它仍然可以,但这个解决方案只是打印到标准。 You could also use a Perl one liner as shown here: sed edit file in place
您也可以使用Perl one liner,如下所示: sed编辑文件
你问了一个Sed单行,你得到了它。
sed 's/\\([^.]*\\)\\.txt/perl mycode.pl \\/home\\/neversaint\\/\\1.txt > \\/home\\/neversaint\\/\\1.output/' submit.txt > output.txt
The perl oneliner for doing the same is 用于执行相同操作的perl oneliner是
perl -pe "s@(.*?)(\.txt)@perl mycode.pl /home/neversaint/\\1\\2 > /home/neversaint/\\1.output@g" submit.txt
The above command will produce a replaced string in the console and you have to redirect the output to another file. 上面的命令将在控制台中生成替换的字符串,您必须将输出重定向到另一个文件。
For replacing within the file (inline replace) you can add -i option . 要在文件中替换(内联替换),可以添加-i选项。 For eg
例如
perl -pe "s@(.*?)(.txt)@perl mycode.pl /home/neversaint/\1\2 > /home/neversaint/\1.output@g" -i submit.txt
The above will perform a replace within the submit.txt file itself. 以上将在submit.txt文件中执行替换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.