[英]How to generate a pattern of lines using a text editor in different columns
我的案文如下:
a text is present
for a few lines
which I dont want
to edit
1 2 ab
2 3 ab
3 4 ab
4 5 ab
5
6
7
8
現在,我想在第二列中生成數字的升序,並在第三列中填充字符串“ ab”,如下所示。
a text is present
for a few lines
which I dont want
to edit
1 2 ab
2 3 ab
3 4 ab
4 5 ab
5 6 ab
6 7 ab
7 8 ab
8 9 ab
這可能是您要尋找的:
$ awk '{$2=($2?$2:p[2]+1); $3=($3?$3:p[3]); split($0,p)} 1' file
1 2 ab
2 3 ab
3 4 ab
4 5 ab
5 6 ab
6 7 ab
7 8 ab
8 9 ab
鑒於您有新的要求在第100行開始上述轉換:
awk 'NR>99{$2=($2?$2:p[2]+1); $3=($3?$3:p[3]); split($0,p)} 1' file
另一個awk:
$ awk 'NR>5 && NF==1{$2=1+$1;$3="ab"}1' file
a text is present
for a few lines
which I dont want
to edit
1 2 ab
2 3 ab
3 4 ab
4 5 ab
5 6 ab
6 7 ab
7 8 ab
8 9 ab
您可以使用NR>5
或您想從哪一行開始控制起始記錄。
使用vim:
:g/\d\+\s*$/ %s:\v\d+ (\d+) ab\_s*\d\s*$\zs:\=printf(" %d ab",submatch(1)+1):
在vim中:
qa
:%s/\v( \d+.+)(\n\d+)$/\1\2\1/
w<C-a>
@a
q
@a
這是一個遞歸宏,它填充只有一列的第一行以及上一行的第二列和第三列,然后遞增第二列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.