bash Unix 代码想要更改列值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10641262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unix code wanted to change column values
提问by Abdel
I have a file that looks like this:
我有一个看起来像这样的文件:
ID1 ID2 3
ID2 ID3 3
ID4 ID5 3
ID6 ID7 4
ID8 ID9 4
ID8 ID6 4
And I want to change it to:
我想把它改成:
ID1 ID2 1
ID2 ID3 1
ID4 ID5 1
ID6 ID7 2
ID8 ID9 2
ID8 ID6 2
So I want to change all 3's and 4's from the third column to 1's and 2's respectively. What is the most efficient way to do this for a very large file? Many thanks in advance!
所以我想将第三列中的所有 3 和 4 分别更改为 1 和 2。对于非常大的文件,执行此操作的最有效方法是什么?提前谢谢了!
回答by Mark Reed
awk '{ -= 2; print }' filename >new_filename
Or, if you really only want to touch 3's and 4's:
或者,如果您真的只想触摸 3 和 4:
awk '{ if ( == 3 ) { = 1 } else if ( == 4 ) { = 2 }; print}' filename >new_filename
回答by aland
Assuming third column is always the last one:
假设第三列总是最后一列:
sed 's/\([^0-9]\)3$//;s/\([^0-9]\)4$//'

