如何编写具有特定条件的awk代码
我想创建一个对一行数据中一定数量的数据进行操作的代码,为此我只想计算负数,通过乘以负数本身使它们成为正数 示例
数据
10
11
-12
-13
-14
预期输出
10
11
144
169
196
这是我一直在尝试的,
awk 'int($0)<0 {$4 = int($0) + 360}
END {print $4}' data.txt
但我什至没有得到输出,任何人都可以帮助我吗?
I want to create a code that operates on a certain number of a row of data, for which I just want to count negative numbers to make them positive by multiplying by the number itself negative
example
data
10
11
-12
-13
-14
expected output
10
11
144
169
196
this is what I've been try
awk 'int($0)<0 {$4 = int($0) + 360}
END {print $4}' data.txt
but I don't even get the output, anyone can help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当第一个条件为负时,将值乘以自身。条件
1
始终为 true,因此该行将无条件打印。The first condition multiplies the value by itself when it's negative. The condition
1
is always true, so the line is printed unconditionally.还:
Also:
您还可以仅匹配以
-
开头的数字,在这种情况下,将它们各自相乘输出
You could also match only digits that start with
-
and in that case multiply them by themselvesOutput