1

在文件中的每一行之后添加额外的行

对于大约 1000 行文件的以下任务,我需要帮助。

输入

    ./create.pl     1eaj.out
    ./create.pl     1ezg.out
    ./create.pl     1f41.out
    ...

输出

    ./create.pl     1eaj.out
    mv complex.* 1eaj
    ./create.pl     1ezg.out
    mv complex.* 1ezg
    ./create.pl     1f41.out
    mv complex.* 1f41
    ...

我知道以下命令可以添加新行和第一部分,使输出如下所示。

    awk ' {print;} NR % 1 == 0 { print "mv complex.*  "; }'

    ./create.pl     1eaj.out
    mv complex.* 
    ./create.pl     1ezg.out
    mv complex.* 
    ./create.pl     1f41.out
    mv complex.* 
    ...

剩下的怎么办?提前非常感谢。

4

3 回答 3

3

我的尝试:

sed -n 's/^\(\.\/create\.pl\)\s*\(.*\)\.out$/\1 \2.out\nmv complex.* \2/p' s.txt

或使用&&between ./create.pland (因为 mv 只有在正确执行mv时才可能需要 ):./create.pl

sed -n 's/^\(\.\/create\.pl\)\s*\(.*\)\.out$/\1 \2.out \&\& mv complex.* \2/p' s.txt

这使:

./create.pl 1eaj.out && mv complex.* 1eaj
./create.pl 1ezg.out && mv complex.* 1ezg
./create.pl 1f41.out && mv complex.* 1f41
于 2013-06-27T08:04:24.893 回答
3

你快到了:

$ awk '{print $1, $2, "\nmv complex.*", $2}' file
./create.pl 1eaj.out 
mv complex.* 1eaj.out
./create.pl 1ezg.out 
mv complex.* 1ezg.out
./create.pl 1f41.out 
mv complex.* 1f41.out
于 2013-06-27T08:30:05.423 回答
2

使用空格或点作为分隔符来提取您需要的单词:

awk -F '[[:blank:].]+' '{print; print "mv complex.*", $4}' filename
于 2013-06-27T10:35:56.180 回答