LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-18-2013, 04:14 AM   #1
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Rep: Reputation: Disabled
Not able to edit multiple files by perl -pi -e!


Code:
perl -pi -e 'print "mango" if (($.==1) && ($_ !~ /#!!/))' flex3.pl new.pl
the above command works only for 1st file (flex3.pl) and not for the 2nd file(new.pl)
i have tested many times , sometimes giving *.pl in filenames but only the 1st one is getting affected. i think it should work for all the files
Let me clarify what i intend to achieve: i want the above command to check 1st line of all the files in the files i provided and then add "mango" if test passes.

Last edited by ravisingh1; 07-18-2013 at 04:19 AM.
 
Old 07-18-2013, 11:44 AM   #2
thirdm
Member
 
Registered: May 2013
Location: Massachusetts
Distribution: Slackware, OpenBSD, NetBSD
Posts: 233

Rep: Reputation: Disabled
Here's a hint from perldoc perlvar: " Because "<>" never does an explicit close, line numbers increase across "ARGV" files (but see examples in "eof" in perlfunc)."

Why is <> relevant?

See perldoc perlrun:

Code:
 From the shell, saying

             $ perl -p -i.orig -e "s/foo/bar/; ... "

         is the same as using the program:

             #!/usr/bin/perl -pi.orig
             s/foo/bar/;

         which is equivalent to

             #!/usr/bin/perl
             $extension = '.orig';
             LINE: while (<>) {
                 if ($ARGV ne $oldargv) {
                     if ($extension !~ /\*/) {
                         $backup = $ARGV . $extension;
                     }
                     else {
                         ($backup = $extension) =~ s/\*/$ARGV/g;
                     }
                     rename($ARGV, $backup);
                     open(ARGVOUT, ">$ARGV");
                     select(ARGVOUT);
                     $oldargv = $ARGV;
                 }
                 s/foo/bar/;
             }
             continue {
                 print;  # this prints to original filename
             }
             select(STDOUT);

         except that the -i form doesn't need to compare $ARGV to $oldargv
         to know when the filename has changed. It does, however, use
         ARGVOUT for the selected filehandle. Note that STDOUT is restored
         as the default output filehandle after the loop.

If what I'm writing isn't clear, try this on a couple of files and it will be clear what's happening:

Code:
perl -pi -e 's/^/$. /' *
# darn, how about this
perl -pi -e 's/^/$. /; $. = 0 if eof' *
# ah, okay.
 
Old 07-18-2013, 12:30 PM   #3
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
Thanks a lot! Seems to be good! I w'd go through it completely and reply.
 
Old 07-18-2013, 12:33 PM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,625

Rep: Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870Reputation: 6870
Quote:
Originally Posted by ravisingh1 View Post
Thanks a lot! Seems to be good! I w'd go through it completely and reply.
Similar to your other thread here, isn't it?
http://www.linuxquestions.org/questi...rl-4175469942/
 
Old 07-18-2013, 12:51 PM   #5
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by TB0ne View Post
Similar to your other thread here, isn't it?
http://www.linuxquestions.org/questi...rl-4175469942/
I read the links provided in my that thread. Today I executed after coding.
Yes I could have replied there only but still I considered this seperate from that post.
That thread was how to add a text on 1st line of a file and this is that I am able to add the text on 1st line of 1st file only though I want to add on 1st line of all the files mentioned in command line. I w'd mart that thread resolved.
 
Old 07-20-2013, 12:45 PM   #6
ravisingh1
Member
 
Registered: Apr 2013
Location: Mumbai
Distribution: Ubuntu13.10
Posts: 291

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by thirdm View Post
Here's a hint from perldoc perlvar: " Because "<>" never does an explicit close, line numbers increase across "ARGV" files (but see examples in "eof" in perlfunc)."

Why is <> relevant?

See perldoc perlrun:

Code:
 From the shell, saying

             $ perl -p -i.orig -e "s/foo/bar/; ... "

         is the same as using the program:

             #!/usr/bin/perl -pi.orig
             s/foo/bar/;

         which is equivalent to

             #!/usr/bin/perl
             $extension = '.orig';
             LINE: while (<>) {
                 if ($ARGV ne $oldargv) {
                     if ($extension !~ /\*/) {
                         $backup = $ARGV . $extension;
                     }
                     else {
                         ($backup = $extension) =~ s/\*/$ARGV/g;
                     }
                     rename($ARGV, $backup);
                     open(ARGVOUT, ">$ARGV");
                     select(ARGVOUT);
                     $oldargv = $ARGV;
                 }
                 s/foo/bar/;
             }
             continue {
                 print;  # this prints to original filename
             }
             select(STDOUT);

         except that the -i form doesn't need to compare $ARGV to $oldargv
         to know when the filename has changed. It does, however, use
         ARGVOUT for the selected filehandle. Note that STDOUT is restored
         as the default output filehandle after the loop.

If what I'm writing isn't clear, try this on a couple of files and it will be clear what's happening:

Code:
perl -pi -e 's/^/$. /' *
# darn, how about this
perl -pi -e 's/^/$. /; $. = 0 if eof' *
# ah, okay.
Thank you very much . i went through the perl document (perldoc perlrun) and got the solution.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] auto edit multiple files gary_in_springhill Programming 5 09-30-2011 10:59 PM
Use sed to edit string in multiple files not contained in current directory DylanB Linux - Newbie 1 04-07-2011 02:27 AM
Mass edit files using perl script BabySunny Linux - Newbie 10 02-03-2011 11:30 AM
[SOLVED] how to edit files with the ntfs-3g and Windows XP in a multiple boot system james2b Linux - Newbie 2 08-12-2009 11:21 PM
Edit multiple files in VI netchtech Linux - Software 1 11-23-2005 07:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration