How to avoid data trimming while formatting file in unix











up vote
0
down vote

favorite












I have a file in below format



A           |b -c          |   c      |  d  -c | | |


And I need to format the above file like below



A|b -c|c|d  -c|||


Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).










share|improve this question
























  • Is it fair to say that you now want to remove space from after the pipe as well?
    – Jeff Schaller
    Nov 16 at 20:38










  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Nov 18 at 13:25















up vote
0
down vote

favorite












I have a file in below format



A           |b -c          |   c      |  d  -c | | |


And I need to format the above file like below



A|b -c|c|d  -c|||


Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).










share|improve this question
























  • Is it fair to say that you now want to remove space from after the pipe as well?
    – Jeff Schaller
    Nov 16 at 20:38










  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Nov 18 at 13:25













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file in below format



A           |b -c          |   c      |  d  -c | | |


And I need to format the above file like below



A|b -c|c|d  -c|||


Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).










share|improve this question















I have a file in below format



A           |b -c          |   c      |  d  -c | | |


And I need to format the above file like below



A|b -c|c|d  -c|||


Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).







text-formatting






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 at 20:22









steeldriver

33.6k34982




33.6k34982










asked Nov 16 at 17:56









user313150

113




113












  • Is it fair to say that you now want to remove space from after the pipe as well?
    – Jeff Schaller
    Nov 16 at 20:38










  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Nov 18 at 13:25


















  • Is it fair to say that you now want to remove space from after the pipe as well?
    – Jeff Schaller
    Nov 16 at 20:38










  • If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
    – Jeff Schaller
    Nov 18 at 13:25
















Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38




Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38












If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25




If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25










2 Answers
2






active

oldest

votes

















up vote
2
down vote













One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:



sed  's/  *|/|/g' < input > output


To also remove spaces after the pipe:



sed  's/  *|/|/g; s/|  */|/g' < input > output





share|improve this answer























  • Thanks a lot ..It worked.
    – user313150
    Nov 16 at 18:08












  • what If I want to remove extra spaces before pipe | too..
    – user313150
    Nov 16 at 18:10












  • doesn't the above remove extra spaces before the pipe?
    – Jeff Schaller
    Nov 16 at 18:25










  • No..it didn't remove
    – user313150
    Nov 16 at 18:46










  • Do you have two spaces between the first slash and the asterisk?
    – Jeff Schaller
    Nov 16 at 18:49


















up vote
0
down vote













You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:



awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file





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


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482208%2fhow-to-avoid-data-trimming-while-formatting-file-in-unix%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








    up vote
    2
    down vote













    One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:



    sed  's/  *|/|/g' < input > output


    To also remove spaces after the pipe:



    sed  's/  *|/|/g; s/|  */|/g' < input > output





    share|improve this answer























    • Thanks a lot ..It worked.
      – user313150
      Nov 16 at 18:08












    • what If I want to remove extra spaces before pipe | too..
      – user313150
      Nov 16 at 18:10












    • doesn't the above remove extra spaces before the pipe?
      – Jeff Schaller
      Nov 16 at 18:25










    • No..it didn't remove
      – user313150
      Nov 16 at 18:46










    • Do you have two spaces between the first slash and the asterisk?
      – Jeff Schaller
      Nov 16 at 18:49















    up vote
    2
    down vote













    One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:



    sed  's/  *|/|/g' < input > output


    To also remove spaces after the pipe:



    sed  's/  *|/|/g; s/|  */|/g' < input > output





    share|improve this answer























    • Thanks a lot ..It worked.
      – user313150
      Nov 16 at 18:08












    • what If I want to remove extra spaces before pipe | too..
      – user313150
      Nov 16 at 18:10












    • doesn't the above remove extra spaces before the pipe?
      – Jeff Schaller
      Nov 16 at 18:25










    • No..it didn't remove
      – user313150
      Nov 16 at 18:46










    • Do you have two spaces between the first slash and the asterisk?
      – Jeff Schaller
      Nov 16 at 18:49













    up vote
    2
    down vote










    up vote
    2
    down vote









    One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:



    sed  's/  *|/|/g' < input > output


    To also remove spaces after the pipe:



    sed  's/  *|/|/g; s/|  */|/g' < input > output





    share|improve this answer














    One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:



    sed  's/  *|/|/g' < input > output


    To also remove spaces after the pipe:



    sed  's/  *|/|/g; s/|  */|/g' < input > output






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 16 at 20:39

























    answered Nov 16 at 18:00









    Jeff Schaller

    36.3k952119




    36.3k952119












    • Thanks a lot ..It worked.
      – user313150
      Nov 16 at 18:08












    • what If I want to remove extra spaces before pipe | too..
      – user313150
      Nov 16 at 18:10












    • doesn't the above remove extra spaces before the pipe?
      – Jeff Schaller
      Nov 16 at 18:25










    • No..it didn't remove
      – user313150
      Nov 16 at 18:46










    • Do you have two spaces between the first slash and the asterisk?
      – Jeff Schaller
      Nov 16 at 18:49


















    • Thanks a lot ..It worked.
      – user313150
      Nov 16 at 18:08












    • what If I want to remove extra spaces before pipe | too..
      – user313150
      Nov 16 at 18:10












    • doesn't the above remove extra spaces before the pipe?
      – Jeff Schaller
      Nov 16 at 18:25










    • No..it didn't remove
      – user313150
      Nov 16 at 18:46










    • Do you have two spaces between the first slash and the asterisk?
      – Jeff Schaller
      Nov 16 at 18:49
















    Thanks a lot ..It worked.
    – user313150
    Nov 16 at 18:08






    Thanks a lot ..It worked.
    – user313150
    Nov 16 at 18:08














    what If I want to remove extra spaces before pipe | too..
    – user313150
    Nov 16 at 18:10






    what If I want to remove extra spaces before pipe | too..
    – user313150
    Nov 16 at 18:10














    doesn't the above remove extra spaces before the pipe?
    – Jeff Schaller
    Nov 16 at 18:25




    doesn't the above remove extra spaces before the pipe?
    – Jeff Schaller
    Nov 16 at 18:25












    No..it didn't remove
    – user313150
    Nov 16 at 18:46




    No..it didn't remove
    – user313150
    Nov 16 at 18:46












    Do you have two spaces between the first slash and the asterisk?
    – Jeff Schaller
    Nov 16 at 18:49




    Do you have two spaces between the first slash and the asterisk?
    – Jeff Schaller
    Nov 16 at 18:49












    up vote
    0
    down vote













    You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:



    awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file





    share|improve this answer

























      up vote
      0
      down vote













      You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:



      awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:



        awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file





        share|improve this answer












        You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:



        awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 16 at 20:23









        steeldriver

        33.6k34982




        33.6k34982






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f482208%2fhow-to-avoid-data-trimming-while-formatting-file-in-unix%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

            List directoties down one level, excluding some named directories and files

            list processes belonging to a network namespace

            list systemd RuntimeDirectory mounts