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

Re: [PBML] A Perl Question (Was: Not a perl question)

Expand Messages
  • Jeff Eggen
    ... Any reason that this isn t a Perl question? Perl is what I would use for this, and if you re on a recent build of Solaris you should have Perl installed
    Message 1 of 7 , Nov 3, 2003
    • 0 Attachment
      >>> sreeramabsc@... 11/03/03 05:11am >>>
      >Hi ,
      > This is not a perl question.. but since it needed this info quickly .. I am posting it here. Please don't mind.
      >
      >I have a query. I would like to change all occurences of a word,say data1, to another word, say info1 in a file, say file1. I would like to do it using command-line.
      >I tried using the command sed. I tried it in Solaris..like this:
      > /usr/xpg4/bin/sed -n 's/data1/info1/g' file1
      > I could see the changed data on the screen.. but the changes were not done in the file. THen I tried this:
      > /usr/xpg4/bin/sed -n 's/data1/info/w file2' file1
      > Now file2 contained only those lines which contained info1(i.e which contained data1 in original file) but not the other contents of original file.
      > I would like to know the command-line for changing the contents of the file and retain the non-changed parts of the file.

      Any reason that this isn't a Perl question? Perl is what I would use for this, and if you're on a recent build of Solaris you should have Perl installed in /usr/bin.

      perl -pi.orig -e 's/data1/info1/g' file1

      This command will make a copy of file1, called file1.orig. It will then cycle through each line of file1, and run the substitution against it. The results of the substitution, whether there was a match or not, are written to file1.

      See "perldoc perlrun" for what the -p, -i, and -e options do.

      If this can't have a Perl solution, then there are people here who do mind. A lot. Just so you know.

      Hope this helps,

      Jeff Eggen
      IT Programmer Analyst
      Saskatchewan Government Insurance
      Ph (306) 751-1795
      email jeggen@...
    • Sreeram B S
      Really an excellent solution.. Thanks a lot. Sreeram ... Any reason that this isn t a Perl question? Perl is what I would use for this, and if you re on a
      Message 2 of 7 , Nov 3, 2003
      • 0 Attachment
        Really an excellent solution.. Thanks a lot.

        Sreeram

        Jeff Eggen <jeggen@...> wrote:
        >>> sreeramabsc@... 11/03/03 05:11am >>>
        >Hi ,
        > This is not a perl question.. but since it needed this info quickly .. I am posting it here. Please don't mind.
        >
        >I have a query. I would like to change all occurences of a word,say data1, to another word, say info1 in a file, say file1. I would like to do it using command-line.
        >I tried using the command sed. I tried it in Solaris..like this:
        > /usr/xpg4/bin/sed -n 's/data1/info1/g' file1
        > I could see the changed data on the screen.. but the changes were not done in the file. THen I tried this:
        > /usr/xpg4/bin/sed -n 's/data1/info/w file2' file1
        > Now file2 contained only those lines which contained info1(i.e which contained data1 in original file) but not the other contents of original file.
        > I would like to know the command-line for changing the contents of the file and retain the non-changed parts of the file.

        Any reason that this isn't a Perl question? Perl is what I would use for this, and if you're on a recent build of Solaris you should have Perl installed in /usr/bin.

        perl -pi.orig -e 's/data1/info1/g' file1

        This command will make a copy of file1, called file1.orig. It will then cycle through each line of file1, and run the substitution against it. The results of the substitution, whether there was a match or not, are written to file1.

        See "perldoc perlrun" for what the -p, -i, and -e options do.

        If this can't have a Perl solution, then there are people here who do mind. A lot. Just so you know.

        Hope this helps,

        Jeff Eggen
        IT Programmer Analyst
        Saskatchewan Government Insurance
        Ph (306) 751-1795
        email jeggen@...


        Yahoo! Groups Sponsor
        Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

        Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


        ---------------------------------
        Do you Yahoo!?
        Exclusive Video Premiere - Britney Spears

        [Non-text portions of this message have been removed]
      • Sreeram B S
        Hi, The command: perl -pi -e s/fmt1/fmt2/g file1 will change all occurences of fmt1 with fmt2 and we get the results in file1 only. But I am not able to use
        Message 3 of 7 , Nov 4, 2003
        • 0 Attachment
          Hi,
          The command:
          perl -pi -e 's/fmt1/fmt2/g' file1
          will change all occurences of fmt1 with fmt2 and we get the results in file1 only.
          But I am not able to use this command from a script..like this:

          system("perl -pi -e 's/fmt1/fmt2/g' file1");
          or `perl -pi -e 's/fmt1/fmt2/g' file1 `

          Why is this happening..?
          Please tell the modifications to the command(if any) to use it in the script.

          Thanks in advance.

          From,
          Sreeram

          Jeff Eggen <jeggen@...> wrote:
          >>> sreeramabsc@... 11/03/03 05:11am >>>
          >Hi ,
          > This is not a perl question.. but since it needed this info quickly .. I am posting it here. Please don't mind.
          >
          >I have a query. I would like to change all occurences of a word,say data1, to another word, say info1 in a file, say file1. I would like to do it using command-line.
          >I tried using the command sed. I tried it in Solaris..like this:
          > /usr/xpg4/bin/sed -n 's/data1/info1/g' file1
          > I could see the changed data on the screen.. but the changes were not done in the file. THen I tried this:
          > /usr/xpg4/bin/sed -n 's/data1/info/w file2' file1
          > Now file2 contained only those lines which contained info1(i.e which contained data1 in original file) but not the other contents of original file.
          > I would like to know the command-line for changing the contents of the file and retain the non-changed parts of the file.

          Any reason that this isn't a Perl question? Perl is what I would use for this, and if you're on a recent build of Solaris you should have Perl installed in /usr/bin.

          perl -pi.orig -e 's/data1/info1/g' file1

          This command will make a copy of file1, called file1.orig. It will then cycle through each line of file1, and run the substitution against it. The results of the substitution, whether there was a match or not, are written to file1.

          See "perldoc perlrun" for what the -p, -i, and -e options do.

          If this can't have a Perl solution, then there are people here who do mind. A lot. Just so you know.

          Hope this helps,

          Jeff Eggen
          IT Programmer Analyst
          Saskatchewan Government Insurance
          Ph (306) 751-1795
          email jeggen@...


          Yahoo! Groups Sponsor
          Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

          Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


          ---------------------------------
          Do you Yahoo!?
          Protect your identity with Yahoo! Mail AddressGuard

          [Non-text portions of this message have been removed]
        • Damien Carbery
          ... results in file1 only. ... in the script. ... I was had no problems using $ perl -pi -e s/fmt1/fmt2/g file1 # At command line or system( perl -pi -e
          Message 4 of 7 , Nov 5, 2003
          • 0 Attachment
            --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
            wrote:
            > Hi,
            > The command:
            > perl -pi -e 's/fmt1/fmt2/g' file1
            > will change all occurences of fmt1 with fmt2 and we get the
            results in file1 only.
            > But I am not able to use this command from a script..like this:
            >
            > system("perl -pi -e 's/fmt1/fmt2/g' file1");
            > or `perl -pi -e 's/fmt1/fmt2/g' file1 `
            >
            > Why is this happening..?
            > Please tell the modifications to the command(if any) to use it
            in the script.
            >
            > Thanks in advance.
            >
            > From,
            > Sreeram
            >
            > snip old stuff

            I was had no problems using
            $ perl -pi -e 's/fmt1/fmt2/g' file1 # At command line
            or
            system( "perl -pi -e 's/fmt1/fmt2/g' file1" );

            Are you getting any warnings?
            Are warnings on? Does your #! line have '-w' at the end.
          • Sreeram B S
            Hi friends, The script is not giving any errors or warnings. It is executing silently. But the relevant changes that had to be done in the file are not done!!!
            Message 5 of 7 , Nov 5, 2003
            • 0 Attachment
              Hi friends,
              The script is not giving any errors or warnings. It is executing silently. But the relevant changes that had to be done in the file are not done!!!
              What may be the reason for this?
              I have also put on, the warning flag ie. #!/usr/bin/perl -w

              Need your help.

              Thanks,
              Sreeram


              Damien Carbery <daymobrew@...> wrote:
              --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
              wrote:
              > Hi,
              > The command:
              > perl -pi -e 's/fmt1/fmt2/g' file1
              > will change all occurences of fmt1 with fmt2 and we get the
              results in file1 only.
              > But I am not able to use this command from a script..like this:
              >
              > system("perl -pi -e 's/fmt1/fmt2/g' file1");
              > or `perl -pi -e 's/fmt1/fmt2/g' file1 `
              >
              > Why is this happening..?
              > Please tell the modifications to the command(if any) to use it
              in the script.
              >
              > Thanks in advance.
              >
              > From,
              > Sreeram
              >
              > snip old stuff

              I was had no problems using
              $ perl -pi -e 's/fmt1/fmt2/g' file1 # At command line
              or
              system( "perl -pi -e 's/fmt1/fmt2/g' file1" );

              Are you getting any warnings?
              Are warnings on? Does your #! line have '-w' at the end.



              Yahoo! Groups SponsorADVERTISEMENT

              Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

              Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


              ---------------------------------
              Do you Yahoo!?
              Protect your identity with Yahoo! Mail AddressGuard

              [Non-text portions of this message have been removed]
            • Damien Carbery
              ... silently. But the relevant changes that had to be done in the file are not done!!! ... Change the system() line to : system( echo Before;perl -pi -e
              Message 6 of 7 , Nov 5, 2003
              • 0 Attachment
                --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
                wrote:
                > Hi friends,
                > The script is not giving any errors or warnings. It is executing
                silently. But the relevant changes that had to be done in the file are
                not done!!!
                > What may be the reason for this?
                > I have also put on, the warning flag ie. #!/usr/bin/perl -w
                >
                > Need your help.
                >
                > Thanks,
                > Sreeram
                >
                >
                > Damien Carbery <daymobrew@y...> wrote:
                > --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
                > wrote:
                > > Hi,
                > > The command:
                > > perl -pi -e 's/fmt1/fmt2/g' file1
                > > will change all occurences of fmt1 with fmt2 and we get the
                > results in file1 only.
                > > But I am not able to use this command from a script..like this:
                > >
                > > system("perl -pi -e 's/fmt1/fmt2/g' file1");
                > > or `perl -pi -e 's/fmt1/fmt2/g' file1 `
                > >
                > > Why is this happening..?
                > > Please tell the modifications to the command(if any) to use it
                > in the script.
                > >
                > > Thanks in advance.
                > >
                > > From,
                > > Sreeram
                > >
                > > snip old stuff
                >
                > I was had no problems using
                > $ perl -pi -e 's/fmt1/fmt2/g' file1 # At command line
                > or
                > system( "perl -pi -e 's/fmt1/fmt2/g' file1" );
                >
                > Are you getting any warnings?
                > Are warnings on? Does your #! line have '-w' at the end.
                >

                Change the system() line to :
                system( "echo Before;perl -pi -e 's/fmt1/fmt2/g' file1;echo After" );

                This will display "Before" and "After" in your console window to
                confirm the command was run. Even when running correctly it runs
                silently so there is no harm in generating some visual feedback.

                Even better, remove the -i switch (change '-pi' to '-p') so the
                modified lines are sent to STDOUT. You should see the modified lines.
              • Sreeram B S
                Thanks Damien.. Really it was an excellent solution. By using echo before and echo after and by removal of the -i option... I could make out that the way I
                Message 7 of 7 , Nov 6, 2003
                • 0 Attachment
                  Thanks Damien..
                  Really it was an excellent solution. By using 'echo before and echo after' and by removal of the -i option... I could make out that the way I was invoking the command itself was wrong. Now I have corrected it and it is working fine.

                  Thanks for your timely support.

                  Regards,
                  Sreeram


                  Damien Carbery <daymobrew@...> wrote:
                  --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
                  wrote:
                  > Hi friends,
                  > The script is not giving any errors or warnings. It is executing
                  silently. But the relevant changes that had to be done in the file are
                  not done!!!
                  > What may be the reason for this?
                  > I have also put on, the warning flag ie. #!/usr/bin/perl -w
                  >
                  > Need your help.
                  >
                  > Thanks,
                  > Sreeram
                  >
                  >
                  > Damien Carbery <daymobrew@y...> wrote:
                  > --- In perl-beginner@yahoogroups.com, Sreeram B S <sreeramabsc@y...>
                  > wrote:
                  > > Hi,
                  > > The command:
                  > > perl -pi -e 's/fmt1/fmt2/g' file1
                  > > will change all occurences of fmt1 with fmt2 and we get the
                  > results in file1 only.
                  > > But I am not able to use this command from a script..like this:
                  > >
                  > > system("perl -pi -e 's/fmt1/fmt2/g' file1");
                  > > or `perl -pi -e 's/fmt1/fmt2/g' file1 `
                  > >
                  > > Why is this happening..?
                  > > Please tell the modifications to the command(if any) to use it
                  > in the script.
                  > >
                  > > Thanks in advance.
                  > >
                  > > From,
                  > > Sreeram
                  > >
                  > > snip old stuff
                  >
                  > I was had no problems using
                  > $ perl -pi -e 's/fmt1/fmt2/g' file1 # At command line
                  > or
                  > system( "perl -pi -e 's/fmt1/fmt2/g' file1" );
                  >
                  > Are you getting any warnings?
                  > Are warnings on? Does your #! line have '-w' at the end.
                  >

                  Change the system() line to :
                  system( "echo Before;perl -pi -e 's/fmt1/fmt2/g' file1;echo After" );

                  This will display "Before" and "After" in your console window to
                  confirm the command was run. Even when running correctly it runs
                  silently so there is no harm in generating some visual feedback.

                  Even better, remove the -i switch (change '-pi' to '-p') so the
                  modified lines are sent to STDOUT. You should see the modified lines.



                  Yahoo! Groups SponsorADVERTISEMENT

                  Unsubscribing info is here: http://help.yahoo.com/help/us/groups/groups-32.html

                  Your use of Yahoo! Groups is subject to the Yahoo! Terms of Service.


                  ---------------------------------
                  Do you Yahoo!?
                  Protect your identity with Yahoo! Mail AddressGuard

                  [Non-text portions of this message have been removed]
                Your message has been successfully submitted and would be delivered to recipients shortly.