how to increment a column value with 1 in a csv file
Clash Royale CLAN TAG#URR8PPP
I have a text file with 3 columns as below.
$ cat test.txt
1,A,300
1,B,300
1,C,300
Now i want to increment the third column only,
the output should be like below
1,A,300
1,B,301
1,C,302
Till now i have tried as,
awk -F, '$3=$3+1;print' OFS=, test.txt
But output is coming as,
1,A,301
1,B,301
1,C,301
Please do suggest, how to achieve the desired output?
awk
add a comment |
I have a text file with 3 columns as below.
$ cat test.txt
1,A,300
1,B,300
1,C,300
Now i want to increment the third column only,
the output should be like below
1,A,300
1,B,301
1,C,302
Till now i have tried as,
awk -F, '$3=$3+1;print' OFS=, test.txt
But output is coming as,
1,A,301
1,B,301
1,C,301
Please do suggest, how to achieve the desired output?
awk
1
or keep a separate variable for the amount to increment, and increment that variable for each line:$3 += inc; inc++
– glenn jackman
Feb 1 '17 at 18:36
add a comment |
I have a text file with 3 columns as below.
$ cat test.txt
1,A,300
1,B,300
1,C,300
Now i want to increment the third column only,
the output should be like below
1,A,300
1,B,301
1,C,302
Till now i have tried as,
awk -F, '$3=$3+1;print' OFS=, test.txt
But output is coming as,
1,A,301
1,B,301
1,C,301
Please do suggest, how to achieve the desired output?
awk
I have a text file with 3 columns as below.
$ cat test.txt
1,A,300
1,B,300
1,C,300
Now i want to increment the third column only,
the output should be like below
1,A,300
1,B,301
1,C,302
Till now i have tried as,
awk -F, '$3=$3+1;print' OFS=, test.txt
But output is coming as,
1,A,301
1,B,301
1,C,301
Please do suggest, how to achieve the desired output?
awk
awk
edited Feb 1 '17 at 18:35
glenn jackman
51.5k572111
51.5k572111
asked Feb 1 '17 at 18:23
swapneilswapneil
12
12
1
or keep a separate variable for the amount to increment, and increment that variable for each line:$3 += inc; inc++
– glenn jackman
Feb 1 '17 at 18:36
add a comment |
1
or keep a separate variable for the amount to increment, and increment that variable for each line:$3 += inc; inc++
– glenn jackman
Feb 1 '17 at 18:36
1
1
or keep a separate variable for the amount to increment, and increment that variable for each line:
$3 += inc; inc++
– glenn jackman
Feb 1 '17 at 18:36
or keep a separate variable for the amount to increment, and increment that variable for each line:
$3 += inc; inc++
– glenn jackman
Feb 1 '17 at 18:36
add a comment |
2 Answers
2
active
oldest
votes
awk -F, 'BEGIN offset=0; OFS="," print $1,$2,$3+offset++"' /path/to/input
or, alternatively:
awk -F, 'BEGIN OFS="," print $1,$2,$3+(NR-1)"' /path/to/input
add a comment |
Your initial approach with a small tweak:
awk -F',' 'BEGIN OFS = FS $3 = $3 + (NR-1); print ' test.txt
The NR
variable holds the number of records (lines) read so far.
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%2f341806%2fhow-to-increment-a-column-value-with-1-in-a-csv-file%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
awk -F, 'BEGIN offset=0; OFS="," print $1,$2,$3+offset++"' /path/to/input
or, alternatively:
awk -F, 'BEGIN OFS="," print $1,$2,$3+(NR-1)"' /path/to/input
add a comment |
awk -F, 'BEGIN offset=0; OFS="," print $1,$2,$3+offset++"' /path/to/input
or, alternatively:
awk -F, 'BEGIN OFS="," print $1,$2,$3+(NR-1)"' /path/to/input
add a comment |
awk -F, 'BEGIN offset=0; OFS="," print $1,$2,$3+offset++"' /path/to/input
or, alternatively:
awk -F, 'BEGIN OFS="," print $1,$2,$3+(NR-1)"' /path/to/input
awk -F, 'BEGIN offset=0; OFS="," print $1,$2,$3+offset++"' /path/to/input
or, alternatively:
awk -F, 'BEGIN OFS="," print $1,$2,$3+(NR-1)"' /path/to/input
answered Feb 1 '17 at 18:48
DopeGhotiDopeGhoti
45.5k55988
45.5k55988
add a comment |
add a comment |
Your initial approach with a small tweak:
awk -F',' 'BEGIN OFS = FS $3 = $3 + (NR-1); print ' test.txt
The NR
variable holds the number of records (lines) read so far.
add a comment |
Your initial approach with a small tweak:
awk -F',' 'BEGIN OFS = FS $3 = $3 + (NR-1); print ' test.txt
The NR
variable holds the number of records (lines) read so far.
add a comment |
Your initial approach with a small tweak:
awk -F',' 'BEGIN OFS = FS $3 = $3 + (NR-1); print ' test.txt
The NR
variable holds the number of records (lines) read so far.
Your initial approach with a small tweak:
awk -F',' 'BEGIN OFS = FS $3 = $3 + (NR-1); print ' test.txt
The NR
variable holds the number of records (lines) read so far.
answered Feb 1 '17 at 19:46
KusalanandaKusalananda
129k16244402
129k16244402
add a comment |
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%2f341806%2fhow-to-increment-a-column-value-with-1-in-a-csv-file%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
1
or keep a separate variable for the amount to increment, and increment that variable for each line:
$3 += inc; inc++
– glenn jackman
Feb 1 '17 at 18:36