문제

I have a huge file dat file(comma separated). In which I have to replace 2nd column with "foo"(with double quotes, if 3rd column has value starting with "AB(with quote)

Here is the sample input.dat

"5000","ABC","AB8989"
"3000","   ","AB7676"
"2000","   ","A07444"
"4000","   ","BN0909"

This is the command I am using:

awk -F, -vOFS=, '{if($3 ~/^"AB/){$2="foo"}print;}' input.dat

This command ideally should match only 1st two rows, but I don't know why it is matching the 3rd line also. Looks like it is not reading B after A in reg ex. Please suggest.

도움이 되었습니까?

해결책

This looks fine for me (I have shorten it some), what system are you on?

awk -F, -vOFS=, '$3 ~/^"AB/{$2="\"foo\""}1' input.dat
"5000","foo","AB8989"
"3000","foo","AB7676"
"2000","   ","A07444"
"4000","   ","BN0909"

PS I added double quotes to "foo"

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top