Which is better between a for loop and while loop for Korn shell

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











up vote
0
down vote

favorite












I have a file file_list.txt which contains a list of filenames to be processed. And for all the files present in the file_list.txt I have to remove the trailing blank spaces and extra newline characters.
How can I achieve this. Using a while loop will be better or a for loop?







share|improve this question


















  • 2




    for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd
    – don_crissti
    Nov 28 '17 at 12:56







  • 1




    Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)?
    – Stéphane Chazelas
    Nov 28 '17 at 13:04











  • Its about editting the content of every file in the file_list.txt
    – Raghunath Choudhary
    Nov 28 '17 at 13:24










  • What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines?
    – Stéphane Chazelas
    Nov 28 '17 at 14:07










  • Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt)
    – Raghunath Choudhary
    Nov 28 '17 at 14:10















up vote
0
down vote

favorite












I have a file file_list.txt which contains a list of filenames to be processed. And for all the files present in the file_list.txt I have to remove the trailing blank spaces and extra newline characters.
How can I achieve this. Using a while loop will be better or a for loop?







share|improve this question


















  • 2




    for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd
    – don_crissti
    Nov 28 '17 at 12:56







  • 1




    Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)?
    – Stéphane Chazelas
    Nov 28 '17 at 13:04











  • Its about editting the content of every file in the file_list.txt
    – Raghunath Choudhary
    Nov 28 '17 at 13:24










  • What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines?
    – Stéphane Chazelas
    Nov 28 '17 at 14:07










  • Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt)
    – Raghunath Choudhary
    Nov 28 '17 at 14:10













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have a file file_list.txt which contains a list of filenames to be processed. And for all the files present in the file_list.txt I have to remove the trailing blank spaces and extra newline characters.
How can I achieve this. Using a while loop will be better or a for loop?







share|improve this question














I have a file file_list.txt which contains a list of filenames to be processed. And for all the files present in the file_list.txt I have to remove the trailing blank spaces and extra newline characters.
How can I achieve this. Using a while loop will be better or a for loop?









share|improve this question













share|improve this question




share|improve this question








edited Nov 28 '17 at 13:05









Stéphane Chazelas

282k53520854




282k53520854










asked Nov 28 '17 at 12:52









Raghunath Choudhary

588




588







  • 2




    for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd
    – don_crissti
    Nov 28 '17 at 12:56







  • 1




    Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)?
    – Stéphane Chazelas
    Nov 28 '17 at 13:04











  • Its about editting the content of every file in the file_list.txt
    – Raghunath Choudhary
    Nov 28 '17 at 13:24










  • What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines?
    – Stéphane Chazelas
    Nov 28 '17 at 14:07










  • Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt)
    – Raghunath Choudhary
    Nov 28 '17 at 14:10













  • 2




    for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd
    – don_crissti
    Nov 28 '17 at 12:56







  • 1




    Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)?
    – Stéphane Chazelas
    Nov 28 '17 at 13:04











  • Its about editting the content of every file in the file_list.txt
    – Raghunath Choudhary
    Nov 28 '17 at 13:24










  • What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines?
    – Stéphane Chazelas
    Nov 28 '17 at 14:07










  • Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt)
    – Raghunath Choudhary
    Nov 28 '17 at 14:10








2




2




for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd
– don_crissti
Nov 28 '17 at 12:56





for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd
– don_crissti
Nov 28 '17 at 12:56





1




1




Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)?
– Stéphane Chazelas
Nov 28 '17 at 13:04





Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)?
– Stéphane Chazelas
Nov 28 '17 at 13:04













Its about editting the content of every file in the file_list.txt
– Raghunath Choudhary
Nov 28 '17 at 13:24




Its about editting the content of every file in the file_list.txt
– Raghunath Choudhary
Nov 28 '17 at 13:24












What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines?
– Stéphane Chazelas
Nov 28 '17 at 14:07




What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines?
– Stéphane Chazelas
Nov 28 '17 at 14:07












Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt)
– Raghunath Choudhary
Nov 28 '17 at 14:10





Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt)
– Raghunath Choudhary
Nov 28 '17 at 14:10











1 Answer
1






active

oldest

votes

















up vote
2
down vote













If it's about editing the content of the file_list.txt, you'd use a text editing tool, not a shell loop, like:



sed 's/[[:space:]]*$//; # remove trailing whitespace
/./!d; # remove empty lines' < file_list.txt > new_file_list.txt


Or for in-place editing:



perl -nli.back -e 's/s+$//;print if /./' file_list.txt


(remove .back if you don't need backup copies of the original).



If file_list.txt contains a list of files and it's those files whose content you want to edit, then again a loop is not ideal unless you do want to run one editing command per file.



If the content of file_list.txt is compatible with xargs input format, that is where file names are whitespace (including newline) separated and double quotes, single quotes or backslash can escape whitespace and each other (allowing any character), then you can just do:



xargs < file_list.txt perl -nli.back -e 's/s+$//;print if /./' --





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: 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%2f407499%2fwhich-is-better-between-a-for-loop-and-while-loop-for-korn-shell%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    If it's about editing the content of the file_list.txt, you'd use a text editing tool, not a shell loop, like:



    sed 's/[[:space:]]*$//; # remove trailing whitespace
    /./!d; # remove empty lines' < file_list.txt > new_file_list.txt


    Or for in-place editing:



    perl -nli.back -e 's/s+$//;print if /./' file_list.txt


    (remove .back if you don't need backup copies of the original).



    If file_list.txt contains a list of files and it's those files whose content you want to edit, then again a loop is not ideal unless you do want to run one editing command per file.



    If the content of file_list.txt is compatible with xargs input format, that is where file names are whitespace (including newline) separated and double quotes, single quotes or backslash can escape whitespace and each other (allowing any character), then you can just do:



    xargs < file_list.txt perl -nli.back -e 's/s+$//;print if /./' --





    share|improve this answer


























      up vote
      2
      down vote













      If it's about editing the content of the file_list.txt, you'd use a text editing tool, not a shell loop, like:



      sed 's/[[:space:]]*$//; # remove trailing whitespace
      /./!d; # remove empty lines' < file_list.txt > new_file_list.txt


      Or for in-place editing:



      perl -nli.back -e 's/s+$//;print if /./' file_list.txt


      (remove .back if you don't need backup copies of the original).



      If file_list.txt contains a list of files and it's those files whose content you want to edit, then again a loop is not ideal unless you do want to run one editing command per file.



      If the content of file_list.txt is compatible with xargs input format, that is where file names are whitespace (including newline) separated and double quotes, single quotes or backslash can escape whitespace and each other (allowing any character), then you can just do:



      xargs < file_list.txt perl -nli.back -e 's/s+$//;print if /./' --





      share|improve this answer
























        up vote
        2
        down vote










        up vote
        2
        down vote









        If it's about editing the content of the file_list.txt, you'd use a text editing tool, not a shell loop, like:



        sed 's/[[:space:]]*$//; # remove trailing whitespace
        /./!d; # remove empty lines' < file_list.txt > new_file_list.txt


        Or for in-place editing:



        perl -nli.back -e 's/s+$//;print if /./' file_list.txt


        (remove .back if you don't need backup copies of the original).



        If file_list.txt contains a list of files and it's those files whose content you want to edit, then again a loop is not ideal unless you do want to run one editing command per file.



        If the content of file_list.txt is compatible with xargs input format, that is where file names are whitespace (including newline) separated and double quotes, single quotes or backslash can escape whitespace and each other (allowing any character), then you can just do:



        xargs < file_list.txt perl -nli.back -e 's/s+$//;print if /./' --





        share|improve this answer














        If it's about editing the content of the file_list.txt, you'd use a text editing tool, not a shell loop, like:



        sed 's/[[:space:]]*$//; # remove trailing whitespace
        /./!d; # remove empty lines' < file_list.txt > new_file_list.txt


        Or for in-place editing:



        perl -nli.back -e 's/s+$//;print if /./' file_list.txt


        (remove .back if you don't need backup copies of the original).



        If file_list.txt contains a list of files and it's those files whose content you want to edit, then again a loop is not ideal unless you do want to run one editing command per file.



        If the content of file_list.txt is compatible with xargs input format, that is where file names are whitespace (including newline) separated and double quotes, single quotes or backslash can escape whitespace and each other (allowing any character), then you can just do:



        xargs < file_list.txt perl -nli.back -e 's/s+$//;print if /./' --






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 28 '17 at 14:11

























        answered Nov 28 '17 at 13:18









        Stéphane Chazelas

        282k53520854




        282k53520854



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407499%2fwhich-is-better-between-a-for-loop-and-while-loop-for-korn-shell%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?