我有一个包含
形式的行的文件this is block 1
a 1 2 3
this is block 2
a 3 1 9
this is block 3
a 10 2 32
...
我想复制一些行并替换开头,使其成为:
this is block 1
a 1 2 3
b 1 2 3
c 1 2 3
this is block 2
a 3 1 9
b 3 1 9
c 3 1 9
this is block 3
a 10 2 32
b 10 2 32
c 10 2 32
...
不确定如何使用awk,sed或其他优雅选项解决此问题。
答案 0 :(得分:1)
使用sed
或awk
,您可以这样做:
$ cat data
this is block 1
a 1 2 3
this is block 2
a 3 1 9
this is block 3
a 10 2 32
$ sed -rn '/^a/{p;s/^a/b/;p;s/^b/c/;p;n};p' data
this is block 1
a 1 2 3
b 1 2 3
c 1 2 3
this is block 2
a 3 1 9
b 3 1 9
c 3 1 9
this is block 3
a 10 2 32
b 10 2 32
c 10 2 32
$ awk '{if($1=="a") {print;$1="b";print;$1="c";print} else {print}}' data
this is block 1
a 1 2 3
b 1 2 3
c 1 2 3
this is block 2
a 3 1 9
b 3 1 9
c 3 1 9
this is block 3
a 10 2 32
b 10 2 32
c 10 2 32
$