Delete a line from file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ajd335
    New Member
    • Apr 2008
    • 123

    Delete a line from file

    hey all,
    I want to remove the below line from the resulting text...
    Code:
    (.*)$/',$lines,$matches))10.(.*)$/',$lines,$matches))(.*)$/',$lines,$matches))
    i used
    Code:
    perl -pi -e 's/\(.*\)\$/',\$lines,\$matches\)\)10.\(.*\)\$/',\$lines,\$matches\)\)\(.*\)\$/',\$lines,\$matches\)\) / /gi' result.txt > result1.txt
    But dint work..
    Please help me..
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    Try this instead:

    Code:
    perl -pi -e 's#\Q(.*)\$/',\$lines,\$matches))10.(.*)\$/',\$lines,\$matches))(.*)\$/',\$lines,\$matches))# #gi' result.txt > result1.txt
    \Q will escape all the meta characters in the pattern except $ and @. So I escaped all the $ manually.

    Comment

    • ajd335
      New Member
      • Apr 2008
      • 123

      #3
      Originally posted by KevinADC
      Try this instead:

      Code:
      perl -pi -e 's#\Q(.*)\$/',\$lines,\$matches))10.(.*)\$/',\$lines,\$matches))(.*)\$/',\$lines,\$matches))# #gi' result.txt > result1.txt
      \Q will escape all the meta characters in the pattern except $ and @. So I escaped all the $ manually.
      Hi Kevin,
      when I tried above , It gives me
      " Unmatched '. "
      Why its giving that?

      Comment

      • KevinADC
        Recognized Expert Specialist
        • Jan 2007
        • 4092

        #4
        oops, try like this instead:

        Code:
        perl -pi -e 's#\Q(.*)\$/\',\$lines,\$matches))10.(.*)\$/\',\$lines,\$matches))(.*)\$/\',\$lines,\$matches))# #gi' result.txt > result1.txt
        the single quotes inside the pattern have to be escaped.

        Comment

        • alampally
          New Member
          • Jul 2008
          • 5

          #5
          Code:
          ######################################
          # subroutine delete file
          ######################################
          
          sub Delete
          {
          
          $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
          
          open OUT, "$out" or die "Cannot open $out for write :$!";
          
          read(OUT,$oldstuff,10000);
          
          close OUT;
          
          @changes=split(/n/,$oldstuff);
          
          open OUT, ">$out" or die "Cannot open $out for write :$!";
          
          undef $changes[$fields{linenumber}]; ### this leaves a blank line which messes up my output ###
          
          
          for $x (0 .. $#changes) 
          
          {
          print OUT "$changes[$x]n";
          }
          
          close OUT;
          
          }
          
          ######################################
          # subroutine adjust file
          ######################################
          
          sub Adjust
          {
          
          $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
          
          open OUT, "$out" or die "Cannot open $out for write :$!";
          
          read(OUT,$oldstuff,10000);
          
          close OUT;
          
          @changes=split(/n/,$oldstuff);
          
          open OUT, ">$out" or die "Cannot open $out for write :$!";
          
          $changes[$fields{linenumber}]=$adjust;
          
          for $x (0 .. $#changes) 
          
          {
          print OUT "$changes[$x]n";
          }
          
          close OUT;
          
          }
          Write me for any help in C,C++,Perl,PHP and Unix related stuff. I will answer you with in the time with free of cost.

          Rammohan Alampally,
          HP Technologies
          Bangalore
          EMAIL AND URL REMOVED! AGAINST SITE POSTING POLICY

          Comment

          • numberwhun
            Recognized Expert Moderator Specialist
            • May 2007
            • 3467

            #6
            alampally,

            Before you post to any other forums here on bytes.com, please read our Posting Guidelines. You seem to consistently break them and need to please read and understand them.

            Regards,

            Jeff

            Comment

            • ajd335
              New Member
              • Apr 2008
              • 123

              #7
              Originally posted by KevinADC
              oops, try like this instead:

              Code:
              perl -pi -e 's#\Q(.*)\$/\',\$lines,\$matches))10.(.*)\$/\',\$lines,\$matches))(.*)\$/\',\$lines,\$matches))# #gi' result.txt > result1.txt
              the single quotes inside the pattern have to be escaped.
              Hi Kevin,
              Thanks for the help..
              Kevin again i get the same error ,
              ( Unmatched ' . )
              my line is
              Code:
              (.*)$/',$lines,$matches)        10.(.*)$/',$lines,$matches)     (.*)$/',$lines,$matches)
              There is a Tab between .
              What can be done ?
              Thanks...
              Last edited by numberwhun; Jul 16 '08, 03:57 PM. Reason: Please use code tags

              Comment

              • numberwhun
                Recognized Expert Moderator Specialist
                • May 2007
                • 3467

                #8
                As mentioned earlier to another OP in this post, code tags are required. You used them in your first post, please use them to surround any code you provide.

                Thank you!

                Regards,

                Moderator

                Comment

                • ajd335
                  New Member
                  • Apr 2008
                  • 123

                  #9
                  Originally posted by numberwhun
                  As mentioned earlier to another OP in this post, code tags are required. You used them in your first post, please use them to surround any code you provide.

                  Thank you!

                  Regards,

                  Moderator
                  Oh Sorry,
                  I'll take care of that..

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    Originally posted by alampally
                    Code:
                    ######################################
                    # subroutine delete file
                    ######################################
                    
                    sub Delete
                    {
                    
                    $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
                    
                    open OUT, "$out" or die "Cannot open $out for write :$!";
                    
                    read(OUT,$oldstuff,10000);
                    
                    close OUT;
                    
                    @changes=split(/n/,$oldstuff);
                    
                    open OUT, ">$out" or die "Cannot open $out for write :$!";
                    
                    undef $changes[$fields{linenumber}]; ### this leaves a blank line which messes up my output ###
                    
                    
                    for $x (0 .. $#changes) 
                    
                    {
                    print OUT "$changes[$x]n";
                    }
                    
                    close OUT;
                    
                    }
                    
                    ######################################
                    # subroutine adjust file
                    ######################################
                    
                    sub Adjust
                    {
                    
                    $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
                    
                    open OUT, "$out" or die "Cannot open $out for write :$!";
                    
                    read(OUT,$oldstuff,10000);
                    
                    close OUT;
                    
                    @changes=split(/n/,$oldstuff);
                    
                    open OUT, ">$out" or die "Cannot open $out for write :$!";
                    
                    $changes[$fields{linenumber}]=$adjust;
                    
                    for $x (0 .. $#changes) 
                    
                    {
                    print OUT "$changes[$x]n";
                    }
                    
                    close OUT;
                    
                    }
                    Write me for any help in C,C++,Perl,PHP and Unix related stuff. I will answer you with in the time with free of cost.

                    Rammohan Alampally,
                    HP Technologies
                    Bangalore
                    EMAIL AND URL REMOVED! AGAINST SITE POSTING POLICY

                    I was very pleased to see a new person posting in the perl forum but it has become evident that your real purpose is to post your advertising. You are offering suggestions that sometimes do not even really apply to the question, like your above code. Your advertising could be tolerated, at least by me personally, if your suggestions were better thought out and relevant to the question and not posted in old threads like you did previously (not this thread). I hope you continue to post but I also hope you offer better replies and stop violating the forum policy of posting signatures with email addresses and advertising.

                    Regards,
                    Kevin

                    Comment

                    • KevinADC
                      Recognized Expert Specialist
                      • Jan 2007
                      • 4092

                      #11
                      Originally posted by ajd335
                      Hi Kevin,
                      Thanks for the help..
                      Kevin again i get the same error ,
                      ( Unmatched ' . )
                      my line is
                      Code:
                      (.*)$/',$lines,$matches)        10.(.*)$/',$lines,$matches)     (.*)$/',$lines,$matches)
                      There is a Tab between .
                      What can be done ?
                      Thanks...
                      Is that literally the line in the file? Or are those perl variables ($lines,$matche s). I'm getting confused.

                      Comment

                      • numberwhun
                        Recognized Expert Moderator Specialist
                        • May 2007
                        • 3467

                        #12
                        Originally posted by KevinADC
                        I was very pleased to see a new person posting in the perl forum but it has become evident that your real purpose is to post your advertising. You are offering suggestions that sometimes do not even really apply to the question, like your above code. Your advertising could be tolerated, at least by me personally, if your suggestions were better thought out and relevant to the question and not posted in old threads like you did previously (not this thread). I hope you continue to post but I also hope you offer better replies and stop violating the forum policy of posting signatures with email addresses and advertising.

                        Regards,
                        Kevin
                        I fully agree, it is nice to see, it means we are (hopefully) doing something right. Considering that they joined this morning and did all their posting then, I hope that, as you said, they will continue, but modify their posting practices. As you saw, I posted a link to the policy in hopes that they would read and adhere to it.

                        Regards,

                        Jeff

                        Comment

                        • ajd335
                          New Member
                          • Apr 2008
                          • 123

                          #13
                          Originally posted by KevinADC
                          Is that literally the line in the file? Or are those perl variables ($lines,$matche s). I'm getting confused.
                          Hi Kevin ,
                          Yes , they are the lines only .Actually they are the result of preg_match(PHP) and have stored that in output file(i.e result.txt), which includes that line( It's bcz the result from preg_match).

                          Thanks,

                          Comment

                          • KevinADC
                            Recognized Expert Specialist
                            • Jan 2007
                            • 4092

                            #14
                            Originally posted by ajd335
                            Hi Kevin ,
                            Yes , they are the lines only .Actually they are the result of preg_match(PHP) and have stored that in output file(i.e result.txt), which includes that line( It's bcz the result from preg_match).

                            Thanks,
                            hmmm.... I'm stumped. To me it looks like it should work but evidently I am wrong or your code is different than what I have suggested you use. Try asking on www.perlmonks.c om and see if one the members there can offer some suggestions. I know you could rewrite it as a script instead of a oneliner but it would be interesting to know why the oneliner is not working.

                            Comment

                            • ajd335
                              New Member
                              • Apr 2008
                              • 123

                              #15
                              Originally posted by KevinADC
                              hmmm.... I'm stumped. To me it looks like it should work but evidently I am wrong or your code is different than what I have suggested you use. Try asking on www.perlmonks.c om and see if one the members there can offer some suggestions. I know you could rewrite it as a script instead of a oneliner but it would be interesting to know why the oneliner is not working.
                              Thanks Kevin,
                              I have asked that q in perl monks...Lets c..
                              Thanks for the help...

                              Comment

                              Working...