我有一个巨大的文件dat文件(以逗号分隔)。其中我必须用“foo”替换第二列(使用双引号,如果第三列的值以“AB(带引号)
开头”以下是input.dat示例
"5000","ABC","AB8989"
"3000"," ","AB7676"
"2000"," ","A07444"
"4000"," ","BN0909"
这是我正在使用的命令:
awk -F, -vOFS=, '{if($3 ~/^"AB/){$2="foo"}print;}' input.dat
理想情况下,此命令应仅匹配前两行,但我不知道为什么它也匹配第3行。看起来它不是在注册前的A之后阅读B.请建议。
答案 0 :(得分:1)
这对我来说很好(我已经缩短了一些),你在用什么系统?
awk -F, -vOFS=, '$3 ~/^"AB/{$2="\"foo\""}1' input.dat
"5000","foo","AB8989"
"3000","foo","AB7676"
"2000"," ","A07444"
"4000"," ","BN0909"
PS我在"foo"