By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,373 Members | 1,984 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,373 IT Pros & Developers. It's quick & easy.

Delete a line from file

100+
P: 123
hey all,
I want to remove the below line from the resulting text...
Expand|Select|Wrap|Line Numbers
  1. (.*)$/',$lines,$matches))10.(.*)$/',$lines,$matches))(.*)$/',$lines,$matches))
  2.  
i used
Expand|Select|Wrap|Line Numbers
  1. perl -pi -e 's/\(.*\)\$/',\$lines,\$matches\)\)10.\(.*\)\$/',\$lines,\$matches\)\)\(.*\)\$/',\$lines,\$matches\)\) / /gi' result.txt > result1.txt
  2.  
But dint work..
Please help me..
Jul 15 '08 #1
Share this Question
Share on Google+
21 Replies


KevinADC
Expert 2.5K+
P: 4,059
Try this instead:

Expand|Select|Wrap|Line Numbers
  1. perl -pi -e 's#\Q(.*)\$/',\$lines,\$matches))10.(.*)\$/',\$lines,\$matches))(.*)\$/',\$lines,\$matches))# #gi' result.txt > result1.txt
  2.  
\Q will escape all the meta characters in the pattern except $ and @. So I escaped all the $ manually.
Jul 15 '08 #2

100+
P: 123
Try this instead:

Expand|Select|Wrap|Line Numbers
  1. perl -pi -e 's#\Q(.*)\$/',\$lines,\$matches))10.(.*)\$/',\$lines,\$matches))(.*)\$/',\$lines,\$matches))# #gi' result.txt > result1.txt
  2.  
\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?
Jul 15 '08 #3

KevinADC
Expert 2.5K+
P: 4,059
oops, try like this instead:

Expand|Select|Wrap|Line Numbers
  1. 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.
Jul 16 '08 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1. ######################################
  2. # subroutine delete file
  3. ######################################
  4.  
  5. sub Delete
  6. {
  7.  
  8. $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
  9.  
  10. open OUT, "$out" or die "Cannot open $out for write :$!";
  11.  
  12. read(OUT,$oldstuff,10000);
  13.  
  14. close OUT;
  15.  
  16. @changes=split(/n/,$oldstuff);
  17.  
  18. open OUT, ">$out" or die "Cannot open $out for write :$!";
  19.  
  20. undef $changes[$fields{linenumber}]; ### this leaves a blank line which messes up my output ###
  21.  
  22.  
  23. for $x (0 .. $#changes) 
  24.  
  25. {
  26. print OUT "$changes[$x]n";
  27. }
  28.  
  29. close OUT;
  30.  
  31. }
  32.  
  33. ######################################
  34. # subroutine adjust file
  35. ######################################
  36.  
  37. sub Adjust
  38. {
  39.  
  40. $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
  41.  
  42. open OUT, "$out" or die "Cannot open $out for write :$!";
  43.  
  44. read(OUT,$oldstuff,10000);
  45.  
  46. close OUT;
  47.  
  48. @changes=split(/n/,$oldstuff);
  49.  
  50. open OUT, ">$out" or die "Cannot open $out for write :$!";
  51.  
  52. $changes[$fields{linenumber}]=$adjust;
  53.  
  54. for $x (0 .. $#changes) 
  55.  
  56. {
  57. print OUT "$changes[$x]n";
  58. }
  59.  
  60. close OUT;
  61.  
  62. }
  63.  
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
Jul 16 '08 #5

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 16 '08 #6

100+
P: 123
oops, try like this instead:

Expand|Select|Wrap|Line Numbers
  1. 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
Expand|Select|Wrap|Line Numbers
  1. (.*)$/',$lines,$matches)        10.(.*)$/',$lines,$matches)     (.*)$/',$lines,$matches)
  2.  
There is a Tab between .
What can be done ?
Thanks...
Jul 16 '08 #7

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 16 '08 #8

100+
P: 123
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..
Jul 16 '08 #9

KevinADC
Expert 2.5K+
P: 4,059
Expand|Select|Wrap|Line Numbers
  1. ######################################
  2. # subroutine delete file
  3. ######################################
  4.  
  5. sub Delete
  6. {
  7.  
  8. $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
  9.  
  10. open OUT, "$out" or die "Cannot open $out for write :$!";
  11.  
  12. read(OUT,$oldstuff,10000);
  13.  
  14. close OUT;
  15.  
  16. @changes=split(/n/,$oldstuff);
  17.  
  18. open OUT, ">$out" or die "Cannot open $out for write :$!";
  19.  
  20. undef $changes[$fields{linenumber}]; ### this leaves a blank line which messes up my output ###
  21.  
  22.  
  23. for $x (0 .. $#changes) 
  24.  
  25. {
  26. print OUT "$changes[$x]n";
  27. }
  28.  
  29. close OUT;
  30.  
  31. }
  32.  
  33. ######################################
  34. # subroutine adjust file
  35. ######################################
  36.  
  37. sub Adjust
  38. {
  39.  
  40. $out="cgi-bin/order/$ENV{'REMOTE_ADDR'}.txt";
  41.  
  42. open OUT, "$out" or die "Cannot open $out for write :$!";
  43.  
  44. read(OUT,$oldstuff,10000);
  45.  
  46. close OUT;
  47.  
  48. @changes=split(/n/,$oldstuff);
  49.  
  50. open OUT, ">$out" or die "Cannot open $out for write :$!";
  51.  
  52. $changes[$fields{linenumber}]=$adjust;
  53.  
  54. for $x (0 .. $#changes) 
  55.  
  56. {
  57. print OUT "$changes[$x]n";
  58. }
  59.  
  60. close OUT;
  61.  
  62. }
  63.  
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
Jul 16 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
Hi Kevin,
Thanks for the help..
Kevin again i get the same error ,
( Unmatched ' . )
my line is
Expand|Select|Wrap|Line Numbers
  1. (.*)$/',$lines,$matches)        10.(.*)$/',$lines,$matches)     (.*)$/',$lines,$matches)
  2.  
There is a Tab between .
What can be done ?
Thanks...
Is that literally the line in the file? Or are those perl variables ($lines,$matches). I'm getting confused.
Jul 16 '08 #11

numberwhun
Expert Mod 2.5K+
P: 3,503
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
Jul 16 '08 #12

100+
P: 123
Is that literally the line in the file? Or are those perl variables ($lines,$matches). 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,
Jul 16 '08 #13

KevinADC
Expert 2.5K+
P: 4,059
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.com 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.
Jul 16 '08 #14

100+
P: 123
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.com 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...
Jul 16 '08 #15

100+
P: 123
Thanks Kevin,
I have asked that q in perl monks...Lets c..
Thanks for the help...
Hey Kevin,
They Asked me to use shell_escape , Just to know well , is it correct way to use shell_escape ?? as below.
Expand|Select|Wrap|Line Numbers
  1.  perl -pi -e shell_escape('s#\Q(.*)\$/\',\$lines,\$matches))10.(.*)\$/\',\$lines,\$matches))(.*)\$/\',\$lines,\$matches))# #gi')  result.txt > result1.txt
  2.  
  3.  
Thanks ,
Jul 16 '08 #16

KevinADC
Expert 2.5K+
P: 4,059
There is no perl function shell_escape that I know of so maybe it is a function of a perl module, but I have not heard of it. You will have to ask the monks how to use that function.

I don't see the question posted on www.perlmonks.com
Jul 16 '08 #17

100+
P: 123
There is no perl function shell_escape that I know of so maybe it is a function of a perl module, but I have not heard of it. You will have to ask the monks how to use that function.

I don't see the question posted on www.perlmonks.com
Thanks Kevin,
Here's the link on perlmonks for my post..
http://perlmonks.org/index.pl?node_id=698176
Jul 16 '08 #18

numberwhun
Expert Mod 2.5K+
P: 3,503
By shell-escape, they are meaning the back slash character.

If you keep reading the replies to your post on Perlmonks, Netwallah has a good trick that may work: ' " ' " '


Regards,

Jeff
Jul 17 '08 #19

100+
P: 123
By shell-escape, they are meaning the back slash character.

If you keep reading the replies to your post on Perlmonks, Netwallah has a good trick that may work: ' " ' " '


Regards,

Jeff
Hi NumberWhun,
Yes , you are right.they give good tricks to look for....I got the problem solved by changing some script in PHP only .But still learn so much from th help n replies..
Thanks.
Jul 17 '08 #20

KevinADC
Expert 2.5K+
P: 4,059
You could avoid the whole issue by running a perl script instead of a oneliner that has to run in the shell and is subject to the whims of the shell. A little more code but much easier to write since there are no odd-ball shell considerations to take into account.
Jul 17 '08 #21

100+
P: 123
You could avoid the whole issue by running a perl script instead of a oneliner that has to run in the shell and is subject to the whims of the shell. A little more code but much easier to write since there are no odd-ball shell considerations to take into account.
Hi Kevin
May be yes , but still the desired script /code makes me do so...But , ya we can always avoid tedious code.
Thanks.
Jul 17 '08 #22

Post your reply

Sign in to post your reply or Sign up for a free account.