简体   繁体   中英

Performing regex capture and then substitute using SED/PERL

I have a data that looks like this (let's call this file 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?

My difficulty is in capturing the input file name and then create the output file ( .output ) - for each line - based on that. 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.

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). 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

你问了一个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 -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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM