You seem to have got the quotes wrong. You need to do as below
awk -F"," 'BEGIN { OFS = "," } {$2="\"2.4.0\""; print}' test.csv > output.csv
This is explained in the GNU awk man page - 3.2 Escape Sequences
Some characters cannot be included literally in string constants ("foo")
or regexp constants (/foo/
). Instead, they should be represented with escape sequences, which are character sequences beginning with a backslash (\
). One use of an escape sequence is to include a double-quote character in a string constant. Because a plain double quote ends the string, you must use \"
to represent an actual double-quote character as a part of the string..
As far as the reason I could understand the reason for the behavior, awk
seems to have interpreted 2.4.0
as a numeric word with the extra quotes from your OP and decides to lose the precision after the first dot.
i.e.
$2="\""2.4.0"\""
becomes just
$2=""2.4.0""
which awk
no longer understands as a string. You can reproduce this behavior by simply doing
awk 'BEGIN { print ""2.4.0"" }'
2.40
which happens to be the result when you do
awk 'BEGIN { print 2.4.0 + 0 }'