How to Iterate through rows and append incremental number to duplicates?










0















I have an environment of several hundred autoscaled servers and server host file that gets generated every few minutes, with a row per server with the 3 columns:





eg.



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.6 upload-server upload
10.10.1.7 editing-server edit
10.10.1.8 editing-server edit
10.10.1.9 data-storage-server-01 data-01


also, the list is random and unsorted.



pretty simple env, so dont want to over-engineer the problem with fancy self-reporting dns or whatever on boot or termination. they're rarely accessed but if so its via a jumpbox, just want that server to always have correct host info and short tagged names for easy connection



servers that are always singular, have a number on the end of them. servers without a number scale up and down all the time, and these are the problem ones, because they all get tagged with the same name.



need to sort the rows, then ignore any row ending with a number, and for any other row where the 3rd column has no number and there are duplicate results, they need a number incremented and appended to the end.



eg. end result:



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01









share|improve this question

















  • 1





    what have you tried?.

    – stack0114106
    Nov 16 '18 at 5:44











  • is the file sorted by ip address in the first column?..

    – stack0114106
    Nov 16 '18 at 5:48















0















I have an environment of several hundred autoscaled servers and server host file that gets generated every few minutes, with a row per server with the 3 columns:





eg.



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.6 upload-server upload
10.10.1.7 editing-server edit
10.10.1.8 editing-server edit
10.10.1.9 data-storage-server-01 data-01


also, the list is random and unsorted.



pretty simple env, so dont want to over-engineer the problem with fancy self-reporting dns or whatever on boot or termination. they're rarely accessed but if so its via a jumpbox, just want that server to always have correct host info and short tagged names for easy connection



servers that are always singular, have a number on the end of them. servers without a number scale up and down all the time, and these are the problem ones, because they all get tagged with the same name.



need to sort the rows, then ignore any row ending with a number, and for any other row where the 3rd column has no number and there are duplicate results, they need a number incremented and appended to the end.



eg. end result:



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01









share|improve this question

















  • 1





    what have you tried?.

    – stack0114106
    Nov 16 '18 at 5:44











  • is the file sorted by ip address in the first column?..

    – stack0114106
    Nov 16 '18 at 5:48













0












0








0








I have an environment of several hundred autoscaled servers and server host file that gets generated every few minutes, with a row per server with the 3 columns:





eg.



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.6 upload-server upload
10.10.1.7 editing-server edit
10.10.1.8 editing-server edit
10.10.1.9 data-storage-server-01 data-01


also, the list is random and unsorted.



pretty simple env, so dont want to over-engineer the problem with fancy self-reporting dns or whatever on boot or termination. they're rarely accessed but if so its via a jumpbox, just want that server to always have correct host info and short tagged names for easy connection



servers that are always singular, have a number on the end of them. servers without a number scale up and down all the time, and these are the problem ones, because they all get tagged with the same name.



need to sort the rows, then ignore any row ending with a number, and for any other row where the 3rd column has no number and there are duplicate results, they need a number incremented and appended to the end.



eg. end result:



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01









share|improve this question














I have an environment of several hundred autoscaled servers and server host file that gets generated every few minutes, with a row per server with the 3 columns:





eg.



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.6 upload-server upload
10.10.1.7 editing-server edit
10.10.1.8 editing-server edit
10.10.1.9 data-storage-server-01 data-01


also, the list is random and unsorted.



pretty simple env, so dont want to over-engineer the problem with fancy self-reporting dns or whatever on boot or termination. they're rarely accessed but if so its via a jumpbox, just want that server to always have correct host info and short tagged names for easy connection



servers that are always singular, have a number on the end of them. servers without a number scale up and down all the time, and these are the problem ones, because they all get tagged with the same name.



need to sort the rows, then ignore any row ending with a number, and for any other row where the 3rd column has no number and there are duplicate results, they need a number incremented and appended to the end.



eg. end result:



10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01






bash scripting increment






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 5:22









geniestacksgeniestacks

184




184







  • 1





    what have you tried?.

    – stack0114106
    Nov 16 '18 at 5:44











  • is the file sorted by ip address in the first column?..

    – stack0114106
    Nov 16 '18 at 5:48












  • 1





    what have you tried?.

    – stack0114106
    Nov 16 '18 at 5:44











  • is the file sorted by ip address in the first column?..

    – stack0114106
    Nov 16 '18 at 5:48







1




1





what have you tried?.

– stack0114106
Nov 16 '18 at 5:44





what have you tried?.

– stack0114106
Nov 16 '18 at 5:44













is the file sorted by ip address in the first column?..

– stack0114106
Nov 16 '18 at 5:48





is the file sorted by ip address in the first column?..

– stack0114106
Nov 16 '18 at 5:48












1 Answer
1






active

oldest

votes


















1














Check this out:



$ cat genie1.txt
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.8 editing-server edit
10.10.1.6 upload-server upload
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.9 data-storage-server-01 data-01
10.10.1.7 editing-server edit

$ sort -k2,3 -k1 -n genie1.txt | perl -F"s+" -lane ' if( /(.+?)(?<![0-9])$/) $k="$F[1] $F[2]";$col3$k++; printf("%s %s %s-%02dn",$F[0],$F[1],$F[2],$col3$k)
else print '
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01

$





share|improve this answer























  • thank you, some perl wizardry was exactly the ticket!

    – geniestacks
    Nov 19 '18 at 0:09










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
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: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
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
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331888%2fhow-to-iterate-through-rows-and-append-incremental-number-to-duplicates%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









1














Check this out:



$ cat genie1.txt
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.8 editing-server edit
10.10.1.6 upload-server upload
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.9 data-storage-server-01 data-01
10.10.1.7 editing-server edit

$ sort -k2,3 -k1 -n genie1.txt | perl -F"s+" -lane ' if( /(.+?)(?<![0-9])$/) $k="$F[1] $F[2]";$col3$k++; printf("%s %s %s-%02dn",$F[0],$F[1],$F[2],$col3$k)
else print '
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01

$





share|improve this answer























  • thank you, some perl wizardry was exactly the ticket!

    – geniestacks
    Nov 19 '18 at 0:09















1














Check this out:



$ cat genie1.txt
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.8 editing-server edit
10.10.1.6 upload-server upload
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.9 data-storage-server-01 data-01
10.10.1.7 editing-server edit

$ sort -k2,3 -k1 -n genie1.txt | perl -F"s+" -lane ' if( /(.+?)(?<![0-9])$/) $k="$F[1] $F[2]";$col3$k++; printf("%s %s %s-%02dn",$F[0],$F[1],$F[2],$col3$k)
else print '
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01

$





share|improve this answer























  • thank you, some perl wizardry was exactly the ticket!

    – geniestacks
    Nov 19 '18 at 0:09













1












1








1







Check this out:



$ cat genie1.txt
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.8 editing-server edit
10.10.1.6 upload-server upload
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.9 data-storage-server-01 data-01
10.10.1.7 editing-server edit

$ sort -k2,3 -k1 -n genie1.txt | perl -F"s+" -lane ' if( /(.+?)(?<![0-9])$/) $k="$F[1] $F[2]";$col3$k++; printf("%s %s %s-%02dn",$F[0],$F[1],$F[2],$col3$k)
else print '
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01

$





share|improve this answer













Check this out:



$ cat genie1.txt
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.8 editing-server edit
10.10.1.6 upload-server upload
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload
10.10.1.5 upload-server upload
10.10.1.9 data-storage-server-01 data-01
10.10.1.7 editing-server edit

$ sort -k2,3 -k1 -n genie1.txt | perl -F"s+" -lane ' if( /(.+?)(?<![0-9])$/) $k="$F[1] $F[2]";$col3$k++; printf("%s %s %s-%02dn",$F[0],$F[1],$F[2],$col3$k)
else print '
10.10.1.2 video-server-usa-01 vs-usa-01
10.10.1.3 video-server-jap-01 vs-jap-01
10.10.1.4 upload-server upload-01
10.10.1.5 upload-server upload-02
10.10.1.6 upload-server upload-03
10.10.1.7 editing-server edit-01
10.10.1.8 editing-server edit-02
10.10.1.9 data-storage-server-01 data-01

$






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 6:15









stack0114106stack0114106

4,8322423




4,8322423












  • thank you, some perl wizardry was exactly the ticket!

    – geniestacks
    Nov 19 '18 at 0:09

















  • thank you, some perl wizardry was exactly the ticket!

    – geniestacks
    Nov 19 '18 at 0:09
















thank you, some perl wizardry was exactly the ticket!

– geniestacks
Nov 19 '18 at 0:09





thank you, some perl wizardry was exactly the ticket!

– geniestacks
Nov 19 '18 at 0:09



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • 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.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53331888%2fhow-to-iterate-through-rows-and-append-incremental-number-to-duplicates%23new-answer', 'question_page');

);

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







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線