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

Help on Edit a line in a File

rajiv07
100+
P: 141
Hi to all,

I want to know how do i do edit or delete a line from file.The line can be any where in the file.

Is any Idea Please.

Thanks.

Regards
Rajiv
Dec 31 '07 #1
Share this Question
Share on Google+
16 Replies


mehj123
P: 55
Hi Rajiv,

You have to provide more details about what you are trying to do.. But I guess you should go for pattern matching with regular expressions.

Mehjabeen
Dec 31 '07 #2

rajiv07
100+
P: 141
Hi Rajiv,

You have to provide more details about what you are trying to do.. But I guess you should go for pattern matching with regular expressions.

Mehjabeen

Thank U mehj123

Let me explain.I have a file has set of name and age.So i have an input name 'Rajiv' if it is match in any line in the file, i want to edit that line to some other details.

But i have solved this problem using Tie::File Module.

Expand|Select|Wrap|Line Numbers
  1. my $cName="Rajiv";
  2.  
  3. use Tie::File;
  4.  
  5. my $cFile="sms.dat";
  6.  
  7. tie my @array,'Tie::File',$cFile or die "cannot tie file";
  8.  
  9. for $i(0..$#array){
  10.  
  11. my @split=split(/\#/,$array[$i]);
  12.  
  13. if($split[1] eq $cName){
  14.  
  15. $array[$i]="$split[0]#$split[1]#$split[2]#$split[3]#$split[4]#UPLOAD";
  16. }
  17.  
  18. }
  19.  
  20. untie @array;
Now, Is it efficient way to do this task or please Let me know any other easy and efficient method if u have.

Thank u.

Regards
RajivGandhi.
Dec 31 '07 #3

KevinADC
Expert 2.5K+
P: 4,059
Until you start replying to all your threads I personally am not going to help you anymore. That is my own personal policy, not the policy of this website.
http://www.thescripts.com/forum/thread752418.html (unreplied by you)
http://www.thescripts.com/forum/thread750611.html (abandoned by you)
http://www.thescripts.com/forum/thread749769.html (unreplied and abondoned by you)
http://www.thescripts.com/forum/thread746910.html (abandoned by you)
http://www.thescripts.com/forum/thread746423.html (unreplied and abandoned)

the list goes on.
Dec 31 '07 #4

numberwhun
Expert Mod 2.5K+
P: 3,503
Until you start replying to all your threads I personally am not going to help you anymore. That is my own personal policy, not the policy of this website.
http://www.thescripts.com/forum/thread752418.html (unreplied by you)
http://www.thescripts.com/forum/thread750611.html (abandoned by you)
http://www.thescripts.com/forum/thread749769.html (unreplied and abondoned by you)
http://www.thescripts.com/forum/thread746910.html (abandoned by you)
http://www.thescripts.com/forum/thread746423.html (unreplied and abandoned)

the list goes on.
I have to fully agree with Kevin on this. While it is not against the site policy (at this time), the posting of threads and then ignoring/abandoning them without acknowledging the answer or posting another of the same question is just unacceptable but myself and many others here. It is rude and inconsiderate.

All of the people here (experts, moderators and admins alike) are all volunteers. We have day jobs, but we volunteer our time to this forum to make it what it is and will continue to be as well as answer all of the questions posted by OPs such as yourself. Please keep that in mind should you decide to just abandon a thread again. You need to change your ways and start keeping up on your posted threads. This is your first, and only, warning!

Regards,

Jeff
Jan 1 '08 #5

Kelicula
Expert 100+
P: 176
I have to fully agree with Kevin on this. While it is not against the site policy (at this time), the posting of threads and then ignoring/abandoning them without acknowledging the answer or posting another of the same question is just unacceptable but myself and many others here. It is rude and inconsiderate.

All of the people here (experts, moderators and admins alike) are all volunteers. We have day jobs, but we volunteer our time to this forum to make it what it is and will continue to be as well as answer all of the questions posted by OPs such as yourself. Please keep that in mind should you decide to just abandon a thread again. You need to change your ways and start keeping up on your posted threads. This is your first, and only, warning!

Regards,

Jeff
Ditto.
It's pretty bad when people spend more time on your post than YOU do.
There are quite a few things I would point out about your code.
But will you ever read it?
Why would I bother combing through all that to help you, if you don't even read the replies?


Not trying to be mean, but I agree with these guys.
Try to see others point of view.
It's a lesson I benefited from, and you can too.
Jan 1 '08 #6

rajiv07
100+
P: 141
I am really sorry to my all Masters.Let me clear my position,actually i was getting
solution from your replies that is why i am not reply to that posts.I am not abandon or unread your replies.Thats my mistake so please give me a chance to correct myself.Hereafter i wont repeat this mistake again.

Thank You

Regards
RajivGandhi
Jan 2 '08 #7

eWish
Expert 100+
P: 971
We were unaware of the fact you were benefiting from our replies. From our vantage point it appears as though you were not returning. Therefore it does appear that the posts were abandoned.

Thank you for your replies.

--Kevin
Jan 2 '08 #8

Kelicula
Expert 100+
P: 176
Now that your back.

You will benefit later when you go back to look at your code if you put all the other packages and pragmas you are using at the top of your code.

use strict;
use Tie::File;
etc...

Next you can benefit by seeing why something didn't work, so in your die statement include this variable "$!".

Like so:
Expand|Select|Wrap|Line Numbers
  1.  
  2. tie my @array, 'Tie::File', $cFile or die "cannot tie file: $!";
  3.  
Also while it will work, you don't need to create your loop, the way you did. Perl is smart enough to know how long the array is and when to stop iterating.

Expand|Select|Wrap|Line Numbers
  1. for my $i (0..$#array){
  2.  
  3. # Can be replaced with...
  4.  
  5. for my $i (@array){
  6.  
  7. # or even...
  8.  
  9. for(@array){
  10.  
  11. # then use $_ as the current iterator, (this will be used by default anyway)
  12.  
Finally...

You shouldn't waste time splitting the line until you know there is a match.
Perl will scan the line and return true if there is a match anywhere in the line, unless you specify otherwise.
Actually you aren't using perl's regular expressions at all.

Try this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. if($_ =~ /split[1]/) # You won't be using split actually...but I left it for understanding.
  3.  
  4.  
Putting all this together your code can be simplified thus:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4. use Tie::File;
  5.  
  6. my $cName = 'Rajiv';
  7. my $cFile = 'sms.dat';
  8.  
  9. tie my @array, 'Tie::File', $cFile or die "Can't tie file: $!";
  10.  
  11. for(@array){
  12.  
  13. if(/$cName/){
  14.  
  15. @result = split(/#/);
  16. push(@result, 'UPLOAD');
  17. }
  18. }
  19.  
  20. untie @array;
  21.  
  22.  
Not really sure you are doing with the last line, so I just added the string UPLOAD as the last element of @result array.

Hope it helps!!

Let me know?
Jan 3 '08 #9

rajiv07
100+
P: 141
Thank You Kelicula

Its working fine.But i want to know is any other better method to do this file edit
task without using any module.If so Let me know please.

Thank u

Regards
RajivGandhi.
Jan 3 '08 #10

KevinADC
Expert 2.5K+
P: 4,059
Thank You Kelicula

Its working fine.But i want to know is any other better method to do this file edit
task without using any module.If so Let me know please.

Thank u

Regards
RajivGandhi.
Use perls in place editor. You run it from the command line using the -i switch or use it in a script with the $^I variable.
Jan 3 '08 #11

rajiv07
100+
P: 141
Use perls in place editor. You run it from the command line using the -i switch or use it in a script with the $^I variable.
Thank You KevinADC

I am new for $^I special variable.But let me try and i will ask You if i cannot able to do.

Thank u.
Regards
RajivGandhi
Jan 3 '08 #12

numberwhun
Expert Mod 2.5K+
P: 3,503
Thank You KevinADC

I am new for $^I special variable.But let me try and i will ask You if i cannot able to do.

Thank u.
Regards
RajivGandhi
You can find reference to the special variables here.

Regards,

Jeff
Jan 3 '08 #13

rajiv07
100+
P: 141
You can find reference to the special variables here.

Regards,

Jeff
Thank U numberwhun

It is very useful for me.

Regards
RajivGandhi
Jan 3 '08 #14

rajiv07
100+
P: 141
Expand|Select|Wrap|Line Numbers
  1. my $file = 'c:/perl/sms.dat';
  2.  
  3.      $num="9484857575";
  4.  
  5.    local @ARGV = ($file);
  6.     local $^I = '.bak';
  7.  
  8.     while(<>)
  9.     {
  10.             my @array = split(/\#/);
  11.             if($num eq $array[1]){
  12.                 chomp($array[4]);
  13.        print "$array[0]#$array[1]#$array[2]#$array[3]#$array[4]#UPDATE\n";
  14.  
  15.             }    
  16.             else {
  17.                print;
  18.             }
  19.     }
  20.  
  21.     }

This is the Solution for edit a file without using any module .But i cannot get the Magic in this code.What is happening in the $^I and @ARGV.
Please Explain Me.

Thanks

Regards
Rajivgandhi
Jan 3 '08 #15

KevinADC
Expert 2.5K+
P: 4,059
Look through/read this page:

http://perldoc.perl.org/perlvar.html

Bookmark the perldoc website if you will continue to use and learn perl, there is a lot of information on that website. Especially the Language Reference section which the above link is a part of.
Jan 3 '08 #16

rajiv07
100+
P: 141
Look through/read this page:

http://perldoc.perl.org/perlvar.html

Bookmark the perldoc website if you will continue to use and learn perl, there is a lot of information on that website. Especially the Language Reference section which the above link is a part of.
Thank U KevinADC

Its very useful for me.

Regards.
RajivGandhi
Jan 4 '08 #17

Post your reply

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