editing text file in perl

I want to edit a perticular line in a 50 lines text file using perl.

File handle can be used for this.
But how can i edit  a string present in say 20th line  using perl
script.

0
7/3/2006 7:10:53 AM
comp.lang.perl.misc 33177 articles. 1 followers. brian (1246) is leader. Post Follow

5 Replies
694 Views

Similar Articles

[PageSpeed] 36

king wrote:
> I want to edit a perticular line in a 50 lines text file using perl.

That's a FAQ.

     perldoc -q "change one line"

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
Gunnar
7/3/2006 7:26:16 AM
king wrote:

> I want to edit a perticular line in a 50 lines text file using perl.
>
> File handle can be used for this.
> But how can i edit  a string present in say 20th line  using perl
> script.

  #!perl
  use strict;
  use warnings;
  my $file         = 'file.dat';
  my $line_counter = 0;
  my $new_content;

  open my $F, '<', $file || die "Cant open $file: $!";
  flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
  while(<$F>)  {
    $line_counter ++;
    if ($line_counter == 20)  {
      # do something on 20th line, eg. change it
      $_ = "this is new content of line 20\n";
    }
    $new_content.= $_;
  }
  close $F || die "Cant close $file: $!";
  
  # report
  print $new_content;

Hope this helps,

-- 
 Bart

0
Bart
7/3/2006 7:27:07 AM
Bart Van der Donck wrote:
> king wrote:
> 
> 
>>I want to edit a perticular line in a 50 lines text file using perl.
>>
>>File handle can be used for this.
>>But how can i edit  a string present in say 20th line  using perl
>>script.
> 
> 
>   #!perl
>   use strict;
>   use warnings;
>   my $file         = 'file.dat';
>   my $line_counter = 0;
>   my $new_content;
> 
>   open my $F, '<', $file || die "Cant open $file: $!";
>   flock($F, 1) || die "Cant get LOCK_SH on $file: $!";
>   while(<$F>)  {
>     $line_counter ++;
>     if ($line_counter == 20)  {
>       # do something on 20th line, eg. change it
>       $_ = "this is new content of line 20\n";
>     }
>     $new_content.= $_;
>   }
>   close $F || die "Cant close $file: $!";
>   
>   # report
>   print $new_content;
> 

I prefer to keep simple jobs simple :-)
  perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat
or
  perl -pi -e '($. == 20) && s/^.*$/new content for line 20/;' file.dat
0
Ian
7/3/2006 9:44:12 AM
Ian Wilson wrote:
> I prefer to keep simple jobs simple :-)
>  perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat
> or
>  perl -pi -e '($. == 20) && s/^.*$/new content for line 20/;' file.dat

'short' ne 'simple'

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
0
Gunnar
7/3/2006 11:19:30 AM
Gunnar Hjalmarsson <noreply@gunnar.cc> wrote:
> Ian Wilson wrote:
>> I prefer to keep simple jobs simple :-)
>>  perl -pi -e 'if ($.==20) {s/^.*$/new content for line 20/;}' file.dat
>> or
>>  perl -pi -e '($. == 20) && s/^.*$/new content for line 20/;' file.dat
> 
> 'short' ne 'simple'


And attempting to re-answer a Frequently Asked Question is neither.


-- 
    Tad McClellan                          SGML consulting
    tadmc@augustmail.com                   Perl programming
    Fort Worth, Texas
0
Tad
7/3/2006 1:53:12 PM
Reply:

Similar Artilces:

deisng perl perl programmibng langs in perl
then liberate big war zones with notes on their download pages. -r www.thevoid1.net/ ...

Perl send args to other perl file.
Hello all, Can i send vars from file1.pl to file2.pl and display or read the sended vars in file2.pl? The goal is to communicate between 2 perl appl. wich are converterd to .exe files. This is what I have already: File1 ----------------------------- #!/usr/local/bin/perl @hello=("test"); @args=("File2.pl", "@hello"); system(@args)==0 or die "system @args failed:$?"; File2 ----------------------------- #!/usr/local/bin/perl if(@hello){ print "Value: @hello\n"; } else { print "Nothing received\n";} When executing File1.pl I get the r...

US-CA: Beverly Hills-Perl Developer - Perl Programmer - Perl Engineer
************************************************************** JobCircle.com is the largest regional job board in the Mid-Atlantic region, with tens of thousands of job offerings in PA, NJ, DE, MD, NY, and Washington D.C. To learn more, visit http://www.jobcircle.com?source=ng ************************************************************** Job Title: Perl Developer - Perl Programmer - Perl Engineer - Perl... Job Location: CA: Beverly Hills Pay Rate: competitive Job Length: full time Start Date: 2010-11-19 Company Name: Cybercoders Contact: Shea Stone ...

FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl?
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.37: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms ar...

editing pdf files with perl
Hi, I have a collection of PDF files which are stored in a directory. These PDF files follow a particular format. Each one of these files has some fields in the first page which are left blank. I need to fill those fields based on the input given by the user(s). I CANNOT overwrite these PDF files since the data contained in them is not elsewhere. I simply need to be able to edit and update the first page of these PDF files. Is there any way I can do this with Perl? Thanks in advance :) Aditya <aditya2507@gmail.com> wrote in message news:1108405623.370567.287490@o13g2000cwo.google...

Compiling perl? perl??
I suspect this has been discussed in the past before, and so I searched around the web and virtually everything I found out there says perlcc is buggy and not to use it, especially in production, so my question is are there any reliable alternatives? Or is there some magic incantation to get perlcc to build without segment faulting as that's what it's doing to me? The problem I'm interesting is solving is getting something to run on a system that doesn't have perl installed. I know there are also those who want to compile as a way of hiding source, but I don't car...

FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl?
This message is one of several periodic postings to comp.lang.perl.misc intended to make it easier for perl programmers to find answers to common questions. The core of this message represents an excerpt from the documentation provided with Perl. -------------------------------------------------------------------- 5.36: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in the "Far More Than You Ever Wanted To Know" collec...

FAQ 5.35 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl?
This message is one of several periodic postings to comp.lang.perl.misc intended to make it easier for perl programmers to find answers to common questions. The core of this message represents an excerpt from the documentation provided with Perl. -------------------------------------------------------------------- 5.35: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in the "Far More Than You Ever Wanted To Know" collec...

how to cut a text file in perl
hi i have a log file that it is increasing to big, how can i keep this file of a certain size, in other words, how can i make sure that the file is only 200 lines long and delete the first lines if longer. i think it would be easier to write this in a shell script. but i have no idea in perl. can anyone tell me how to print the last 200 lines of a file and overwrite the ssame file. thanks. Joseph wrote: > hi i have a log file that it is increasing to big, how can i keep this > file of > a certain size, in other words, how can i make sure that the file is > only 200 lines long an...

Editing PDF files with perl
I have a number of PDF files. I would like, for all of them, to remove the "page before last". In addition, they contain a watermark that I want to remove. These files are not copyrighted since they contain the works of the US government. So please, no self righteous copyright comments. There seems to be a multitude of PDF perl modules. Is there any one specifically suited for editing pdf files. i Ignoramus3188 <ignoramus3188@NOSPAM.3188.invalid> wrote: > I have a number of PDF files. > > I would like, for all of them, to remove the "page ...

Perl vs. perl
this is for T. do you know the difference between Perl (the language) and perl (the program)? let's hear you explain the difference. uri On 08/13/2015 08:07 PM, Uri Guttman wrote: > > this is for T. do you know the difference between Perl (the language) > and perl (the program)? let's hear you explain the difference. > > uri > Hi Uri, Homework! Homework! Why! Why! Why! K-I-D-S T-H-E-S-E D-A-Y-S! Wait! This is a test! Test! Test! I didn't know there was going to be a test! I over slept! My dog ate my paper! I went to Publik ...

FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #6
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.37: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #2
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.37: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #5
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.37: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #3
This message is one of several periodic postings to comp.lang.perl.misc intended to make it easier for perl programmers to find answers to common questions. The core of this message represents an excerpt from the documentation provided with Perl. -------------------------------------------------------------------- 5.36: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in the "Far More Than You Ever Wanted To Know" collec...

FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #15
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.36: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #4
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.37: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.37 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #7
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.37: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #4
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.38: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? 475604
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.38: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms ar...

FAQ 5.38 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #3
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.38: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #11
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.36: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #9
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.36: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

FAQ 5.36 Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? #10
This is an excerpt from the latest version perlfaq5.pod, which comes with the standard Perl distribution. These postings aim to reduce the number of repeated questions as well as allow the community to review and update the answers. The latest version of the complete perlfaq is at http://faq.perl.org . -------------------------------------------------------------------- 5.36: Why does Perl let me delete read-only files? Why does "-i" clobber protected files? Isn't this a bug in Perl? This is elaborately and painstakingly described in the file-dir-perms article in th...

Web resources about - editing text file in perl - comp.lang.perl.misc

Editing - Wikipedia, the free encyclopedia
Editing is the process of selecting and preparing written , visual , audible and film media used to convey information. The editing process can ...

Editing - Wikipedia, the free encyclopedia
Editing is the process of selecting and preparing written , visual , audible and film media used to convey information. The editing process can ...

Google Chrome Plugin Photon Brings Instagram-Style Photo Editing To Facebook
A new plugin for Google Chrome brings Instagram -style photo-editing capabilities to Facebook, and Photon is available free-of-charge in the ...

Facebook Ads Manager Now With Ad Editing, HTML Reports
... advertisers to choose the specific start and end times for campaigns. Now, Facebook has launched two more new Facebook Ads features: ad editing ...

Publishing and Editing Professionals
Publishing and Editing Professionals

Potential Markup and Editing Choices
Good programmers never write what they can steal or borrow. With that in mind, I did some research and found the promising but unfortunately ...

Epic announces VR updates for Unreal Editor, predicts VR editing future
The battle for the next big thing in virtual reality isn't just raging among headset makers; there's also a battle over the 3D engines that power ...

The US intelligence chief added gene editing to a list of threats that includes North Korea's nukes and ...
The United States' top intelligence official just added gene editing technology to a list of threats that includes North Korea's nukes and Syria's ...

UK approves controversial human gene editing technique
New York Times UK approves controversial human gene editing technique CBS News Britain's fertility regulator has approved a scientist's request ...

Duchess Kate skipped out of HuffPo guest-editing early to go shopping
... that she has to head out for some retail therapy. I also think it’s funny that Kate couldn’t even keep up the pretense of “guest-editing” HuffPo ...

Resources last updated: 2/22/2016 4:07:59 PM