How to search and replace string using perl?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Elizabeth H
    New Member
    • Jan 2011
    • 19

    How to search and replace string using perl?

    Hi,

    I had a few questions regarding searching for a string and replacing it using perl scripts. I have 100's of files written in Unix shell scripts and I need to edit them all. Its like
    Code:
    function1()
    { 
             variable=xxx
    }
     
    function 2()
    {
             variable='aaa bbb ccc ddd'
    }
    I have to search exactly for function 2 and only replace the variable in function 2. I have to append a new value ggg to the variable only in function 2.

    Everytime I try doing this with the perl one liner 's/searchtext/replacetext/g', it inserts in the wrong position and in the wrong function. Can you please help me with this as I am completely new to perl

    Thanks
    Last edited by numberwhun; Jan 21 '11, 03:40 PM. Reason: Please use code tags!
  • rovf
    New Member
    • May 2010
    • 41

    #2
    I could imagine two approaches:

    (1) Your attempted solution processes one line at a time. If you would instead slurp the whole file into a big string, you could then include the function name into your search.

    (2) If you prefer processing line-by-line, you can keep track of the functions being seen, and substitute only after you have seen function2. Perls ".." operator could be handy here.

    Comment

    • rski
      Recognized Expert Contributor
      • Dec 2006
      • 700

      #3
      You need to define range and change within it
      Code:
      sed  -i  "/function 2()/,/}/ s/variable='\(.*\)'/variable='\1 ggg'/" 1.txt

      Comment

      • Elizabeth H
        New Member
        • Jan 2011
        • 19

        #4
        That was such a great answer and worked really really well...Thankss a bunch..I have one more issue....In the above same problem..How do I insert the new string "ggg" exactly after the string "ccc" or so...is there a position based substitution?

        Comment

        • rski
          Recognized Expert Contributor
          • Dec 2006
          • 700

          #5
          Code:
          sed  -i  "/function 2()/,/}/ s/variable='\(.* ccc\)\(.*\)'/variable='\1 ggg\2'/" 1.txt

          Comment

          • Elizabeth H
            New Member
            • Jan 2011
            • 19

            #6
            Thnks again...Could you please explain what it does..I especially the 1 and 2

            Comment

            • rski
              Recognized Expert Contributor
              • Dec 2006
              • 700

              #7
              The pieces
              Code:
              \(.* ccc\) and \(.*\)
              are so called capturing groups.
              And \1 and \2 are capturing groups result (so called backreference) so \1 is a string that will match
              ".* ccc" and \2 is a string that match ".*"
              I'm not sure if understand what I mean (is is hard to explain in a few words) so if you google for "regexp backreference" you will find clear explanation.

              Comment

              • Elizabeth H
                New Member
                • Jan 2011
                • 19

                #8
                Thanks a lot! :)

                Comment

                • Elizabeth H
                  New Member
                  • Jan 2011
                  • 19

                  #9
                  I also had another question. In the same scenario above, how do I set two variables VAR1 and VAR2(both used in function1) to blank at the end of function1(). Do i need to write a small script or if there is a one liner which can search for function1() and set those variables to blank at the end of the function. Thanks

                  Comment

                  • rovf
                    New Member
                    • May 2010
                    • 41

                    #10
                    > how do I set two variables VAR1 and VAR2(both used in function1) to blank at the end of function1

                    This is tricky, because you need to recognize the end of function1. It depends what you know about "function1" . For example, if you know that your input files are formatted properly, you can parse the file until you encounter /^function1/, then continue parsing until you encounter /^\}/, and insert your statement just before.

                    Comment

                    • Elizabeth H
                      New Member
                      • Jan 2011
                      • 19

                      #11
                      Thanks..lemme try

                      Comment

                      • Prakash Gnana
                        New Member
                        • Jan 2011
                        • 25

                        #12
                        Thanks rski

                        Comment

                        • Elizabeth H
                          New Member
                          • Jan 2011
                          • 19

                          #13
                          Can I have some more clue on this as the function itself has several small blocks of statements with closing curly braces which makes it hard for me to parse exactly till end of function where I need to put my two variables

                          Comment

                          • rovf
                            New Member
                            • May 2010
                            • 41

                            #14
                            If we know that the functions are nicely formatted, you just need to have a look at closing braces in column 1.

                            If this is not the case, you would basically need a Shell script parser, because even counting opening and closing braces would not help (because they might also occur, unmatched, in arbitrary command line arguments, HERE-documents, comments). With other words: If you want to succeed using simple line-by-line reading of your file and checking the lines by regular expressions, you need to specify pretty exactly how the "inside" of your functions might look like. If the answer is "arbitrary shell code", then your analysis program needs to be as powerful as the parser of the shell.

                            Comment

                            Working...