bash - 使用 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
解决方案
您能否尝试以下操作,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.
推荐阅读
- javascript - 是否让匿名闭包替换为具有私有变量?
- c++ - 无法打开包含文件:'cxxabi.h':没有这样的文件或目录
- php - 如何在 PHP 中删除最后一个 html 实体
- mysql - 为什么第一个查询在数据库中发送空白数据而其他查询正常?
- java - 我需要从 MainActivity 取消在服务类中启动的 Timer,但得到空引用异常
- django - Django在布尔字段上注释
- googletest - 如何在 Google Test 的 case 方法中提取测试名称?
- java - 如何从应用程序中准确查找 Java 应用程序使用了多少内存
- laravel - sqlite 连接使用用户名和密码
- c++ - g++ 是否将 C 标准库的名称声明为 extern "C" 链接或 extern "C++"?