Reverse the order of first 3 digits in a line

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.



ex: input



214 4234-5555


ex: output



412 4234-5555


this is what I have so far



sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt






share|improve this question


















  • 1




    different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/… .. also, question mentions put them in brackets but expected output doesn't show it..
    – Sundeep
    Feb 19 at 3:49











  • reddit link was the answer ty
    – Soda
    Feb 19 at 4:04










  • Similar to How to reverse a string made of digit in bash?.
    – Î±Ò“sнιη
    Feb 19 at 7:56














up vote
1
down vote

favorite












I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.



ex: input



214 4234-5555


ex: output



412 4234-5555


this is what I have so far



sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt






share|improve this question


















  • 1




    different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/… .. also, question mentions put them in brackets but expected output doesn't show it..
    – Sundeep
    Feb 19 at 3:49











  • reddit link was the answer ty
    – Soda
    Feb 19 at 4:04










  • Similar to How to reverse a string made of digit in bash?.
    – Î±Ò“sнιη
    Feb 19 at 7:56












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.



ex: input



214 4234-5555


ex: output



412 4234-5555


this is what I have so far



sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt






share|improve this question














I am trying to do a assignment for university, but I am currently stuck.
The goal is to read some phone numbers and reverse the order of the first 3 digits and put them in brackets. I can get it to read the phone numbers but not do the reversal of the digits.



ex: input



214 4234-5555


ex: output



412 4234-5555


this is what I have so far



sed -r "s/([0-9]), ([0-9]), ([0-9])/321/g" phone.txt








share|improve this question













share|improve this question




share|improve this question








edited Feb 19 at 6:42









Kusalananda

103k13202318




103k13202318










asked Feb 19 at 3:41









Soda

111




111







  • 1




    different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/… .. also, question mentions put them in brackets but expected output doesn't show it..
    – Sundeep
    Feb 19 at 3:49











  • reddit link was the answer ty
    – Soda
    Feb 19 at 4:04










  • Similar to How to reverse a string made of digit in bash?.
    – Î±Ò“sнιη
    Feb 19 at 7:56












  • 1




    different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/… .. also, question mentions put them in brackets but expected output doesn't show it..
    – Sundeep
    Feb 19 at 3:49











  • reddit link was the answer ty
    – Soda
    Feb 19 at 4:04










  • Similar to How to reverse a string made of digit in bash?.
    – Î±Ò“sнιη
    Feb 19 at 7:56







1




1




different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/… .. also, question mentions put them in brackets but expected output doesn't show it..
– Sundeep
Feb 19 at 3:49





different site duplicate ;) reddit.com/r/commandline/comments/7y51wd/… .. also, question mentions put them in brackets but expected output doesn't show it..
– Sundeep
Feb 19 at 3:49













reddit link was the answer ty
– Soda
Feb 19 at 4:04




reddit link was the answer ty
– Soda
Feb 19 at 4:04












Similar to How to reverse a string made of digit in bash?.
– Î±Ò“sнιη
Feb 19 at 7:56




Similar to How to reverse a string made of digit in bash?.
– Î±Ò“sнιη
Feb 19 at 7:56










5 Answers
5






active

oldest

votes

















up vote
5
down vote













To modify OP's attempt



$ cat ip.txt
214 4234-5555
foo 123 4533-3242

$ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
412 4234-5555
foo 321 4533-3242

$ # adding parenthesis as well
$ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242

$ # if ERE is not supported
$ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
(412) 4234-5555
foo (321) 4533-3242


  • Note that some sed implementation would need -E instead of -r

  • Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes


  • ([0-9]), ([0-9]), ([0-9]) means matching 3 digits separated by comma and space


  • g modifier is needed if all matches in a line should be changed



For a generic solution, i.e defining number of digits to reverse as a numeric argument



$ perl -pe 's/d3/reverse $&/e' ip.txt
412 4234-5555
foo 321 4533-3242
$ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
(412) 4234-5555
foo (321) 4533-3242





share|improve this answer


















  • 1




    +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
    – Sergiy Kolodyazhnyy
    Feb 19 at 5:55






  • 1




    @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
    – Stéphane Chazelas
    Feb 19 at 16:19

















up vote
1
down vote













Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:



sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'


This works as so:



 # pretend 214 4234-5555 is the current line
h; # copy the current line into hold space
s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
:1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
# see notes below; 214 becomes 412
s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
# delete first 3 chars;
# pattern space now is <space>4234-5555

x;G;s/n// # swap again, append hold buffer to pattern buffer;
# now pattern buffer is (412)<newline> 4234-5555;
# finally delete newline; we get (412) 4234-5555


And that's how it looks like in action:



$ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
(412) 4234-5555
(321) 3333


Note: String reversal originally found on Stephane Chazelas's comment






share|improve this answer



























    up vote
    0
    down vote













    $ n='214 4234-5555'
    $ echo `echo $n:0:3|rev`$n:3:9
    412 4234-555





    share|improve this answer




















    • Brevity is acceptable, but fuller explanations are better.
      – Kusalananda
      Feb 19 at 10:52

















    up vote
    0
    down vote













    Method1



    I have used below method get the same result



    i=`awk 'print $1' example.txt| rev`
    awk -v i="$i" 'print i,$2' example.txt


    Output



    412 4234-5555


    Method2



    sed 's/(.)(.)(.)/321/' example.txt


    output



    412 4234-5555





    share|improve this answer






















    • I think your first line is missing some command substitution.
      – Jeff Schaller
      Feb 20 at 3:21










    • @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
      – Praveen Kumar BS
      Feb 20 at 15:16










    • Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
      – Jeff Schaller
      Feb 20 at 16:02

















    up vote
    -3
    down vote













    If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:



    !/bin/bash



    echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'






    share|improve this answer


















    • 1




      That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
      – terdon♦
      Feb 19 at 10:32










    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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425073%2freverse-the-order-of-first-3-digits-in-a-line%23new-answer', 'question_page');

    );

    Post as a guest






























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    5
    down vote













    To modify OP's attempt



    $ cat ip.txt
    214 4234-5555
    foo 123 4533-3242

    $ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
    412 4234-5555
    foo 321 4533-3242

    $ # adding parenthesis as well
    $ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242

    $ # if ERE is not supported
    $ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242


    • Note that some sed implementation would need -E instead of -r

    • Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes


    • ([0-9]), ([0-9]), ([0-9]) means matching 3 digits separated by comma and space


    • g modifier is needed if all matches in a line should be changed



    For a generic solution, i.e defining number of digits to reverse as a numeric argument



    $ perl -pe 's/d3/reverse $&/e' ip.txt
    412 4234-5555
    foo 321 4533-3242
    $ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242





    share|improve this answer


















    • 1




      +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
      – Sergiy Kolodyazhnyy
      Feb 19 at 5:55






    • 1




      @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
      – Stéphane Chazelas
      Feb 19 at 16:19














    up vote
    5
    down vote













    To modify OP's attempt



    $ cat ip.txt
    214 4234-5555
    foo 123 4533-3242

    $ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
    412 4234-5555
    foo 321 4533-3242

    $ # adding parenthesis as well
    $ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242

    $ # if ERE is not supported
    $ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242


    • Note that some sed implementation would need -E instead of -r

    • Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes


    • ([0-9]), ([0-9]), ([0-9]) means matching 3 digits separated by comma and space


    • g modifier is needed if all matches in a line should be changed



    For a generic solution, i.e defining number of digits to reverse as a numeric argument



    $ perl -pe 's/d3/reverse $&/e' ip.txt
    412 4234-5555
    foo 321 4533-3242
    $ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242





    share|improve this answer


















    • 1




      +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
      – Sergiy Kolodyazhnyy
      Feb 19 at 5:55






    • 1




      @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
      – Stéphane Chazelas
      Feb 19 at 16:19












    up vote
    5
    down vote










    up vote
    5
    down vote









    To modify OP's attempt



    $ cat ip.txt
    214 4234-5555
    foo 123 4533-3242

    $ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
    412 4234-5555
    foo 321 4533-3242

    $ # adding parenthesis as well
    $ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242

    $ # if ERE is not supported
    $ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242


    • Note that some sed implementation would need -E instead of -r

    • Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes


    • ([0-9]), ([0-9]), ([0-9]) means matching 3 digits separated by comma and space


    • g modifier is needed if all matches in a line should be changed



    For a generic solution, i.e defining number of digits to reverse as a numeric argument



    $ perl -pe 's/d3/reverse $&/e' ip.txt
    412 4234-5555
    foo 321 4533-3242
    $ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242





    share|improve this answer














    To modify OP's attempt



    $ cat ip.txt
    214 4234-5555
    foo 123 4533-3242

    $ sed -r 's/([0-9])([0-9])([0-9])/321/' ip.txt
    412 4234-5555
    foo 321 4533-3242

    $ # adding parenthesis as well
    $ sed -r 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242

    $ # if ERE is not supported
    $ sed 's/([0-9])([0-9])([0-9])/(321)/' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242


    • Note that some sed implementation would need -E instead of -r

    • Use single quotes unless you need interpolation, see also https://mywiki.wooledge.org/Quotes


    • ([0-9]), ([0-9]), ([0-9]) means matching 3 digits separated by comma and space


    • g modifier is needed if all matches in a line should be changed



    For a generic solution, i.e defining number of digits to reverse as a numeric argument



    $ perl -pe 's/d3/reverse $&/e' ip.txt
    412 4234-5555
    foo 321 4533-3242
    $ perl -pe 's/d3/sprintf "(%s)", scalar reverse $&/e' ip.txt
    (412) 4234-5555
    foo (321) 4533-3242






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Feb 19 at 6:15

























    answered Feb 19 at 5:03









    Sundeep

    6,9511826




    6,9511826







    • 1




      +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
      – Sergiy Kolodyazhnyy
      Feb 19 at 5:55






    • 1




      @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
      – Stéphane Chazelas
      Feb 19 at 16:19












    • 1




      +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
      – Sergiy Kolodyazhnyy
      Feb 19 at 5:55






    • 1




      @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
      – Stéphane Chazelas
      Feb 19 at 16:19







    1




    1




    +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
    – Sergiy Kolodyazhnyy
    Feb 19 at 5:55




    +1'ed, good answer, especially perl . You may want to include POSIX grouping ([0-9]) since POSIX sed doesn't have -r , neither does FreeBSD sed.
    – Sergiy Kolodyazhnyy
    Feb 19 at 5:55




    1




    1




    @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
    – Stéphane Chazelas
    Feb 19 at 16:19




    @SergiyKolodyazhnyy, FreeBSD's sed now does support -r for compatibility with GNU sed. GNU sed now also supports -E for compatibility with BSDs and also because it will be the one specified in the next major version of the POSIX specification.
    – Stéphane Chazelas
    Feb 19 at 16:19












    up vote
    1
    down vote













    Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:



    sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'


    This works as so:



     # pretend 214 4234-5555 is the current line
    h; # copy the current line into hold space
    s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
    :1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
    # see notes below; 214 becomes 412
    s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
    x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
    # delete first 3 chars;
    # pattern space now is <space>4234-5555

    x;G;s/n// # swap again, append hold buffer to pattern buffer;
    # now pattern buffer is (412)<newline> 4234-5555;
    # finally delete newline; we get (412) 4234-5555


    And that's how it looks like in action:



    $ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
    (412) 4234-5555
    (321) 3333


    Note: String reversal originally found on Stephane Chazelas's comment






    share|improve this answer
























      up vote
      1
      down vote













      Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:



      sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'


      This works as so:



       # pretend 214 4234-5555 is the current line
      h; # copy the current line into hold space
      s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
      :1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
      # see notes below; 214 becomes 412
      s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
      x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
      # delete first 3 chars;
      # pattern space now is <space>4234-5555

      x;G;s/n// # swap again, append hold buffer to pattern buffer;
      # now pattern buffer is (412)<newline> 4234-5555;
      # finally delete newline; we get (412) 4234-5555


      And that's how it looks like in action:



      $ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
      (412) 4234-5555
      (321) 3333


      Note: String reversal originally found on Stephane Chazelas's comment






      share|improve this answer






















        up vote
        1
        down vote










        up vote
        1
        down vote









        Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:



        sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'


        This works as so:



         # pretend 214 4234-5555 is the current line
        h; # copy the current line into hold space
        s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
        :1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
        # see notes below; 214 becomes 412
        s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
        x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
        # delete first 3 chars;
        # pattern space now is <space>4234-5555

        x;G;s/n// # swap again, append hold buffer to pattern buffer;
        # now pattern buffer is (412)<newline> 4234-5555;
        # finally delete newline; we get (412) 4234-5555


        And that's how it looks like in action:



        $ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
        (412) 4234-5555
        (321) 3333


        Note: String reversal originally found on Stephane Chazelas's comment






        share|improve this answer












        Here's a very long, convoluted, and probably unnecessary sed, but here it is nonetheless because fun:



        sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'


        This works as so:



         # pretend 214 4234-5555 is the current line
        h; # copy the current line into hold space
        s/^([0-9]*) *(.*)/1n/; # keep only first 3 numbers, 214
        :1 s/(.)(.*n)/21/;t1; s/.//; # reversing string in sed,
        # see notes below; 214 becomes 412
        s/^(.*)$/(1)/; # After string is reversed, add brackets; (412)
        x;s/([0-9]3)(.*)/2/; # swap hold and pattern buffer,
        # delete first 3 chars;
        # pattern space now is <space>4234-5555

        x;G;s/n// # swap again, append hold buffer to pattern buffer;
        # now pattern buffer is (412)<newline> 4234-5555;
        # finally delete newline; we get (412) 4234-5555


        And that's how it looks like in action:



        $ printf "214 4234-5555n123 3333n" | sed -re 'h; s/^([0-9]*) *(.*)/1n/; :1 s/(.)(.*n)/21/;t1; s/.//; s/^(.*)$/(1)/; x;s/([0-9]3)(.*)/2/;x;G;s/n//'
        (412) 4234-5555
        (321) 3333


        Note: String reversal originally found on Stephane Chazelas's comment







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Feb 19 at 7:47









        Sergiy Kolodyazhnyy

        7,63311547




        7,63311547




















            up vote
            0
            down vote













            $ n='214 4234-5555'
            $ echo `echo $n:0:3|rev`$n:3:9
            412 4234-555





            share|improve this answer




















            • Brevity is acceptable, but fuller explanations are better.
              – Kusalananda
              Feb 19 at 10:52














            up vote
            0
            down vote













            $ n='214 4234-5555'
            $ echo `echo $n:0:3|rev`$n:3:9
            412 4234-555





            share|improve this answer




















            • Brevity is acceptable, but fuller explanations are better.
              – Kusalananda
              Feb 19 at 10:52












            up vote
            0
            down vote










            up vote
            0
            down vote









            $ n='214 4234-5555'
            $ echo `echo $n:0:3|rev`$n:3:9
            412 4234-555





            share|improve this answer












            $ n='214 4234-5555'
            $ echo `echo $n:0:3|rev`$n:3:9
            412 4234-555






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 19 at 6:26









            Budi

            112




            112











            • Brevity is acceptable, but fuller explanations are better.
              – Kusalananda
              Feb 19 at 10:52
















            • Brevity is acceptable, but fuller explanations are better.
              – Kusalananda
              Feb 19 at 10:52















            Brevity is acceptable, but fuller explanations are better.
            – Kusalananda
            Feb 19 at 10:52




            Brevity is acceptable, but fuller explanations are better.
            – Kusalananda
            Feb 19 at 10:52










            up vote
            0
            down vote













            Method1



            I have used below method get the same result



            i=`awk 'print $1' example.txt| rev`
            awk -v i="$i" 'print i,$2' example.txt


            Output



            412 4234-5555


            Method2



            sed 's/(.)(.)(.)/321/' example.txt


            output



            412 4234-5555





            share|improve this answer






















            • I think your first line is missing some command substitution.
              – Jeff Schaller
              Feb 20 at 3:21










            • @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
              – Praveen Kumar BS
              Feb 20 at 15:16










            • Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
              – Jeff Schaller
              Feb 20 at 16:02














            up vote
            0
            down vote













            Method1



            I have used below method get the same result



            i=`awk 'print $1' example.txt| rev`
            awk -v i="$i" 'print i,$2' example.txt


            Output



            412 4234-5555


            Method2



            sed 's/(.)(.)(.)/321/' example.txt


            output



            412 4234-5555





            share|improve this answer






















            • I think your first line is missing some command substitution.
              – Jeff Schaller
              Feb 20 at 3:21










            • @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
              – Praveen Kumar BS
              Feb 20 at 15:16










            • Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
              – Jeff Schaller
              Feb 20 at 16:02












            up vote
            0
            down vote










            up vote
            0
            down vote









            Method1



            I have used below method get the same result



            i=`awk 'print $1' example.txt| rev`
            awk -v i="$i" 'print i,$2' example.txt


            Output



            412 4234-5555


            Method2



            sed 's/(.)(.)(.)/321/' example.txt


            output



            412 4234-5555





            share|improve this answer














            Method1



            I have used below method get the same result



            i=`awk 'print $1' example.txt| rev`
            awk -v i="$i" 'print i,$2' example.txt


            Output



            412 4234-5555


            Method2



            sed 's/(.)(.)(.)/321/' example.txt


            output



            412 4234-5555






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 20 at 15:13

























            answered Feb 19 at 16:04









            Praveen Kumar BS

            1,010128




            1,010128











            • I think your first line is missing some command substitution.
              – Jeff Schaller
              Feb 20 at 3:21










            • @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
              – Praveen Kumar BS
              Feb 20 at 15:16










            • Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
              – Jeff Schaller
              Feb 20 at 16:02
















            • I think your first line is missing some command substitution.
              – Jeff Schaller
              Feb 20 at 3:21










            • @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
              – Praveen Kumar BS
              Feb 20 at 15:16










            • Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
              – Jeff Schaller
              Feb 20 at 16:02















            I think your first line is missing some command substitution.
            – Jeff Schaller
            Feb 20 at 3:21




            I think your first line is missing some command substitution.
            – Jeff Schaller
            Feb 20 at 3:21












            @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
            – Praveen Kumar BS
            Feb 20 at 15:16




            @JeffSchaller Added the ` in first line which was missing. As checked it working fine now. Kindly let me know for any confusion
            – Praveen Kumar BS
            Feb 20 at 15:16












            Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
            – Jeff Schaller
            Feb 20 at 16:02




            Still no parenthesis; and what about when there’s more than one line of input? Any comment to the OP to teach them what was wrong with their attempt? Otherwise they’re stuck copy-pasting solutions that they don’t understand.
            – Jeff Schaller
            Feb 20 at 16:02










            up vote
            -3
            down vote













            If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:



            !/bin/bash



            echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'






            share|improve this answer


















            • 1




              That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
              – terdon♦
              Feb 19 at 10:32














            up vote
            -3
            down vote













            If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:



            !/bin/bash



            echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'






            share|improve this answer


















            • 1




              That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
              – terdon♦
              Feb 19 at 10:32












            up vote
            -3
            down vote










            up vote
            -3
            down vote









            If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:



            !/bin/bash



            echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'






            share|improve this answer














            If you have the number in phone.txt as "xxx xxx-xxxx", then you can use the below:



            !/bin/bash



            echo '('$(cat phone.txt | cut -d ' ' -f1 | rev)')'







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 19 at 5:40

























            answered Feb 19 at 5:25









            sai

            13




            13







            • 1




              That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
              – terdon♦
              Feb 19 at 10:32












            • 1




              That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
              – terdon♦
              Feb 19 at 10:32







            1




            1




            That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
            – terdon♦
            Feb 19 at 10:32




            That only prints the 1st three numbers reversed, it removes the rest. And this will print one parenthesis at the top of the file, one at the bottom and everything else in the middle. You also don't need the cat, just cut -d ' ' -f1 phone.txt | rev would do the same (not very helpful) thing.
            – terdon♦
            Feb 19 at 10:32












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f425073%2freverse-the-order-of-first-3-digits-in-a-line%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Peggy Mitchell

            Palaiologos

            The Forum (Inglewood, California)