Unix - Remove Leading/Trailing Spaces (Column Wise)












0















I have text lines like these below:



P,   123456  ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,


I want them to be like below:



P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,


Requirements:




  1. Remove all leading & trailing spaces from 2,6,7 column only

  2. Values in 7th Column should remain separated by single space only


Research:



I have tried many combinations with awk, sed, tr but could not succeed according to my requirements. I would like to have a preferable solution in awk, sed, tr only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.










share|improve this question

























  • see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...

    – Sundeep
    Nov 3 '16 at 8:42











  • I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305

    – user3676305
    Nov 3 '16 at 8:49
















0















I have text lines like these below:



P,   123456  ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,


I want them to be like below:



P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,


Requirements:




  1. Remove all leading & trailing spaces from 2,6,7 column only

  2. Values in 7th Column should remain separated by single space only


Research:



I have tried many combinations with awk, sed, tr but could not succeed according to my requirements. I would like to have a preferable solution in awk, sed, tr only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.










share|improve this question

























  • see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...

    – Sundeep
    Nov 3 '16 at 8:42











  • I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305

    – user3676305
    Nov 3 '16 at 8:49














0












0








0








I have text lines like these below:



P,   123456  ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,


I want them to be like below:



P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,


Requirements:




  1. Remove all leading & trailing spaces from 2,6,7 column only

  2. Values in 7th Column should remain separated by single space only


Research:



I have tried many combinations with awk, sed, tr but could not succeed according to my requirements. I would like to have a preferable solution in awk, sed, tr only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.










share|improve this question
















I have text lines like these below:



P,   123456  ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,


I want them to be like below:



P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,


Requirements:




  1. Remove all leading & trailing spaces from 2,6,7 column only

  2. Values in 7th Column should remain separated by single space only


Research:



I have tried many combinations with awk, sed, tr but could not succeed according to my requirements. I would like to have a preferable solution in awk, sed, tr only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.







unix awk sed tr






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 3 '16 at 8:46









sat

11.7k23056




11.7k23056










asked Nov 3 '16 at 8:37









user3676305user3676305

408




408













  • see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...

    – Sundeep
    Nov 3 '16 at 8:42











  • I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305

    – user3676305
    Nov 3 '16 at 8:49



















  • see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...

    – Sundeep
    Nov 3 '16 at 8:42











  • I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305

    – user3676305
    Nov 3 '16 at 8:49

















see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...

– Sundeep
Nov 3 '16 at 8:42





see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...

– Sundeep
Nov 3 '16 at 8:42













I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305

– user3676305
Nov 3 '16 at 8:49





I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305

– user3676305
Nov 3 '16 at 8:49












2 Answers
2






active

oldest

votes


















3














You can use this awk:



awk -F' *, *' '$1=$1' OFS=, file


Understandable way:



awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file


As commented by @glennjackman, safer to use



awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file


Explanation:





  • FS - Set input field separator


  • OFS - Set output field separator


  • $1=$1 - This will make awk to format the fields with OFS. This returns true which makes awk to print current line (reformatted).






share|improve this answer


























  • worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

    – user3676305
    Nov 3 '16 at 9:00











  • Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

    – fedorqui
    Nov 3 '16 at 9:21











  • @user3676305, Updated answer.

    – sat
    Nov 3 '16 at 9:28











  • Thanks for the explanation. Your solution has been marked as "Accepted"

    – user3676305
    Nov 3 '16 at 9:47






  • 1





    Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

    – glenn jackman
    Nov 3 '16 at 10:49





















1














sed is a good choice too.



sed 's/ *, */,/g' file


We're assuming this CSV file does not contain a line like this:



a, b, "this field, this very one, should not be touched", d





share|improve this answer























    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%2f40396947%2funix-remove-leading-trailing-spaces-column-wise%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    You can use this awk:



    awk -F' *, *' '$1=$1' OFS=, file


    Understandable way:



    awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file


    As commented by @glennjackman, safer to use



    awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file


    Explanation:





    • FS - Set input field separator


    • OFS - Set output field separator


    • $1=$1 - This will make awk to format the fields with OFS. This returns true which makes awk to print current line (reformatted).






    share|improve this answer


























    • worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

      – user3676305
      Nov 3 '16 at 9:00











    • Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

      – fedorqui
      Nov 3 '16 at 9:21











    • @user3676305, Updated answer.

      – sat
      Nov 3 '16 at 9:28











    • Thanks for the explanation. Your solution has been marked as "Accepted"

      – user3676305
      Nov 3 '16 at 9:47






    • 1





      Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

      – glenn jackman
      Nov 3 '16 at 10:49


















    3














    You can use this awk:



    awk -F' *, *' '$1=$1' OFS=, file


    Understandable way:



    awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file


    As commented by @glennjackman, safer to use



    awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file


    Explanation:





    • FS - Set input field separator


    • OFS - Set output field separator


    • $1=$1 - This will make awk to format the fields with OFS. This returns true which makes awk to print current line (reformatted).






    share|improve this answer


























    • worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

      – user3676305
      Nov 3 '16 at 9:00











    • Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

      – fedorqui
      Nov 3 '16 at 9:21











    • @user3676305, Updated answer.

      – sat
      Nov 3 '16 at 9:28











    • Thanks for the explanation. Your solution has been marked as "Accepted"

      – user3676305
      Nov 3 '16 at 9:47






    • 1





      Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

      – glenn jackman
      Nov 3 '16 at 10:49
















    3












    3








    3







    You can use this awk:



    awk -F' *, *' '$1=$1' OFS=, file


    Understandable way:



    awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file


    As commented by @glennjackman, safer to use



    awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file


    Explanation:





    • FS - Set input field separator


    • OFS - Set output field separator


    • $1=$1 - This will make awk to format the fields with OFS. This returns true which makes awk to print current line (reformatted).






    share|improve this answer















    You can use this awk:



    awk -F' *, *' '$1=$1' OFS=, file


    Understandable way:



    awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file


    As commented by @glennjackman, safer to use



    awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file


    Explanation:





    • FS - Set input field separator


    • OFS - Set output field separator


    • $1=$1 - This will make awk to format the fields with OFS. This returns true which makes awk to print current line (reformatted).







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 3 '16 at 11:13

























    answered Nov 3 '16 at 8:47









    satsat

    11.7k23056




    11.7k23056













    • worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

      – user3676305
      Nov 3 '16 at 9:00











    • Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

      – fedorqui
      Nov 3 '16 at 9:21











    • @user3676305, Updated answer.

      – sat
      Nov 3 '16 at 9:28











    • Thanks for the explanation. Your solution has been marked as "Accepted"

      – user3676305
      Nov 3 '16 at 9:47






    • 1





      Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

      – glenn jackman
      Nov 3 '16 at 10:49





















    • worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

      – user3676305
      Nov 3 '16 at 9:00











    • Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

      – fedorqui
      Nov 3 '16 at 9:21











    • @user3676305, Updated answer.

      – sat
      Nov 3 '16 at 9:28











    • Thanks for the explanation. Your solution has been marked as "Accepted"

      – user3676305
      Nov 3 '16 at 9:47






    • 1





      Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

      – glenn jackman
      Nov 3 '16 at 10:49



















    worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

    – user3676305
    Nov 3 '16 at 9:00





    worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.

    – user3676305
    Nov 3 '16 at 9:00













    Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

    – fedorqui
    Nov 3 '16 at 9:21





    Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!

    – fedorqui
    Nov 3 '16 at 9:21













    @user3676305, Updated answer.

    – sat
    Nov 3 '16 at 9:28





    @user3676305, Updated answer.

    – sat
    Nov 3 '16 at 9:28













    Thanks for the explanation. Your solution has been marked as "Accepted"

    – user3676305
    Nov 3 '16 at 9:47





    Thanks for the explanation. Your solution has been marked as "Accepted"

    – user3676305
    Nov 3 '16 at 9:47




    1




    1





    Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

    – glenn jackman
    Nov 3 '16 at 10:49







    Pedantically, $1=$1 returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'. Safer to use {$1=$1; print}

    – glenn jackman
    Nov 3 '16 at 10:49















    1














    sed is a good choice too.



    sed 's/ *, */,/g' file


    We're assuming this CSV file does not contain a line like this:



    a, b, "this field, this very one, should not be touched", d





    share|improve this answer




























      1














      sed is a good choice too.



      sed 's/ *, */,/g' file


      We're assuming this CSV file does not contain a line like this:



      a, b, "this field, this very one, should not be touched", d





      share|improve this answer


























        1












        1








        1







        sed is a good choice too.



        sed 's/ *, */,/g' file


        We're assuming this CSV file does not contain a line like this:



        a, b, "this field, this very one, should not be touched", d





        share|improve this answer













        sed is a good choice too.



        sed 's/ *, */,/g' file


        We're assuming this CSV file does not contain a line like this:



        a, b, "this field, this very one, should not be touched", d






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 3 '16 at 10:53









        glenn jackmanglenn jackman

        166k26143235




        166k26143235






























            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%2f40396947%2funix-remove-leading-trailing-spaces-column-wise%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

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'