Linux awk:仅在特定字段中查找和替换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14054791/
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
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:08:29 来源:igfitidea点击:
awk: find and replace in certain field only
提问by Yishu Fang
I have a text file like this:
我有一个这样的文本文件:
$ cat test
12 13 2100 s
12 13 3100 s
100 13 100 s
12 13 300 s
I want the output to be like this:
我希望输出是这样的:
$ cat test
12 13 22000 s
12 13 32000 s
100 13 2000 s
12 13 300 s
I only want to replace 100in field 3 (once 100is contained in $3) into 2000. How can I accomplish this job using awk?
我只想将100字段 3(曾经100包含在 中$3)替换为2000. 我怎样才能完成这项工作awk?
采纳答案by Steve
Here's one way using awk:
这是使用的一种方法awk:
awk '{ sub(/100$/, "2000", ) }1' file
Results:
结果:
12 13 22000 s
12 13 32000 s
100 13 2000 s
12 13 300 s
回答by Vijay
awk '~/100/{gsub(/100/,"2000",)}1' your_file
回答by anishsane
Try:
尝试:
awk '{=gensub(100,2000,1,);print}' test.txt

