首页 > 解决方案 > 使用 awk 更改多行和多列的值

问题描述

我有一个值列表。我想将特定行和第二列的值移动 0.2。那是-

1 2 3
4 5 6  
7 8 9

进入

1 2.2 3
4 5 6
7 8.2 9

……

for row in 1, 3,6,24,41,47,42,48,29,35,30,36,17,23,18,24
do
slight_up_COORD=`echo "$slight_up_COORD"|awk 'FNR =="${row}" {a=$2} FNR=="${row}" {$2=a+0.2} {print}'`
done

标签: bashawk

解决方案


您能否尝试以下操作,split您需要在哪里定义要添加行的所有行号.2。在这里我为1,3,6你做了可以在这里提到更多的行。

awk '
BEGIN{
  num=split("1,3,6",array,",")
  for(i=1;i<=num;i++){
    array1[array[i]]
  }
}
FNR in array1{
  $2+=.2
}
1
' Input_file

说明:在此处添加上述代码的详细说明。

awk '                               ##Starting awk program from here.
BEGIN{                              ##Starting BEGIN section of this awk program from here.
  num=split("1,3,6",array,",")      ##Splitting 1,3,6 values into array with delimiter comma and getting their total length(of passed line numbers) in variable named num here.
  for(i=1;i<=num;i++){              ##Starting a for Loop starting from i=1 to value of num
    array1[array[i]]                ##Creating array named array1 whose index is array[i] value.
  }
}
FNR in array1{                      ##Checking condition if current line number is present in array1 then do following.
  $2+=.2                            ##Adding .2 to current Line 2nd field here.
}
1                                   ##1 will print edited/non-edited lines here.
' Input_file                        ##Mentioning Input_file name here.

推荐阅读