Copy digits from end of string to anotherHow to permanently change a file using awk? (“in-place” edits,...

Why isn't Bash trap working if output is redirected to stdout?

Options basics: How to realize profit from a long call position

Why did the World Bank set the global poverty line at $1.90?

Do you have to have figures when playing D&D?

Grep Match and extract

What is Gilligan's full Name?

Use 1 9 6 2 in this order to make 75

How to get depth and other lengths of a font?

Canada travel to US using Global Entry

Convert only certain words to lowercase

Oil draining out shortly after turbo hose detached/broke

Seasonality after 1st differencing

Rail-to-rail op-amp only reaches 90% of VCC, works sometimes, not everytime

bash vs. zsh: What are the practical differences?

What do Birth, Age, and Death mean in the first noble truth?

If absolute velocity does not exist, how can we say a rocket accelerates in empty space?

How (un)safe is it to ride barefoot?

Suppose leased car is totalled: what are financial implications?

If there's something that implicates the president why is there then a national security issue? (John Dowd)

Why is long-term living in Almost-Earth causing severe health problems?

Do you need to let the DM know when you are multiclassing?

The origin of the Russian proverb about two hares

Should I put programming books I wrote a few years ago on my resume?

Why is the length of the Kelvin unit of temperature equal to that of the Celsius unit?



Copy digits from end of string to another


How to permanently change a file using awk? (“in-place” edits, as with “sed -i”)sed remove end of line for specific linesPrint a line in stdout that matches an expression if the output contains another expressionSplit string into array and print each element on a new line with commandlineIs there any way to Copy and Paste from one file(from beginning to end) to another using SED and AWK?sed remove digits from end of stirngawk remove lines with digits at endExtract words from multiple strings using AWK/SEDRemove “n” string from end of certain linesFiltering list of numbers containing sequential digitsHow to extract second element from path?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















How could I copy digits from one end of a string to another end of a string? So example,



Input -



Example123:Hello
Exp12:Hey1
Exp:heylo


expected output -



Example123:Hello123
Exp12:Hey112
Exp:heylo


I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits










share|improve this question









New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.

    – Stéphane Chazelas
    1 hour ago


















0















How could I copy digits from one end of a string to another end of a string? So example,



Input -



Example123:Hello
Exp12:Hey1
Exp:heylo


expected output -



Example123:Hello123
Exp12:Hey112
Exp:heylo


I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits










share|improve this question









New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2





    From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.

    – Stéphane Chazelas
    1 hour ago














0












0








0








How could I copy digits from one end of a string to another end of a string? So example,



Input -



Example123:Hello
Exp12:Hey1
Exp:heylo


expected output -



Example123:Hello123
Exp12:Hey112
Exp:heylo


I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits










share|improve this question









New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











How could I copy digits from one end of a string to another end of a string? So example,



Input -



Example123:Hello
Exp12:Hey1
Exp:heylo


expected output -



Example123:Hello123
Exp12:Hey112
Exp:heylo


I'm open to using sed or awk, seperator must be accounted for, so row1 is the row to extract digits from and row 2 is the row to place digits







awk sed






share|improve this question









New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










share|improve this question









New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








share|improve this question




share|improve this question








edited 1 hour ago









Sparhawk

10.7k848102




10.7k848102






New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








asked 2 hours ago









user357075user357075

1




1




New contributor



user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




New contributor




user357075 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.










  • 2





    From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.

    – Stéphane Chazelas
    1 hour ago














  • 2





    From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.

    – Stéphane Chazelas
    1 hour ago








2




2





From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.

– Stéphane Chazelas
1 hour ago





From your expected output, it seems you want to copy, not swap those digits from the end of the first field to the end of the line.

– Stéphane Chazelas
1 hour ago










3 Answers
3






active

oldest

votes


















3














Assuming there's one and only one occurrence of : in each line of the input, you could do something like:



sed 's/([[:digit:]]*):.*/&1/' < input


If there can be more than one : (and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:



sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input





share|improve this answer

































    1














    You can use awk here



    awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}' 


    Explanation



    -F':': tells awk to use colon as the separator



    j=$1; gsub(/[^0-9]+/,"",j) : assigns the first column to to a temporary variable j, and then removes anything that is not a digit from j



    printf "%s%sn",$0,j;}' : finally prints the original string appended with j that carries our number






    share|improve this answer































      1














      With the match() function of GNU awk to store the matching group in an array, you could do



      awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file


      On any POSIX compliant awk you could do



      awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file


      See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.






      share|improve this answer


























        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
        });


        }
        });






        user357075 is a new contributor. Be nice, and check out our Code of Conduct.










        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523920%2fcopy-digits-from-end-of-string-to-another%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        3














        Assuming there's one and only one occurrence of : in each line of the input, you could do something like:



        sed 's/([[:digit:]]*):.*/&1/' < input


        If there can be more than one : (and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:



        sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input





        share|improve this answer






























          3














          Assuming there's one and only one occurrence of : in each line of the input, you could do something like:



          sed 's/([[:digit:]]*):.*/&1/' < input


          If there can be more than one : (and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:



          sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input





          share|improve this answer




























            3












            3








            3







            Assuming there's one and only one occurrence of : in each line of the input, you could do something like:



            sed 's/([[:digit:]]*):.*/&1/' < input


            If there can be more than one : (and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:



            sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input





            share|improve this answer















            Assuming there's one and only one occurrence of : in each line of the input, you could do something like:



            sed 's/([[:digit:]]*):.*/&1/' < input


            If there can be more than one : (and you want to append the digits to the end of the line, not the second field), that becomes more complicated, like:



            sed 's/^([^:]*[^:[:digit:]]){0,1}([[:digit:]]*):.*/&2/' < input






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago

























            answered 1 hour ago









            Stéphane ChazelasStéphane Chazelas

            321k57613983




            321k57613983

























                1














                You can use awk here



                awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}' 


                Explanation



                -F':': tells awk to use colon as the separator



                j=$1; gsub(/[^0-9]+/,"",j) : assigns the first column to to a temporary variable j, and then removes anything that is not a digit from j



                printf "%s%sn",$0,j;}' : finally prints the original string appended with j that carries our number






                share|improve this answer




























                  1














                  You can use awk here



                  awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}' 


                  Explanation



                  -F':': tells awk to use colon as the separator



                  j=$1; gsub(/[^0-9]+/,"",j) : assigns the first column to to a temporary variable j, and then removes anything that is not a digit from j



                  printf "%s%sn",$0,j;}' : finally prints the original string appended with j that carries our number






                  share|improve this answer


























                    1












                    1








                    1







                    You can use awk here



                    awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}' 


                    Explanation



                    -F':': tells awk to use colon as the separator



                    j=$1; gsub(/[^0-9]+/,"",j) : assigns the first column to to a temporary variable j, and then removes anything that is not a digit from j



                    printf "%s%sn",$0,j;}' : finally prints the original string appended with j that carries our number






                    share|improve this answer













                    You can use awk here



                    awk -F':' '{ j=$1; gsub(/[^0-9]+/,"",j); printf "%s%sn",$0,j;}' 


                    Explanation



                    -F':': tells awk to use colon as the separator



                    j=$1; gsub(/[^0-9]+/,"",j) : assigns the first column to to a temporary variable j, and then removes anything that is not a digit from j



                    printf "%s%sn",$0,j;}' : finally prints the original string appended with j that carries our number







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 1 hour ago









                    amisaxamisax

                    1,636615




                    1,636615























                        1














                        With the match() function of GNU awk to store the matching group in an array, you could do



                        awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file


                        On any POSIX compliant awk you could do



                        awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file


                        See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.






                        share|improve this answer






























                          1














                          With the match() function of GNU awk to store the matching group in an array, you could do



                          awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file


                          On any POSIX compliant awk you could do



                          awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file


                          See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.






                          share|improve this answer




























                            1












                            1








                            1







                            With the match() function of GNU awk to store the matching group in an array, you could do



                            awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file


                            On any POSIX compliant awk you could do



                            awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file


                            See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.






                            share|improve this answer















                            With the match() function of GNU awk to store the matching group in an array, you could do



                            awk -F: -v OFS=: 'match($1, /([0-9]+)$/ , arr) { $2 = $2 arr[1] } 1' file


                            On any POSIX compliant awk you could do



                            awk -F: -v OFS=: 'match($1, /[[:digit:]]+$/) { $2 = $2 substr($0, RSTART, RLENGTH) }; 1' file


                            See How to permanently change a file using awk? ("in-place" edits, as with "sed -i") to make the file change persistent.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 1 hour ago









                            Stéphane Chazelas

                            321k57613983




                            321k57613983










                            answered 1 hour ago









                            InianInian

                            6,4401734




                            6,4401734






















                                user357075 is a new contributor. Be nice, and check out our Code of Conduct.










                                draft saved

                                draft discarded


















                                user357075 is a new contributor. Be nice, and check out our Code of Conduct.













                                user357075 is a new contributor. Be nice, and check out our Code of Conduct.












                                user357075 is a new contributor. Be nice, and check out our Code of Conduct.
















                                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.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f523920%2fcopy-digits-from-end-of-string-to-another%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

                                Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

                                Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

                                Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...