linux - How to subtract each value in a column from same number in a .txt file in Linux?
问题描述
I have a a .txt file (mydata.txt) in Linux and want to subtract each value in one column and save the output values in a new column alongside with the old column. For example, in the data example below, I subtract each value in C1 column from 2 and saved the output column (C1_sub) as a new column, output of C2 in column C2_sub and so on. How do I do it if I have one or multiple columns?
C1 C1_sub C2 C2_sub C3 C3_sub
1 1 2 0 2 0
0 2 1 1 2 0
2 0 0 2 0 2
0 2 2 0 1 1
0.008 1.992 0 2 2 0
1.999 0.001 1 1 0 2
0 2 2 0 1 1
0 2 0.001 1.999 2 0
1 1 1 1 0 2
2 0 2 0 0.013 1.987
0 2 0.999 1.001 0 2
0 2 2 0 1 1
1 1 1.999 0.001 2 0
1.99 0.01 1.999 0.001 2 0
0 2 1.999 0.001 1 1
解决方案
awk 'NR==1{print "C1", "C1_sub", "C2", "C2_sub", "C3", "C3_sub"; next}
{ print $1, 2 - $1,$2, 2 - $2, $3, 2-$3}' OFS=\\t input
推荐阅读
- jquery - jQuery page scroll not working since update
- javascript - 如何在 Reactjs 中渲染 html 元素?
- google-cloud-firestore - How to query firestore with the Dialogflow inline editor to get information
- ignite - How to configure NLog file-based logging in app.config file
- c# - ctrl c, ctrl v 在 DataGridView 上
- python - WindowsContext: OleInitialize() failed: "COM error 0x80010106 RPC_E_CHANGED_MODE (Unknown error 0x0ffffffff80010106)"
- arrays - Swift:追加到字典中返回随机
- svn - 如何仅签出给定父目录的特定子目录未签出
- javascript - 发布空正文(长度为 0 的字符串)
- ionic-framework - ERROR 错误:尝试比较“[object Object]”时出错。ionic 中只允许使用数组和可迭代对象