remove string ends with certain extension in a file

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











up vote
0
down vote

favorite












Need to remove .sql entry in file



I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file



file.txt



actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql
test4.sql
test5.sql


Output file which i expect is



actual.class
actual1.class
actual2.class
actual3.class
actual4.class


Using below script but not able to achieve



for i in `cat file.txt` ; do
fname=`echo $i | cut -d "." -f1`
echo $fname
if file=$fname.sql
then
sed "/$fname.sql/d" file.txt > output_file.txt
else
exit 0
fi
done


with above script getting below output



actual.class
actual1.class
actual2.class
actual3.class
actual4.class
test.sql
test2.sql
test3.sql






share|improve this question


























    up vote
    0
    down vote

    favorite












    Need to remove .sql entry in file



    I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file



    file.txt



    actual.class
    actual1.class
    actual2.class
    actual3.class
    actual4.class
    test.sql
    test2.sql
    test3.sql
    test4.sql
    test5.sql


    Output file which i expect is



    actual.class
    actual1.class
    actual2.class
    actual3.class
    actual4.class


    Using below script but not able to achieve



    for i in `cat file.txt` ; do
    fname=`echo $i | cut -d "." -f1`
    echo $fname
    if file=$fname.sql
    then
    sed "/$fname.sql/d" file.txt > output_file.txt
    else
    exit 0
    fi
    done


    with above script getting below output



    actual.class
    actual1.class
    actual2.class
    actual3.class
    actual4.class
    test.sql
    test2.sql
    test3.sql






    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Need to remove .sql entry in file



      I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file



      file.txt



      actual.class
      actual1.class
      actual2.class
      actual3.class
      actual4.class
      test.sql
      test2.sql
      test3.sql
      test4.sql
      test5.sql


      Output file which i expect is



      actual.class
      actual1.class
      actual2.class
      actual3.class
      actual4.class


      Using below script but not able to achieve



      for i in `cat file.txt` ; do
      fname=`echo $i | cut -d "." -f1`
      echo $fname
      if file=$fname.sql
      then
      sed "/$fname.sql/d" file.txt > output_file.txt
      else
      exit 0
      fi
      done


      with above script getting below output



      actual.class
      actual1.class
      actual2.class
      actual3.class
      actual4.class
      test.sql
      test2.sql
      test3.sql






      share|improve this question














      Need to remove .sql entry in file



      I have file which contains strings ends with both .class and .sql and I need to remove only string which ends with .sql in the file



      file.txt



      actual.class
      actual1.class
      actual2.class
      actual3.class
      actual4.class
      test.sql
      test2.sql
      test3.sql
      test4.sql
      test5.sql


      Output file which i expect is



      actual.class
      actual1.class
      actual2.class
      actual3.class
      actual4.class


      Using below script but not able to achieve



      for i in `cat file.txt` ; do
      fname=`echo $i | cut -d "." -f1`
      echo $fname
      if file=$fname.sql
      then
      sed "/$fname.sql/d" file.txt > output_file.txt
      else
      exit 0
      fi
      done


      with above script getting below output



      actual.class
      actual1.class
      actual2.class
      actual3.class
      actual4.class
      test.sql
      test2.sql
      test3.sql








      share|improve this question













      share|improve this question




      share|improve this question








      edited Dec 6 '17 at 10:45









      Rahul

      8,52612841




      8,52612841










      asked Dec 6 '17 at 10:27









      Bindhu

      1




      1




















          4 Answers
          4






          active

          oldest

          votes

















          up vote
          2
          down vote













          grep solution:



          grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt


          The 2nd grep statement/command will be executed only if the 1st one returned zero exit status 0 if any match against pattern ..sql$ is found




          The final output_file.txt content:



          actual.class
          actual1.class
          actual2.class
          actual3.class
          actual4.class





          share|improve this answer



























            up vote
            1
            down vote














            • Using sed



              sed '/.sql$/d' test.txt



            • For bash (3.2+)



              while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt



            • Using perl



              perl -ni.bak -e "print unless /.sql/" test.txt






            share|improve this answer


















            • 1




              Anchoring the pattern to $ end-of-line would be an improvement
              – Jeff Schaller
              Dec 6 '17 at 11:10

















            up vote
            0
            down vote













            I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.




            • for i in $(cat ...) is not good as it seperates your input by spaces by default as well, not just by newline.

            • The if statement doesn't make sense as file is not defined. For a typical if statement you would need to invoke test, usually by putting things inside a pair of square brackets. For example, if [ $f = "abc" ]; then. Note that all spaces here are necessary.


            • exit 0 stops the script from handling all the lines after the given line, I believe that is not intended.

            I hope these can help you write better shell scripts.






            share|improve this answer



























              up vote
              0
              down vote













              Another grep, just invert the match



              grep -v ".sql$" filelist





              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%2f409154%2fremove-string-ends-with-certain-extension-in-a-file%23new-answer', 'question_page');

                );

                Post as a guest






























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes








                up vote
                2
                down vote













                grep solution:



                grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt


                The 2nd grep statement/command will be executed only if the 1st one returned zero exit status 0 if any match against pattern ..sql$ is found




                The final output_file.txt content:



                actual.class
                actual1.class
                actual2.class
                actual3.class
                actual4.class





                share|improve this answer
























                  up vote
                  2
                  down vote













                  grep solution:



                  grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt


                  The 2nd grep statement/command will be executed only if the 1st one returned zero exit status 0 if any match against pattern ..sql$ is found




                  The final output_file.txt content:



                  actual.class
                  actual1.class
                  actual2.class
                  actual3.class
                  actual4.class





                  share|improve this answer






















                    up vote
                    2
                    down vote










                    up vote
                    2
                    down vote









                    grep solution:



                    grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt


                    The 2nd grep statement/command will be executed only if the 1st one returned zero exit status 0 if any match against pattern ..sql$ is found




                    The final output_file.txt content:



                    actual.class
                    actual1.class
                    actual2.class
                    actual3.class
                    actual4.class





                    share|improve this answer












                    grep solution:



                    grep -q '..sql$' file.txt && grep -v '.sql$' file.txt > output_file.txt


                    The 2nd grep statement/command will be executed only if the 1st one returned zero exit status 0 if any match against pattern ..sql$ is found




                    The final output_file.txt content:



                    actual.class
                    actual1.class
                    actual2.class
                    actual3.class
                    actual4.class






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Dec 6 '17 at 10:36









                    RomanPerekhrest

                    22.4k12145




                    22.4k12145






















                        up vote
                        1
                        down vote














                        • Using sed



                          sed '/.sql$/d' test.txt



                        • For bash (3.2+)



                          while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt



                        • Using perl



                          perl -ni.bak -e "print unless /.sql/" test.txt






                        share|improve this answer


















                        • 1




                          Anchoring the pattern to $ end-of-line would be an improvement
                          – Jeff Schaller
                          Dec 6 '17 at 11:10














                        up vote
                        1
                        down vote














                        • Using sed



                          sed '/.sql$/d' test.txt



                        • For bash (3.2+)



                          while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt



                        • Using perl



                          perl -ni.bak -e "print unless /.sql/" test.txt






                        share|improve this answer


















                        • 1




                          Anchoring the pattern to $ end-of-line would be an improvement
                          – Jeff Schaller
                          Dec 6 '17 at 11:10












                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote










                        • Using sed



                          sed '/.sql$/d' test.txt



                        • For bash (3.2+)



                          while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt



                        • Using perl



                          perl -ni.bak -e "print unless /.sql/" test.txt






                        share|improve this answer















                        • Using sed



                          sed '/.sql$/d' test.txt



                        • For bash (3.2+)



                          while read -r line; do [[ ! $line =~ .sql ]] && echo "$line"; done <test.txt



                        • Using perl



                          perl -ni.bak -e "print unless /.sql/" test.txt







                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited Dec 6 '17 at 11:21









                        Philippos

                        5,91211546




                        5,91211546










                        answered Dec 6 '17 at 10:39









                        Rahul

                        8,52612841




                        8,52612841







                        • 1




                          Anchoring the pattern to $ end-of-line would be an improvement
                          – Jeff Schaller
                          Dec 6 '17 at 11:10












                        • 1




                          Anchoring the pattern to $ end-of-line would be an improvement
                          – Jeff Schaller
                          Dec 6 '17 at 11:10







                        1




                        1




                        Anchoring the pattern to $ end-of-line would be an improvement
                        – Jeff Schaller
                        Dec 6 '17 at 11:10




                        Anchoring the pattern to $ end-of-line would be an improvement
                        – Jeff Schaller
                        Dec 6 '17 at 11:10










                        up vote
                        0
                        down vote













                        I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.




                        • for i in $(cat ...) is not good as it seperates your input by spaces by default as well, not just by newline.

                        • The if statement doesn't make sense as file is not defined. For a typical if statement you would need to invoke test, usually by putting things inside a pair of square brackets. For example, if [ $f = "abc" ]; then. Note that all spaces here are necessary.


                        • exit 0 stops the script from handling all the lines after the given line, I believe that is not intended.

                        I hope these can help you write better shell scripts.






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.




                          • for i in $(cat ...) is not good as it seperates your input by spaces by default as well, not just by newline.

                          • The if statement doesn't make sense as file is not defined. For a typical if statement you would need to invoke test, usually by putting things inside a pair of square brackets. For example, if [ $f = "abc" ]; then. Note that all spaces here are necessary.


                          • exit 0 stops the script from handling all the lines after the given line, I believe that is not intended.

                          I hope these can help you write better shell scripts.






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.




                            • for i in $(cat ...) is not good as it seperates your input by spaces by default as well, not just by newline.

                            • The if statement doesn't make sense as file is not defined. For a typical if statement you would need to invoke test, usually by putting things inside a pair of square brackets. For example, if [ $f = "abc" ]; then. Note that all spaces here are necessary.


                            • exit 0 stops the script from handling all the lines after the given line, I believe that is not intended.

                            I hope these can help you write better shell scripts.






                            share|improve this answer












                            I think there are already good solutions before this post. However I would like to point out several issues with the script originally posted.




                            • for i in $(cat ...) is not good as it seperates your input by spaces by default as well, not just by newline.

                            • The if statement doesn't make sense as file is not defined. For a typical if statement you would need to invoke test, usually by putting things inside a pair of square brackets. For example, if [ $f = "abc" ]; then. Note that all spaces here are necessary.


                            • exit 0 stops the script from handling all the lines after the given line, I believe that is not intended.

                            I hope these can help you write better shell scripts.







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Dec 6 '17 at 14:17









                            Weijun Zhou

                            1,434119




                            1,434119




















                                up vote
                                0
                                down vote













                                Another grep, just invert the match



                                grep -v ".sql$" filelist





                                share|improve this answer
























                                  up vote
                                  0
                                  down vote













                                  Another grep, just invert the match



                                  grep -v ".sql$" filelist





                                  share|improve this answer






















                                    up vote
                                    0
                                    down vote










                                    up vote
                                    0
                                    down vote









                                    Another grep, just invert the match



                                    grep -v ".sql$" filelist





                                    share|improve this answer












                                    Another grep, just invert the match



                                    grep -v ".sql$" filelist






                                    share|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered Dec 6 '17 at 16:30









                                    bu5hman

                                    1,164214




                                    1,164214



























                                         

                                        draft saved


                                        draft discarded















































                                         


                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function ()
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f409154%2fremove-string-ends-with-certain-extension-in-a-file%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?

                                        Bahrain

                                        Postfix configuration issue with fips on centos 7; mailgun relay