Loading ...
Sorry, an error occurred while loading the content.
 

Re: [blug-prog] Scripting question.......

Expand Messages
  • Mayank Rungta
    ... I think you can do it easily using q comand in Vim. You can open all the files that need to be changed using vim * in the directory containing the files.
    Message 1 of 12 , Sep 29, 2005
      Pradeep Goyal wrote:

      >i want to do certain operations over a file using vi editor like :
      > 1) search <abc123> string.
      >2) copy 4 lines from there onwards
      >3) serach <xyz123> in the same file. and go to 3rd line after that.
      >4) paste the copied lines.
      > i need to do this for almost 200 files. Is there any way to do it
      >automatcally using "vi" or "vim"
      > I know there is way where you can record what ever you are doing it on vi
      >and repeat it over certain number of files. but really not getting it how to
      >do it.
      >Any suggestion could be very helpful.
      > thanks
      >Pardeep

      I think you can do it easily using q comand in Vim. You can open all the
      files that need to be changed using vim * in the directory containing
      the files.
      Use q comand to record your commands and run on all the files as below:

      1. qq (q followed by any alphabet of your choice - I prefer q)
      2. do the steps you mention while it is being recorded.
      3. Once thru in first file move to the next file using :wn (write and
      move to next file).
      4. press q to stop recording - You have recorded steps to do your job
      and move to next file and need to repeat this.
      5. Use @q (the letter that was used at the time of starting the recording).
      6. If it is working fine then repeat the command using @@. Once
      confident key in the no. of times u want to repeat the command and voila
      all your files
      are through.

      The glitch is exactly same steps need to be followed in each file and it
      looks ok for your requirement. Just make sure you don't use copy paste
      and history
      during the recording as it may do funny things in the next files! :-)

      Happy vimming,
      Mynk
    • Narendran Kumaraguru Nathan
      ... Hi Pradeep, You could write down all the vim-commands in a file and use it as a script using the -s option to vim. Use a final :q in your script to exit
      Message 2 of 12 , Sep 29, 2005
        --- Pradeep Goyal <csclin01@...> wrote:

        > i want to do certain operations over a file using vi editor like :
        > 1) search <abc123> string.
        > 2) copy 4 lines from there onwards
        > 3) serach <xyz123> in the same file. and go to 3rd line after that.
        > 4) paste the copied lines.
        > i need to do this for almost 200 files. Is there any way to do it
        > automatcally using "vi" or "vim"
        > I know there is way where you can record what ever you are doing it on vi
        > and repeat it over certain number of files. but really not getting it how to
        > do it.
        > Any suggestion could be very helpful.
        > thanks
        > Pardeep
        >


        Hi Pradeep,
        You could write down all the vim-commands in a file and use it as a script
        using the -s option to vim. Use a final ":q" in your script to exit the file.
        HTH
        Naren.





        ___________________________________________________________
        Yahoo! Messenger - NEW crystal clear PC to PC calling worldwide with voicemail http://uk.messenger.yahoo.com
      • Ganesan Rajagopal
        ... It s amazing that people are still suggesting solutions to do this in vim. However, considering that there are two hundred files, this is the sanest (FWIW)
        Message 3 of 12 , Sep 29, 2005
          >>>>> "Narendran" == Narendran Kumaraguru Nathan <narenkumaraguru@...> writes:

          > --- Pradeep Goyal <csclin01@...> wrote:

          >> i want to do certain operations over a file using vi editor like :
          >> 1) search <abc123> string.
          >> 2) copy 4 lines from there onwards
          >> 3) serach <xyz123> in the same file. and go to 3rd line after that.
          >> 4) paste the copied lines.
          >> i need to do this for almost 200 files. Is there any way to do it
          >> automatcally using "vi" or "vim"
          >> I know there is way where you can record what ever you are doing it on vi
          >> and repeat it over certain number of files. but really not getting it how to
          >> do it.
          >> Any suggestion could be very helpful.
          >> thanks
          >> Pardeep
          >>



          > Hi Pradeep,
          > You could write down all the vim-commands in a file and use it as a script
          > using the -s option to vim. Use a final ":q" in your script to exit the file.
          > HTH
          > Naren.

          It's amazing that people are still suggesting solutions to do this in
          vim. However, considering that there are two hundred files, this is the
          sanest (FWIW) solution I've heard so far. We've seen a Bourne shell
          solution, let me a give it a shot in perl

          =====
          #!/usr/bin/perl

          my @match = ();
          my $copy = 0;

          while (<>) {
          if ($copy) {
          push @match, $_;
          $copy = 0 if (@match == 4);
          }

          if (/<abc123>/) { $copy = 1; }

          print;

          if (/<xyz123>/) {
          print @match;
          @match = ();
          }
          }
          ====

          --
          Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
          Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
        • Philip Tellis
          Sometime Today, GR cobbled together some glyphs to say: Was wondering when someone would provide a perl suggestion :P ... missing use strict and use warnings
          Message 4 of 12 , Sep 29, 2005
            Sometime Today, GR cobbled together some glyphs to say:

            Was wondering when someone would provide a perl suggestion :P

            > #!/usr/bin/perl

            missing use strict and use warnings :D

            >
            > my @match = ();
            > my $copy = 0;
            >
            > while (<>) {
            > if ($copy) {
            > push @match, $_;
            > $copy = 0 if (@match == 4);
            > }

            [snip]

            anyway, I'd do something like this:

            perl -wpi -e '
            /<abc123>/../<xyz123>/ and $buffer .= $_;
            /<xyz123>/ and do { $_ .= $1 if $buffer =~ /<abc123>(.*)<xyz123>/s; $buffer="";}
            ' file1 file2 file3 file 4

            (note, no use strict to allow for auto vivification of $buffer)

            Philip

            --
            "I have five dollars for each of you."
            -- Bernhard Goetz
          • Ganesan Rajagopal
            ... Guilty as charged! I always use -w, don t know how I missed it this time. ... I d prefer -wpi.bak. I have hubris but not that much ;-). ... Nice, I don t
            Message 5 of 12 , Sep 29, 2005
              >>>>> "Philip" == Philip Tellis <philip.tellis@...> writes:

              > Sometime Today, GR cobbled together some glyphs to say:
              > Was wondering when someone would provide a perl suggestion :P

              >> #!/usr/bin/perl

              > missing use strict and use warnings :D

              Guilty as charged! I always use -w, don't know how I missed it this
              time.

              > perl -wpi -e

              I'd prefer -wpi.bak. I have hubris but not that much ;-).

              > /<abc123>/../<xyz123>/ and $buffer .= $_;

              Nice, I don't remember using the .. technique before, thanks. However, this
              doesn't solve the original problem, you need to copy only 4 lines, not all
              the lines in between.

              > (note, no use strict to allow for auto vivification of $buffer)

              Didn't understand this part. $buffer is not a reference.

              Ganesan

              --
              Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
              Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
            • Ganesan Rajagopal
              ... In case pythoneers feel left out, a literal translation of my perl version (not tested) ===== #!/usr/bin/python import re import fileinput match = [] copy
              Message 6 of 12 , Sep 29, 2005
                >>>>> "Philip" == Philip Tellis <philip.tellis@...> writes:

                > Was wondering when someone would provide a perl suggestion :P

                In case pythoneers feel left out, a literal translation of my perl version
                (not tested)

                =====
                #!/usr/bin/python

                import re
                import fileinput

                match = []
                copy = False
                for line in fileinput.input():
                if copy:
                match.append(line)
                if len(match) == 4:
                copy = False
                if re.match(r"<abc123>", line):
                copy = True
                print line,
                if re.match(r"<xyz123>", line):
                for l in match:
                print l,
                match = []

                =====


                --
                Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
                Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
              • Laxminarayan Kamath
                ... ..... Nice excercise for a programming contest .. hmm? -- Laxminarayan Kamath Ammembal (+91) 9342287956 laxminarayan@deeproot.co.in
                Message 7 of 12 , Sep 29, 2005
                  On 9/29/05, Pradeep Goyal <csclin01@...> wrote:
                  > i want to do certain operations over a file using vi editor like :
                  > 1) search <abc123> string.
                  > 2) copy 4 lines from there onwards
                  .....<snip/>
                  Nice excercise for a programming contest .. hmm?


                  --
                  Laxminarayan Kamath Ammembal
                  (+91) 9342287956
                  laxminarayan@...
                  www.geocities.com/kamathln
                • Philip Tellis
                  ... $buffer .= $_ if / /../ / && 4 == scalar @{[$buffer =~ / n/gs]}; I m thinking about how to use $. for this but it may be a little longer. ...
                  Message 8 of 12 , Sep 29, 2005
                    Sometime Today, GR cobbled together some glyphs to say:

                    >> /<abc123>/../<xyz123>/ and $buffer .= $_;
                    >
                    > Nice, I don't remember using the .. technique before, thanks. However,
                    > this doesn't solve the original problem, you need to copy only 4
                    > lines, not all the lines in between.

                    $buffer .= $_ if /<abc123>/../<xyz123>/ && 4 == scalar @{[$buffer =~ /\n/gs]};

                    I'm thinking about how to use $. for this but it may be a little longer.

                    >> (note, no use strict to allow for auto vivification of $buffer)
                    >
                    > Didn't understand this part. $buffer is not a reference.

                    autovivification means the creation of a variable on first use. A
                    scalar is initialised to the empty string or 0 depending on the context
                    in which it is used. An array or hash is initialised to the empty list.

                    --
                    The real fun of living wisely is that you get to be smug about it.
                    -- Calvin
                  • Ganesan Rajagopal
                    ... I know autovivification with respect to building complex datastructures using references. I haven t seen the term applied to scalars. However, admittedly I
                    Message 9 of 12 , Sep 30, 2005
                      >>>>> "Philip" == Philip Tellis <philip.tellis@...> writes:

                      > autovivification means the creation of a variable on first use. A
                      > scalar is initialised to the empty string or 0 depending on the context
                      > in which it is used. An array or hash is initialised to the empty list.

                      I know autovivification with respect to building complex datastructures
                      using references. I haven't seen the term applied to scalars. However,
                      admittedly I am not too much into Perl these days, so my memory may be
                      failing :-).

                      Ganesan

                      --
                      Ganesan Rajagopal (rganesan at debian.org) | GPG Key: 1024D/5D8C12EA
                      Web: http://employees.org/~rganesan | http://rganesan.blogspot.com
                    Your message has been successfully submitted and would be delivered to recipients shortly.