11 Replies Latest reply: Jul 12, 2012 12:14 PM by Catch-22 RSS

    find/replace text

    Roger25
      *
        • 1. Re: find/replace text
          Catch-22
          I have no idea if what you are trying to do makes any sense, but the syntax is relatively easy:
          in="raise_application_error(-20XXX, '<error text message>');"
          out="whatever"
          
          find / -name '*.sql' -exec sed -i "s/$in/$out/g" {} \;
          • 2. Re: find/replace text
            Roger25
            We have many raise_application_error in code. We need to replace all this occurences with something other, this is our business.
            So, for that "in=.......", how can we dynamically find the entire line containing that code?

            e.g. in file1.sql i have
            raise_application_Error(-20114, 'asdasdasdasd');
            ....
            and in file2.sql:
            ...
            raise_application_Error(-20560, 'other error');
            ...
            So i need to find those lines from all .sql files and replace them with "whatever"

            Thanks
            • 3. Re: find/replace text
              Catch-22
              The following should do the trick. It will search from the current directory downwards. Just type it at the command prompt:
              in="raise_application_error(.*);"
              out="raise_application_error(-20XXX, 'whatever');"
              find ./ -name '*.sql' -exec sed -i "s/$in/$out/g" {} \;
              • 4. Re: find/replace text
                EdStevens
                Roger22 wrote:
                Hello,
                We want to replace all occurences of raise_application_error(-20XXX, '<error text message>'); from all oracle sources (all .sql files on the server).
                How can we make use of GREP and SED to achieve this?
                Thanks
                What about the same code that exists in stored procedures within the db?
                • 5. Re: find/replace text
                  Roger25
                  Thank you. However, i have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column of the file, with the contents of the 2nd column

                  e.g. the file can contain:

                  COL1 -- COL2
                  old_one new_one

                  So for all .sql files, i want to replace the string 'old_one' with 'new_one'.
                  Is it possible this?

                  Thanks!
                  • 6. Re: find/replace text
                    Catch-22
                    I think I have given you a valid solution for your initial inquiry. I have a question for you to? What do you mean by "…this is our business.", which you mentioned in your 2nd post?
                    • 7. Re: find/replace text
                      Roger25
                      Yes, you gave me a "static" solution, but how to read those "in" and "out" from a .csv file, "in" being the first column, and "out" the second column?
                      • 8. Re: find/replace text
                        EdStevens
                        Roger22 wrote:
                        Yes, you gave me a "static" solution, but how to read those "in" and "out" from a .csv file, "in" being the first column, and "out" the second column?
                        A couple of minutes work in vi and you could transform that csv file from
                        abc,123
                        def,456
                        ghi,789
                        to
                        find / -name '*.sql' -exec sed -i "s/abc/123/g" {} \;
                        find / -name '*.sql' -exec sed -i "s/def/456/g" {} \;
                        find / -name '*.sql' -exec sed -i "s/ghi/789/g" {} \;
                        Actually, only three vi commands:
                        1,$s/^/find \/ -name '*.sql' -exec sed -i "s/
                        1,$s/,/\//
                        1,$s/$/\/g" {} \\;/
                        (untested and may need tweaking but you get the idea)

                        make it executable, and run it.
                        Probably only practical if this is a one-off

                        If you are looking to developer a generalize 'replacer' utility, that would be a different matter.
                        • 9. Re: find/replace text
                          Roger25
                          Thanks for reply.
                          Ok, for every match in the csv file i should write a corresponding line, like:

                          find / -name '*.sql' -exec sed -i "s/<string to be replaced>/<replacing string>/g" {} \;


                          but i don't understand this:
                          1,$s/^/find \/ -name '*.sql' -exec sed -i "s/
                          1,$s/,/\//
                          1,$s/$/\/g" {} \\;/
                          What is supposed to do this code and where should i write it?
                          Regards,
                          • 10. Re: find/replace text
                            EdStevens
                            Roger22 wrote:
                            Thanks for reply.
                            Ok, for every match in the csv file i should write a corresponding line, like:

                            find / -name '*.sql' -exec sed -i "s/<string to be replaced>/<replacing string>/g" {} \;


                            but i don't understand this:
                            1,$s/^/find \/ -name '*.sql' -exec sed -i "s/
                            1,$s/,/\//
                            1,$s/$/\/g" {} \\;/
                            What is supposed to do this code and where should i write it?
                            Regards,
                            Those are the ed commands to issue when you have your csv file open in vi. That will transform it from just the source and target string pairs to the full 'find' command.
                            You start each ed command with a colon.
                            :1,$s/^/find \/ -name '*.sql' -exec sed -i "s/
                            {code}
                            
                            Interpretation:
                            {code}
                            :1,$s/^/find \/ -name '*.sql' -exec sed -i "s/
                            ++ +++++     +                               +- terminate substitution string
                            || |||||     +----------------------------------escape next character so it is not interpreted as a delimiter
                            || ||||+----------------------------------------delimiter between source and target substitution strings
                            || |||+-----------------------------------------caret means 'beginning of line
                            || ||+------------------------------------------delimter beginning sourc substitution string
                            || |+-------------------------------------------'s' means substitue - just like your sed command
                            || +--------------------------------------------last line of substitution range - $ means 'last line in file'
                            |+----------------------------------------------first line of stubstitution range
                            +-----------------------------------------------begin a command
                            So in the above we are saying
                            for every line from 1 to the end of the file ...
                            substitute
                            the beginning of the line
                            the literal string: find / -name '*.sql' -exec sed -i "s

                            like I said it may take a little tweaking to deal with meta characters that need to be escaped, and make sure you include spaces where needed.
                            • 11. Re: find/replace text
                              Catch-22
                              Yes, you gave me a "static" solution, but how to read those "in" and "out" from a .csv file, "in" being the first column, and "out" the second column?
                              Well, I gave you an answer to your actual inquiry plus another modification for a dynamic search string . However, you come up with additional requirements as the thread develops, which makes it no fun.

                              If I'm not mistaken, you now want the search and replace strings for your search and replace tasks to be retrieved from an external file.

                              You have not given an answer to my question or given any credits, and it does not seem you are actually trying to put in much effort to understand or find a solution for the task yourself. I also see that you have posted the same question at other forums on the web. That's all up to you, but I think it is not within the spirit of this forum to provide a complete solution for someone to be able to run a business as you mentioned. Sorry, perhaps other people may have a different opinion, but that's how I see it.