根据模式条件添加2行的元素
Add the elements of 2 rows based on pattern condition
我想根据模式添加 2 行
我有这个table
1 - 513 1478 966 1
2 - 1594 2130 537 1
3 + 2171 2539 369 1
4 - 2587 3159 573 1
我正在寻找的是添加一个 $7 列,第一个元素以 0 开头,如果 $2 是“-”,则从 $7 中减去 -1,否则在 $7 中添加 +1
像这样:
1 - 513 1478 966 1 -1
2 - 1594 2130 537 1 -2
3 + 2171 2539 369 1 -1
4 - 2587 3159 573 1 -2 `
这是我写的
awk '==0,i=1;{for i in do {if(="-"){=+1}else{=-1} done print}'
我的代码的问题是,如果我删除 for
条件,则整个 $2 变为 - 而整个 $7 为 -1
您的代码根本不起作用。它抱怨几个语法错误。无论如何,我认为你是在想这个问题。如果我没理解错的话,解决方法更简单:
awk 'BEGIN {v=0} {if (=="-") {v=v-1} else {v=v+1}; =v; print}'
使用 var v
来保留最后一个值,并根据 </code> 内容添加或减去一个值。 <code>v
更新后,将其分配给 </code> 并打印整个记录。在下一行中,您已经有了 <code>v
.
中第七列的最后一个值
这应该像下面这样简单。
awk 'BEGIN{OFS="\t\t"} {="1";$(NF+1)=*$NF+prev;prev=$NF} 1' Input_file
简要说明:
- 将
1
添加到每行的 </code> 第二个字段值。</li>
<li>将 <code>
的值与最后一个字段相乘(这意味着将 1
与 +ve 或 -ve 相乘到最后一个字段)并将其值保存到新创建的最后一个字段。
- 现在根据 OP 的问题在此处添加上一行的最后一个字段值。
- 将当前最后一个字段(使用
$(NF+1)
新创建的字段)保存到 prev
变量,以便它可以用于添加到下一行的计算中。
详细解释:
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this awk program from here.
OFS="\t\t" ##Setting value of 2 times TAB for each line here.
} ##Close BEGIN section of this code here.
{
="1" ##Concatenating 1 to value of here.
$(NF+1)=*$NF+prev ##Creating new last field whose value is *$NF and adding prev variable to it.
prev=$NF ##Setting current last field value to variable prev here.
}
1 ##Printing edited/non-edited lines here.
' Input_file ##mentioning Input_file name here.
提供的示例的输出如下。
1 -1 513 1478 966 1 -1
2 -1 1594 2130 537 1 -2
3 +1 2171 2539 369 1 -1
4 -1 2587 3159 573 1 -2
使用@RavinderSingh13 的技巧
$ awk '{print [=10=] "\t" (c+="1")}' file
1 - 513 1478 966 1 -1
2 - 1594 2130 537 1 -2
3 + 2171 2539 369 1 -1
4 - 2587 3159 573 1 -2
我想根据模式添加 2 行
我有这个table
1 - 513 1478 966 1
2 - 1594 2130 537 1
3 + 2171 2539 369 1
4 - 2587 3159 573 1
我正在寻找的是添加一个 $7 列,第一个元素以 0 开头,如果 $2 是“-”,则从 $7 中减去 -1,否则在 $7 中添加 +1 像这样:
1 - 513 1478 966 1 -1
2 - 1594 2130 537 1 -2
3 + 2171 2539 369 1 -1
4 - 2587 3159 573 1 -2 `
这是我写的
awk '==0,i=1;{for i in do {if(="-"){=+1}else{=-1} done print}'
我的代码的问题是,如果我删除 for
条件,则整个 $2 变为 - 而整个 $7 为 -1
您的代码根本不起作用。它抱怨几个语法错误。无论如何,我认为你是在想这个问题。如果我没理解错的话,解决方法更简单:
awk 'BEGIN {v=0} {if (=="-") {v=v-1} else {v=v+1}; =v; print}'
使用 var v
来保留最后一个值,并根据 </code> 内容添加或减去一个值。 <code>v
更新后,将其分配给 </code> 并打印整个记录。在下一行中,您已经有了 <code>v
.
这应该像下面这样简单。
awk 'BEGIN{OFS="\t\t"} {="1";$(NF+1)=*$NF+prev;prev=$NF} 1' Input_file
简要说明:
- 将
1
添加到每行的</code> 第二个字段值。</li> <li>将 <code>
的值与最后一个字段相乘(这意味着将1
与 +ve 或 -ve 相乘到最后一个字段)并将其值保存到新创建的最后一个字段。 - 现在根据 OP 的问题在此处添加上一行的最后一个字段值。
- 将当前最后一个字段(使用
$(NF+1)
新创建的字段)保存到prev
变量,以便它可以用于添加到下一行的计算中。
详细解释:
awk ' ##Starting awk program from here.
BEGIN{ ##Starting BEGIN section of this awk program from here.
OFS="\t\t" ##Setting value of 2 times TAB for each line here.
} ##Close BEGIN section of this code here.
{
="1" ##Concatenating 1 to value of here.
$(NF+1)=*$NF+prev ##Creating new last field whose value is *$NF and adding prev variable to it.
prev=$NF ##Setting current last field value to variable prev here.
}
1 ##Printing edited/non-edited lines here.
' Input_file ##mentioning Input_file name here.
提供的示例的输出如下。
1 -1 513 1478 966 1 -1
2 -1 1594 2130 537 1 -2
3 +1 2171 2539 369 1 -1
4 -1 2587 3159 573 1 -2
使用@RavinderSingh13 的技巧
$ awk '{print [=10=] "\t" (c+="1")}' file
1 - 513 1478 966 1 -1
2 - 1594 2130 537 1 -2
3 + 2171 2539 369 1 -1
4 - 2587 3159 573 1 -2