Read from a file and return new line with loop

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











up vote
2
down vote

favorite












I have a file like



foo.txt



xxx B=C D: A
yyy F=H D:A
zzz K=L D: A
fff M=H D:/llll
kkk S=D D: /kkkkk


this is what I try to get;



xxx B=C; D: A ;pass
yyy F=H; D:A ;pass
zzz K=L; D: A ;pass
fff M=H; D:/llll ;try
kkk S=D; D: /kkkkk/bb ;try


The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.



I can read file like this but there is spaces which is another problem



In addition after "D:" always starts with "A" or "/".



I tried while loop but I have not succeeded.



cat /tmp/foo.txt | cut -d ":" -f,2


Output



 A (one empty space)
A (none)
A (three empty space)
/llll (none)
/kkkkk/bb (two empty space)


This is a example what I have tried



while read -r line;
do
if [[ $line ]]; then
echo $line "try"
else
echo $line "pass"
fi
done < foo.txt









share|improve this question



























    up vote
    2
    down vote

    favorite












    I have a file like



    foo.txt



    xxx B=C D: A
    yyy F=H D:A
    zzz K=L D: A
    fff M=H D:/llll
    kkk S=D D: /kkkkk


    this is what I try to get;



    xxx B=C; D: A ;pass
    yyy F=H; D:A ;pass
    zzz K=L; D: A ;pass
    fff M=H; D:/llll ;try
    kkk S=D; D: /kkkkk/bb ;try


    The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.



    I can read file like this but there is spaces which is another problem



    In addition after "D:" always starts with "A" or "/".



    I tried while loop but I have not succeeded.



    cat /tmp/foo.txt | cut -d ":" -f,2


    Output



     A (one empty space)
    A (none)
    A (three empty space)
    /llll (none)
    /kkkkk/bb (two empty space)


    This is a example what I have tried



    while read -r line;
    do
    if [[ $line ]]; then
    echo $line "try"
    else
    echo $line "pass"
    fi
    done < foo.txt









    share|improve this question

























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      I have a file like



      foo.txt



      xxx B=C D: A
      yyy F=H D:A
      zzz K=L D: A
      fff M=H D:/llll
      kkk S=D D: /kkkkk


      this is what I try to get;



      xxx B=C; D: A ;pass
      yyy F=H; D:A ;pass
      zzz K=L; D: A ;pass
      fff M=H; D:/llll ;try
      kkk S=D; D: /kkkkk/bb ;try


      The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.



      I can read file like this but there is spaces which is another problem



      In addition after "D:" always starts with "A" or "/".



      I tried while loop but I have not succeeded.



      cat /tmp/foo.txt | cut -d ":" -f,2


      Output



       A (one empty space)
      A (none)
      A (three empty space)
      /llll (none)
      /kkkkk/bb (two empty space)


      This is a example what I have tried



      while read -r line;
      do
      if [[ $line ]]; then
      echo $line "try"
      else
      echo $line "pass"
      fi
      done < foo.txt









      share|improve this question















      I have a file like



      foo.txt



      xxx B=C D: A
      yyy F=H D:A
      zzz K=L D: A
      fff M=H D:/llll
      kkk S=D D: /kkkkk


      this is what I try to get;



      xxx B=C; D: A ;pass
      yyy F=H; D:A ;pass
      zzz K=L; D: A ;pass
      fff M=H; D:/llll ;try
      kkk S=D; D: /kkkkk/bb ;try


      The second part of line which starts with "D:",if starts with "/", it should be write "try" else it has to write "pass" end of the line for every line in it.



      I can read file like this but there is spaces which is another problem



      In addition after "D:" always starts with "A" or "/".



      I tried while loop but I have not succeeded.



      cat /tmp/foo.txt | cut -d ":" -f,2


      Output



       A (one empty space)
      A (none)
      A (three empty space)
      /llll (none)
      /kkkkk/bb (two empty space)


      This is a example what I have tried



      while read -r line;
      do
      if [[ $line ]]; then
      echo $line "try"
      else
      echo $line "pass"
      fi
      done < foo.txt






      bash shell-script






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday









      Marco Bonelli

      23312




      23312










      asked yesterday









      1010111100011

      356




      356




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          You can't write in the same file, I would recommend to create a new file and then rename it.



          tmpFile="/tmp/foo2.txt.tmp"

          cat /tmp/foo.txt | while read line; do
          isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
          if (( $isSlash )); then
          echo "$line ;try" >> $tmpFile
          else
          echo "$line ;pass" >> $tmpFile
          fi
          done

          mv $tmpFile /tmp/foo.txt





          share|improve this answer






















          • kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
            – 1010111100011
            yesterday











          • I updated my answer, awk 'print $1' will solve the issue with the empty space.
            – Javier Salas
            yesterday











          • thank you @JavierSalas
            – 1010111100011
            yesterday

















          up vote
          1
          down vote













          You can use awk, which makes it very easy to accomplish what you want:



          awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt


          Output:



          xxx B=C D: A ;pass
          yyy F=H D:A ;pass
          zzz K=L D: A ;pass
          fff M=H D:/llll ;try
          kkk S=D D: /kkkkk ;try


          This basically matches every line not containing D: and / and appends ;pass to it, then skips to the next line. Otherwise it appends ;try to it.






          share|improve this answer



























            up vote
            0
            down vote













            sed OK?



            $ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
            xxx B=C D: A ;pass
            yyy F=H D:A ;pass
            zzz K=L D: A ;pass
            fff M=H D:/llll ;try
            kkk S=D D: /kkkkk ;try





            share|improve this answer




















            • posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
              – Isaac
              yesterday










            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%2f481519%2fread-from-a-file-and-return-new-line-with-loop%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            You can't write in the same file, I would recommend to create a new file and then rename it.



            tmpFile="/tmp/foo2.txt.tmp"

            cat /tmp/foo.txt | while read line; do
            isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
            if (( $isSlash )); then
            echo "$line ;try" >> $tmpFile
            else
            echo "$line ;pass" >> $tmpFile
            fi
            done

            mv $tmpFile /tmp/foo.txt





            share|improve this answer






















            • kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
              – 1010111100011
              yesterday











            • I updated my answer, awk 'print $1' will solve the issue with the empty space.
              – Javier Salas
              yesterday











            • thank you @JavierSalas
              – 1010111100011
              yesterday














            up vote
            0
            down vote



            accepted










            You can't write in the same file, I would recommend to create a new file and then rename it.



            tmpFile="/tmp/foo2.txt.tmp"

            cat /tmp/foo.txt | while read line; do
            isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
            if (( $isSlash )); then
            echo "$line ;try" >> $tmpFile
            else
            echo "$line ;pass" >> $tmpFile
            fi
            done

            mv $tmpFile /tmp/foo.txt





            share|improve this answer






















            • kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
              – 1010111100011
              yesterday











            • I updated my answer, awk 'print $1' will solve the issue with the empty space.
              – Javier Salas
              yesterday











            • thank you @JavierSalas
              – 1010111100011
              yesterday












            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            You can't write in the same file, I would recommend to create a new file and then rename it.



            tmpFile="/tmp/foo2.txt.tmp"

            cat /tmp/foo.txt | while read line; do
            isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
            if (( $isSlash )); then
            echo "$line ;try" >> $tmpFile
            else
            echo "$line ;pass" >> $tmpFile
            fi
            done

            mv $tmpFile /tmp/foo.txt





            share|improve this answer














            You can't write in the same file, I would recommend to create a new file and then rename it.



            tmpFile="/tmp/foo2.txt.tmp"

            cat /tmp/foo.txt | while read line; do
            isSlash=$(echo $line | cut -d ":" -f2 | awk 'print $1' | grep -c ^"/")
            if (( $isSlash )); then
            echo "$line ;try" >> $tmpFile
            else
            echo "$line ;pass" >> $tmpFile
            fi
            done

            mv $tmpFile /tmp/foo.txt






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited yesterday

























            answered yesterday









            Javier Salas

            1266




            1266











            • kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
              – 1010111100011
              yesterday











            • I updated my answer, awk 'print $1' will solve the issue with the empty space.
              – Javier Salas
              yesterday











            • thank you @JavierSalas
              – 1010111100011
              yesterday
















            • kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
              – 1010111100011
              yesterday











            • I updated my answer, awk 'print $1' will solve the issue with the empty space.
              – Javier Salas
              yesterday











            • thank you @JavierSalas
              – 1010111100011
              yesterday















            kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
            – 1010111100011
            yesterday





            kkk S=D D: /kkkkk ;pass it writes pass beacause of the empty space before the "/" so can we add something that ignore the empty spaces?
            – 1010111100011
            yesterday













            I updated my answer, awk 'print $1' will solve the issue with the empty space.
            – Javier Salas
            yesterday





            I updated my answer, awk 'print $1' will solve the issue with the empty space.
            – Javier Salas
            yesterday













            thank you @JavierSalas
            – 1010111100011
            yesterday




            thank you @JavierSalas
            – 1010111100011
            yesterday












            up vote
            1
            down vote













            You can use awk, which makes it very easy to accomplish what you want:



            awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt


            Output:



            xxx B=C D: A ;pass
            yyy F=H D:A ;pass
            zzz K=L D: A ;pass
            fff M=H D:/llll ;try
            kkk S=D D: /kkkkk ;try


            This basically matches every line not containing D: and / and appends ;pass to it, then skips to the next line. Otherwise it appends ;try to it.






            share|improve this answer
























              up vote
              1
              down vote













              You can use awk, which makes it very easy to accomplish what you want:



              awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt


              Output:



              xxx B=C D: A ;pass
              yyy F=H D:A ;pass
              zzz K=L D: A ;pass
              fff M=H D:/llll ;try
              kkk S=D D: /kkkkk ;try


              This basically matches every line not containing D: and / and appends ;pass to it, then skips to the next line. Otherwise it appends ;try to it.






              share|improve this answer






















                up vote
                1
                down vote










                up vote
                1
                down vote









                You can use awk, which makes it very easy to accomplish what you want:



                awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt


                Output:



                xxx B=C D: A ;pass
                yyy F=H D:A ;pass
                zzz K=L D: A ;pass
                fff M=H D:/llll ;try
                kkk S=D D: /kkkkk ;try


                This basically matches every line not containing D: and / and appends ;pass to it, then skips to the next line. Otherwise it appends ;try to it.






                share|improve this answer












                You can use awk, which makes it very easy to accomplish what you want:



                awk '!/D: *//print $0 " ;pass"; next print $0 " ;try"' foo.txt


                Output:



                xxx B=C D: A ;pass
                yyy F=H D:A ;pass
                zzz K=L D: A ;pass
                fff M=H D:/llll ;try
                kkk S=D D: /kkkkk ;try


                This basically matches every line not containing D: and / and appends ;pass to it, then skips to the next line. Otherwise it appends ;try to it.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered yesterday









                Marco Bonelli

                23312




                23312




















                    up vote
                    0
                    down vote













                    sed OK?



                    $ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
                    xxx B=C D: A ;pass
                    yyy F=H D:A ;pass
                    zzz K=L D: A ;pass
                    fff M=H D:/llll ;try
                    kkk S=D D: /kkkkk ;try





                    share|improve this answer




















                    • posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
                      – Isaac
                      yesterday














                    up vote
                    0
                    down vote













                    sed OK?



                    $ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
                    xxx B=C D: A ;pass
                    yyy F=H D:A ;pass
                    zzz K=L D: A ;pass
                    fff M=H D:/llll ;try
                    kkk S=D D: /kkkkk ;try





                    share|improve this answer




















                    • posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
                      – Isaac
                      yesterday












                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    sed OK?



                    $ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
                    xxx B=C D: A ;pass
                    yyy F=H D:A ;pass
                    zzz K=L D: A ;pass
                    fff M=H D:/llll ;try
                    kkk S=D D: /kkkkk ;try





                    share|improve this answer












                    sed OK?



                    $ sed '/D: *A/ s/$/t;pass/; /D: *// s/$/t;try/' file
                    xxx B=C D: A ;pass
                    yyy F=H D:A ;pass
                    zzz K=L D: A ;pass
                    fff M=H D:/llll ;try
                    kkk S=D D: /kkkkk ;try






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered yesterday









                    RudiC

                    2,9311211




                    2,9311211











                    • posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
                      – Isaac
                      yesterday
















                    • posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
                      – Isaac
                      yesterday















                    posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
                    – Isaac
                    yesterday




                    posixly: sed -e 's+D: */.*+&t;try+ ; t' -e 's+$+t;pass+'
                    – Isaac
                    yesterday

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f481519%2fread-from-a-file-and-return-new-line-with-loop%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?