Changing last entries in a comma delimited list2019 Community Moderator ElectionHow to replace line with file contents and around the content with some text?Comparing 3 lines and 2 lines in BASHHow do I get bash to give a relevant error for a file with windows line-endings?Extract strings from between tags and save to new text fileReplace first column of file by command outputRemove all duplicate word from string using shell scriptHow do I prepend a line read from STDIN with an epoch timestamp?Replace string in a huge (70GB), one line, text fileJoining entries based off of column using awk/join
Why Shazam when there is already Superman?
What will be next at the bottom row and why?
What does chmod -u do?
Dealing with a rejection from a journal
Redundant comparison & "if" before assignment
Pre-mixing cryogenic fuels and using only one fuel tank
How does the math work for Perception checks?
Rising and falling intonation
The screen of my macbook suddenly broken down how can I do to recover
Temporarily disable WLAN internet access for children, but allow it for adults
Are paving bricks differently sized for sand bedding vs mortar bedding?
Explaining alternative travel routes when going to the USA
Find the Primitive Roots Mod 31
How could a planet have erratic days?
What was this official D&D 3.5e Lovecraft-flavored rulebook?
Terse Method to Swap Lowest for Highest?
Why did the EU agree to delay the Brexit deadline?
Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?
Travelling outside the UK without a passport
Why a symmetric relation is defined: ∀x∀y( xRy⟹yRx) and not ∀x∀y (xRy⟺yRx)?
Is it improper etiquette to ask your opponent what his/her rating is before the game?
What if a revenant (monster) gains fire resistance?
Not using 's' for he/she/it
Infinite dials to reset ever?
Changing last entries in a comma delimited list
2019 Community Moderator ElectionHow to replace line with file contents and around the content with some text?Comparing 3 lines and 2 lines in BASHHow do I get bash to give a relevant error for a file with windows line-endings?Extract strings from between tags and save to new text fileReplace first column of file by command outputRemove all duplicate word from string using shell scriptHow do I prepend a line read from STDIN with an epoch timestamp?Replace string in a huge (70GB), one line, text fileJoining entries based off of column using awk/join
I have a huge text file which look like this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
The desired output is this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
I have tried other relevant posts here and on other communities but could not exactly get what I want.
UPDATE
This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.
shell-script text-processing
add a comment |
I have a huge text file which look like this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
The desired output is this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
I have tried other relevant posts here and on other communities but could not exactly get what I want.
UPDATE
This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.
shell-script text-processing
add a comment |
I have a huge text file which look like this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
The desired output is this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
I have tried other relevant posts here and on other communities but could not exactly get what I want.
UPDATE
This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.
shell-script text-processing
I have a huge text file which look like this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
The desired output is this:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
I have tried other relevant posts here and on other communities but could not exactly get what I want.
UPDATE
This is the cross-question (I wanted both Unix/perl answers and batch/powershell solutions for this.) that has interesting answers.
shell-script text-processing
shell-script text-processing
edited Mar 12 at 22:00
M-M
asked Apr 13 '17 at 18:56
M-MM-M
15818
15818
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
-F,
- set comma ,
as field separator
$8
- points to the eighth field
%02d
- format which treats function argument as 2-digit number
Note, the last field in a record can be presented by $NF
.
NF is a predefined variable whose value is the number of fields in the current record
So, $NF
is the same as $8
(for your input)
awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file
1
You should useNF
to refer to the last field rather than hardcoding after counting.
– heemayl
Apr 14 '17 at 5:02
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
1
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo"1 2 3 4" | awk '$2=$2;print $0'
gives:1 2 3 4
(only 1 space (or OFS) left between fields). ex2)echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives:1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.
– Olivier Dulac
Apr 14 '17 at 12:38
add a comment |
You can try using awk
:
awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file
add a comment |
Here's perl solution:
$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
The -a
flag allows us to treat input as array, based on separator specified with -F
. Basically we alter last item in that array, and rebuild it via join
command.
Thank you for your answer. It does help if someone needs perl but stillsprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.
– M-M
Apr 13 '17 at 20:47
1
@Masoud well, main reason here is becausesprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't havesprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say
– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
add a comment |
With input data like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
in text.csv
the code below
awk -F"," ' i = 0;
MyOutLine = "";
j = NF - 1;
while ( i < j )
i++;
MyOutLine = MyOutLine""$i",";
i++;
x = sprintf( "%.2i", $i );
y = "MI-"x;
MyOutLine = MyOutLine""y;
print MyOutLine; ' ./text.csv
produces output like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
add a comment |
Tcl
Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file
set in [open input.csv]
set out [open output.csv w]
while ![eof $in]
set line [gets $in]
set last_comma_pos [string last , $line]
puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]
close $in
close $out
demonstration
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%2f358853%2fchanging-last-entries-in-a-comma-delimited-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
-F,
- set comma ,
as field separator
$8
- points to the eighth field
%02d
- format which treats function argument as 2-digit number
Note, the last field in a record can be presented by $NF
.
NF is a predefined variable whose value is the number of fields in the current record
So, $NF
is the same as $8
(for your input)
awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file
1
You should useNF
to refer to the last field rather than hardcoding after counting.
– heemayl
Apr 14 '17 at 5:02
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
1
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo"1 2 3 4" | awk '$2=$2;print $0'
gives:1 2 3 4
(only 1 space (or OFS) left between fields). ex2)echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives:1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.
– Olivier Dulac
Apr 14 '17 at 12:38
add a comment |
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
-F,
- set comma ,
as field separator
$8
- points to the eighth field
%02d
- format which treats function argument as 2-digit number
Note, the last field in a record can be presented by $NF
.
NF is a predefined variable whose value is the number of fields in the current record
So, $NF
is the same as $8
(for your input)
awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file
1
You should useNF
to refer to the last field rather than hardcoding after counting.
– heemayl
Apr 14 '17 at 5:02
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
1
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo"1 2 3 4" | awk '$2=$2;print $0'
gives:1 2 3 4
(only 1 space (or OFS) left between fields). ex2)echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives:1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.
– Olivier Dulac
Apr 14 '17 at 12:38
add a comment |
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
-F,
- set comma ,
as field separator
$8
- points to the eighth field
%02d
- format which treats function argument as 2-digit number
Note, the last field in a record can be presented by $NF
.
NF is a predefined variable whose value is the number of fields in the current record
So, $NF
is the same as $8
(for your input)
awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file
awk approach with sprintf function(to add leading zeros):
awk -F, -v OFS=',' '$8=sprintf("MI-%02d",$8);' file
The output:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
-F,
- set comma ,
as field separator
$8
- points to the eighth field
%02d
- format which treats function argument as 2-digit number
Note, the last field in a record can be presented by $NF
.
NF is a predefined variable whose value is the number of fields in the current record
So, $NF
is the same as $8
(for your input)
awk -F, -v OFS=',' '$(NF)=sprintf("MI-%02d", $(NF))' file
edited Apr 14 '17 at 6:47
answered Apr 13 '17 at 19:18
RomanPerekhrestRomanPerekhrest
23.2k12448
23.2k12448
1
You should useNF
to refer to the last field rather than hardcoding after counting.
– heemayl
Apr 14 '17 at 5:02
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
1
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo"1 2 3 4" | awk '$2=$2;print $0'
gives:1 2 3 4
(only 1 space (or OFS) left between fields). ex2)echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives:1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.
– Olivier Dulac
Apr 14 '17 at 12:38
add a comment |
1
You should useNF
to refer to the last field rather than hardcoding after counting.
– heemayl
Apr 14 '17 at 5:02
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
1
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo"1 2 3 4" | awk '$2=$2;print $0'
gives:1 2 3 4
(only 1 space (or OFS) left between fields). ex2)echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives:1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.
– Olivier Dulac
Apr 14 '17 at 12:38
1
1
You should use
NF
to refer to the last field rather than hardcoding after counting.– heemayl
Apr 14 '17 at 5:02
You should use
NF
to refer to the last field rather than hardcoding after counting.– heemayl
Apr 14 '17 at 5:02
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
@heemayl, added additional note
– RomanPerekhrest
Apr 14 '17 at 6:44
1
1
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo
"1 2 3 4" | awk '$2=$2;print $0'
gives: 1 2 3 4
(only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives: 1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.– Olivier Dulac
Apr 14 '17 at 12:38
A word of warning (irrelevant in this exemple, but could apply in other cases) : changing a value of one of the fields (here: $8) "recomputes" the whole line's fields, and have side effets: ex1: loses 'multiple separators': echo
"1 2 3 4" | awk '$2=$2;print $0'
gives: 1 2 3 4
(only 1 space (or OFS) left between fields). ex2) echo "1,,,2,3,,,,4" | awk -F',' '$2=$2;print $0'
gives: 1 2 3 4
(commas became spaces) . There could be other side effects. Test and take another approach (gsub on a copy variable of $0,for ex) if assiging a field have detrimental side effects.– Olivier Dulac
Apr 14 '17 at 12:38
add a comment |
You can try using awk
:
awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file
add a comment |
You can try using awk
:
awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file
add a comment |
You can try using awk
:
awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file
You can try using awk
:
awk 'BEGIN FS = OFS = "," $NF = sprintf("MI-%02d", $NF); 1' file
edited Apr 13 '17 at 19:28
answered Apr 13 '17 at 19:21
taliezintaliezin
6,88011728
6,88011728
add a comment |
add a comment |
Here's perl solution:
$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
The -a
flag allows us to treat input as array, based on separator specified with -F
. Basically we alter last item in that array, and rebuild it via join
command.
Thank you for your answer. It does help if someone needs perl but stillsprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.
– M-M
Apr 13 '17 at 20:47
1
@Masoud well, main reason here is becausesprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't havesprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say
– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
add a comment |
Here's perl solution:
$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
The -a
flag allows us to treat input as array, based on separator specified with -F
. Basically we alter last item in that array, and rebuild it via join
command.
Thank you for your answer. It does help if someone needs perl but stillsprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.
– M-M
Apr 13 '17 at 20:47
1
@Masoud well, main reason here is becausesprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't havesprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say
– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
add a comment |
Here's perl solution:
$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
The -a
flag allows us to treat input as array, based on separator specified with -F
. Basically we alter last item in that array, and rebuild it via join
command.
Here's perl solution:
$ perl -F',' -lane '$last=$#F;$F[$last]=sprintf("MI-%02d",$F[$last]);print join ",", @F' input.txt
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
The -a
flag allows us to treat input as array, based on separator specified with -F
. Basically we alter last item in that array, and rebuild it via join
command.
edited Apr 13 '17 at 20:44
answered Apr 13 '17 at 20:13
Sergiy KolodyazhnyySergiy Kolodyazhnyy
10.6k42763
10.6k42763
Thank you for your answer. It does help if someone needs perl but stillsprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.
– M-M
Apr 13 '17 at 20:47
1
@Masoud well, main reason here is becausesprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't havesprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say
– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
add a comment |
Thank you for your answer. It does help if someone needs perl but stillsprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.
– M-M
Apr 13 '17 at 20:47
1
@Masoud well, main reason here is becausesprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't havesprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say
– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
Thank you for your answer. It does help if someone needs perl but still
sprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.– M-M
Apr 13 '17 at 20:47
Thank you for your answer. It does help if someone needs perl but still
sprintf
is the core idea of your answer. Not like if it's not right, just not offering something different than accepted answer. +1 anyways.– M-M
Apr 13 '17 at 20:47
1
1
@Masoud well, main reason here is because
sprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
@Masoud well, main reason here is because
sprintf()
is used typically when writing a string of specific format to a variable, which is why it is used in many other languages. I can write it in Python as well - Python doesn't have sprintf()
but the core idea will be the same regardless - writing formatted string to a variable. Alternatively, we can operate on array items directly and just print those. With this type of questions there is finite amount of solutions, basically is what I'm trying to say– Sergiy Kolodyazhnyy
Apr 13 '17 at 20:56
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
Agreed! Your point stands corrected.
– M-M
Apr 13 '17 at 21:04
add a comment |
With input data like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
in text.csv
the code below
awk -F"," ' i = 0;
MyOutLine = "";
j = NF - 1;
while ( i < j )
i++;
MyOutLine = MyOutLine""$i",";
i++;
x = sprintf( "%.2i", $i );
y = "MI-"x;
MyOutLine = MyOutLine""y;
print MyOutLine; ' ./text.csv
produces output like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
add a comment |
With input data like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
in text.csv
the code below
awk -F"," ' i = 0;
MyOutLine = "";
j = NF - 1;
while ( i < j )
i++;
MyOutLine = MyOutLine""$i",";
i++;
x = sprintf( "%.2i", $i );
y = "MI-"x;
MyOutLine = MyOutLine""y;
print MyOutLine; ' ./text.csv
produces output like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
add a comment |
With input data like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
in text.csv
the code below
awk -F"," ' i = 0;
MyOutLine = "";
j = NF - 1;
while ( i < j )
i++;
MyOutLine = MyOutLine""$i",";
i++;
x = sprintf( "%.2i", $i );
y = "MI-"x;
MyOutLine = MyOutLine""y;
print MyOutLine; ' ./text.csv
produces output like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
With input data like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,3
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,8
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,12
in text.csv
the code below
awk -F"," ' i = 0;
MyOutLine = "";
j = NF - 1;
while ( i < j )
i++;
MyOutLine = MyOutLine""$i",";
i++;
x = sprintf( "%.2i", $i );
y = "MI-"x;
MyOutLine = MyOutLine""y;
print MyOutLine; ' ./text.csv
produces output like:
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-03
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-08
36,53,90478,0.58699759849,0.33616,4.83449759849,0.0695335954050315,MI-14
36,53,15596,0.58454577855,0.26119,2.24878677855,0.116147072052964,MI-12
edited Apr 13 '17 at 19:28
answered Apr 13 '17 at 19:21
NormNorm
92
92
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
add a comment |
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
Thank you for your answer. It works fine but I just decided to go with Roman's response since it's just easier to comprehend.
– M-M
Apr 13 '17 at 19:39
add a comment |
Tcl
Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file
set in [open input.csv]
set out [open output.csv w]
while ![eof $in]
set line [gets $in]
set last_comma_pos [string last , $line]
puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]
close $in
close $out
demonstration
add a comment |
Tcl
Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file
set in [open input.csv]
set out [open output.csv w]
while ![eof $in]
set line [gets $in]
set last_comma_pos [string last , $line]
puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]
close $in
close $out
demonstration
add a comment |
Tcl
Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file
set in [open input.csv]
set out [open output.csv w]
while ![eof $in]
set line [gets $in]
set last_comma_pos [string last , $line]
puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]
close $in
close $out
demonstration
Tcl
Here is my solution, done using Tcl which reads from input.csv file and puts the result in output.csv file
set in [open input.csv]
set out [open output.csv w]
while ![eof $in]
set line [gets $in]
set last_comma_pos [string last , $line]
puts $out [string range $line 0 $last_comma_pos][format MI-%02d [string range $line $last_comma_pos+1 end]]
close $in
close $out
demonstration
answered Apr 14 '17 at 0:47
sergiolsergiol
1013
1013
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%2f358853%2fchanging-last-entries-in-a-comma-delimited-list%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
-shell-script, text-processing