awk unexpectedly removes dot from string
I want to add a column (2nd place) in a .csv file and I want the values of that column to be strings and to be quoted;
The following command does add the column but without quotes:
awk -F"," 'BEGIN { OFS = "," } {$2="2.4.0"; print}' test.csv > output.csv
The following approach does incorporate the quotes, but for some reason it removes the last . (dot) from the value
awk -F"," 'BEGIN { OFS = "," } {$2="""2.4.0"""; print}' test.csv > output.csv
so my values end up being "2.40".
How should I go about this?
awk csv
add a comment |
I want to add a column (2nd place) in a .csv file and I want the values of that column to be strings and to be quoted;
The following command does add the column but without quotes:
awk -F"," 'BEGIN { OFS = "," } {$2="2.4.0"; print}' test.csv > output.csv
The following approach does incorporate the quotes, but for some reason it removes the last . (dot) from the value
awk -F"," 'BEGIN { OFS = "," } {$2="""2.4.0"""; print}' test.csv > output.csv
so my values end up being "2.40".
How should I go about this?
awk csv
If your file have 2 or more columns, you want to insert or replace the second column ?
– ctac_
6 hours ago
just insert a column after the first one
– pkaramol
6 hours ago
ok, so to insert, you must use : awk 'BEGIN{FS=OFS=","}{$1=$1","2.4.0""}1'
– ctac_
5 hours ago
or awk '{sub(",",","2.4.0",")}1'
– ctac_
5 hours ago
add a comment |
I want to add a column (2nd place) in a .csv file and I want the values of that column to be strings and to be quoted;
The following command does add the column but without quotes:
awk -F"," 'BEGIN { OFS = "," } {$2="2.4.0"; print}' test.csv > output.csv
The following approach does incorporate the quotes, but for some reason it removes the last . (dot) from the value
awk -F"," 'BEGIN { OFS = "," } {$2="""2.4.0"""; print}' test.csv > output.csv
so my values end up being "2.40".
How should I go about this?
awk csv
I want to add a column (2nd place) in a .csv file and I want the values of that column to be strings and to be quoted;
The following command does add the column but without quotes:
awk -F"," 'BEGIN { OFS = "," } {$2="2.4.0"; print}' test.csv > output.csv
The following approach does incorporate the quotes, but for some reason it removes the last . (dot) from the value
awk -F"," 'BEGIN { OFS = "," } {$2="""2.4.0"""; print}' test.csv > output.csv
so my values end up being "2.40".
How should I go about this?
awk csv
awk csv
asked 7 hours ago
pkaramolpkaramol
656520
656520
If your file have 2 or more columns, you want to insert or replace the second column ?
– ctac_
6 hours ago
just insert a column after the first one
– pkaramol
6 hours ago
ok, so to insert, you must use : awk 'BEGIN{FS=OFS=","}{$1=$1","2.4.0""}1'
– ctac_
5 hours ago
or awk '{sub(",",","2.4.0",")}1'
– ctac_
5 hours ago
add a comment |
If your file have 2 or more columns, you want to insert or replace the second column ?
– ctac_
6 hours ago
just insert a column after the first one
– pkaramol
6 hours ago
ok, so to insert, you must use : awk 'BEGIN{FS=OFS=","}{$1=$1","2.4.0""}1'
– ctac_
5 hours ago
or awk '{sub(",",","2.4.0",")}1'
– ctac_
5 hours ago
If your file have 2 or more columns, you want to insert or replace the second column ?
– ctac_
6 hours ago
If your file have 2 or more columns, you want to insert or replace the second column ?
– ctac_
6 hours ago
just insert a column after the first one
– pkaramol
6 hours ago
just insert a column after the first one
– pkaramol
6 hours ago
ok, so to insert, you must use : awk 'BEGIN{FS=OFS=","}{$1=$1","2.4.0""}1'
– ctac_
5 hours ago
ok, so to insert, you must use : awk 'BEGIN{FS=OFS=","}{$1=$1","2.4.0""}1'
– ctac_
5 hours ago
or awk '{sub(",",","2.4.0",")}1'
– ctac_
5 hours ago
or awk '{sub(",",","2.4.0",")}1'
– ctac_
5 hours ago
add a comment |
1 Answer
1
active
oldest
votes
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 }'
1
@roaima: From what I've know, it seems to have just cancelled out and just resulting in{ print 2.4.0 }or{ print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't
– Inian
6 hours ago
1
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:awk -v q='"' '... print q "2.4.0" q ...
– Thor
4 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504215%2fawk-unexpectedly-removes-dot-from-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 }'
1
@roaima: From what I've know, it seems to have just cancelled out and just resulting in{ print 2.4.0 }or{ print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't
– Inian
6 hours ago
1
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:awk -v q='"' '... print q "2.4.0" q ...
– Thor
4 hours ago
add a comment |
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 }'
1
@roaima: From what I've know, it seems to have just cancelled out and just resulting in{ print 2.4.0 }or{ print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't
– Inian
6 hours ago
1
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:awk -v q='"' '... print q "2.4.0" q ...
– Thor
4 hours ago
add a comment |
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 }'
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 }'
edited 6 hours ago
answered 7 hours ago
InianInian
4,7601228
4,7601228
1
@roaima: From what I've know, it seems to have just cancelled out and just resulting in{ print 2.4.0 }or{ print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't
– Inian
6 hours ago
1
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:awk -v q='"' '... print q "2.4.0" q ...
– Thor
4 hours ago
add a comment |
1
@roaima: From what I've know, it seems to have just cancelled out and just resulting in{ print 2.4.0 }or{ print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't
– Inian
6 hours ago
1
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:awk -v q='"' '... print q "2.4.0" q ...
– Thor
4 hours ago
1
1
@roaima: From what I've know, it seems to have just cancelled out and just resulting in
{ print 2.4.0 } or { print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't– Inian
6 hours ago
@roaima: From what I've know, it seems to have just cancelled out and just resulting in
{ print 2.4.0 } or { print 2.4.0 + 0 }, i.e. as a non-string constituent. I've tried to search fro relevant docs too, but couldn't– Inian
6 hours ago
1
1
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:
awk -v q='"' '... print q "2.4.0" q ...– Thor
4 hours ago
Another way of conveniently adding quotes that I sometimes use, is to define a variable, e.g.:
awk -v q='"' '... print q "2.4.0" q ...– Thor
4 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f504215%2fawk-unexpectedly-removes-dot-from-string%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
If your file have 2 or more columns, you want to insert or replace the second column ?
– ctac_
6 hours ago
just insert a column after the first one
– pkaramol
6 hours ago
ok, so to insert, you must use : awk 'BEGIN{FS=OFS=","}{$1=$1","2.4.0""}1'
– ctac_
5 hours ago
or awk '{sub(",",","2.4.0",")}1'
– ctac_
5 hours ago