Frage

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.

War es hilfreich?

Lösung

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"

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top