我需要支持为包含大约5000行的文件执行以下任务。
输入
cp abc/P_10_10A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
cp abc/P_10_11A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
cp abc/P_10_11B.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
输出
cp abc/P_10_10A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_10A
cp abc/P_10_11A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11A
cp abc/P_10_11B.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11B
我可以添加mv 。,如下所示..
sed '0~4 a\mv *.* \' text_file.sh
我该怎么办呢?非常感谢。
答案 0 :(得分:1)
一个sed版本
sed '/^[[:blank:]]*cp /,/^[[:blank:]]*\./ {
H
/^[[:blank:]]*\./!d
s/.*//;x
s#^\(\(.[[:blank:]]*\)cp \)\([^[:blank:]]*/\([^[:blank:]]*\)\)\(\.pdb.*\)#\1\3\5\2mv *.* \4#
}' YourFile
信息:
/^[[:blank:]]*cp /,/^[[:blank:]]*\./
)直到./H
)/^[[:blank:]]*\./!d
)s/.*//;x
)提取段落的文件名,并使用mv
信息(s#^\(\(.[[:blank:]]*\)cp \)\([^[:blank:]]*/\([^[:blank:]]*\)\)\(\.pdb.*\)#\1\3\5\2mv *.* \4#
)将其添加到最后。这个正则表达式是一个特殊信息
H
而不是h
)让sed打印结果
awk版
awk -F '[/.]' '
/cp / {f[n++]=$2}
/\.\/run/ {print;sub( /\..*/, "mv *.* " f[n=0])}
7
' YourFile
信息:
.
和/
作为字段分隔符(-F '[/.]'
)/cp / {f[n++]=$2}
)./run
的每一行,/\.\/run/ {print;sub( /\..*/, "mv *.* " f[n=0])}
)
mv ...
n=0
)7
答案 1 :(得分:1)
此Perl方法需要命令行上输入文件的路径并将输出发送到stdout
use strict;
use warnings 'all';
local $/ = "";
while ( <> ) {
if ( m| \b cp \s+ (?: \w+ / )* (\w+) |x ) {
my $pdb = $1;
s/ .* \S \K /\nmv *.* $pdb/xs;
}
print;
}
cp abc/P_10_10A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_10A
cp abc/P_10_11A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11A
cp abc/P_10_11B.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11B
答案 2 :(得分:0)
这适用于data
中的给定perl
。
#!/usr/bin/perl
use strict;
use warnings;
open my $IN, "<", "test";
open my $OUT, ">", "test_new";
my $insert;
while (my $line = <$IN>){
chomp($line);
if($line =~ m/cp abc\/(.*).pdb lig.pdb$/){
$insert = $1;
}
if($line =~ m/ \.\/run\.pl/){
$line = $line."\n".' mv *.* '.$insert;
}
print $OUT $line."\n";
}
close $IN;
close $OUT;
<强>输出:强>
cp abc/P_10_10A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_10A
cp abc/P_10_11A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11A
cp abc/P_10_11B.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11B
答案 3 :(得分:0)
单线
awk -F'[/.]' '!s && /cp/{s=$2;}s && /\.\/run\.pl/{$0=$0 RS "mv *.* "s;s=""}1' file
<强>解释强>
awk -F'[/.]' ' # -F set field sep forward slash or dot
!s && /cp/{ # if s is not set and cp word found
s=$2 # Assign second column value to s
}
s && /\.\/run\.pl/{ # when s is set and ./run.pl found
$0 = $0 RS "mv *.* "s # append current record with mv *.* value to s
s="" # Reset s
}1 # perform default operation print $0
' file # Input file
<强>输入强>
$ cat f
cp abc/P_10_10A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
cp abc/P_10_11A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
cp abc/P_10_11B.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
<强>输出强>
$ awk -F'[/.]' '!s && /cp/{s=$2}s && /\.\/run\.pl/{$0 = $0 RS "mv *.* "s; s=""}1' f
cp abc/P_10_10A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_10A
cp abc/P_10_11A.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11A
cp abc/P_10_11B.pdb lig.pdb
cp abc/protein.pdbqt .
cp abc/run.pl .
./run.pl
mv *.* P_10_11B
对于空间修改此声明
$0=$0 RS " mv *.* "s;