Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   inserting lines (http://www.velocityreviews.com/forums/t899987-inserting-lines.html)

PG 09-20-2006 03:29 PM

inserting lines
 
I want to know how to add a line , say "foobar" , after a line which says,
for example "string1", in a inout file.

how can I do this ?

(I'm pretty new to Perl)



David Squire 09-20-2006 03:37 PM

Re: inserting lines
 
PG wrote:
> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
>
> how can I do this ?


What have you tried so far?

What is an "inout" file?


DS

as4109@wayne.edu 09-20-2006 04:22 PM

Re: inserting lines
 

PG ha escrito:

> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
>
> how can I do this ?
>
> (I'm pretty new to Perl)


perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'

The above, of course, is the same as the following script:

#!/usr/bin/perl

while (<>) {
if (/string1/) { chomp; s/$/foobar\n/ };
print
}


David Squire 09-20-2006 04:31 PM

Re: inserting lines
 
as4109@wayne.edu wrote:
> PG ha escrito:
>
>> I want to know how to add a line , say "foobar" , after a line which says,
>> for example "string1", in a inout file.
>>
>> how can I do this ?
>>
>> (I'm pretty new to Perl)

>
> perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
>


.... but this does not solve the OP's problem, which does not mention
substitution, but rather an additional line.

Still waiting to here what the OP has tried...


DS

Uri Guttman 09-20-2006 04:44 PM

Re: inserting lines
 
>>>>> "JG" == Jim Gibson <jgibson@mail.arc.nasa.gov> writes:

JG> The old-fashioned way is to:

JG> 1. Open the old file for reading (perldoc -f open).
JG> 2. Open a new file for writing.
JG> 3. Read the old file a line-at-a-time, looking for "string1" and
JG> writing out each line to the new file (perldoc -f print).
JG> 4. If "string1" is found, write out the line "foobar" (after you copy
JG> the string1 line to the new file.
JG> 5. When the old file has been completely read and copied, close both
JG> files.
JG> 6. Either within your program or via the operating system after the
JG> program has quit, rename the old file to the new file, after optionally
JG> renaming the old file as a backup (perldoc -f rename).

the new fashioned way to do this is with file::slurp and 1 s///g
call. in the near future (i keep promising this) there will be edit_file()
and edit_file_lines() subs added to that module that will do this in one
call.

uri

--
Uri Guttman ------ uri@stemsystems.com -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org

xhoster@gmail.com 09-20-2006 04:45 PM

Re: inserting lines
 
David Squire <David.Squire@no.spam.from.here.au> wrote:
> as4109@wayne.edu wrote:
> > PG ha escrito:
> >
> >> I want to know how to add a line , say "foobar" , after a line which
> >> says, for example "string1", in a inout file.
> >>
> >> how can I do this ?
> >>
> >> (I'm pretty new to Perl)

> >
> > perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
> >

>
> ... but this does not solve the OP's problem, which does not mention
> substitution, but rather an additional line.


How is substituting an empty string with a line not the same
thing as inserting a line? (although he is missing a newline, he
should take out the chomp and move \n to the beginning of the
replacement.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB

David Squire 09-20-2006 05:02 PM

Re: inserting lines
 
xhoster@gmail.com wrote:
> David Squire <David.Squire@no.spam.from.here.au> wrote:
>> as4109@wayne.edu wrote:
>>> PG ha escrito:
>>>
>>>> I want to know how to add a line , say "foobar" , after a line which
>>>> says, for example "string1", in a inout file.
>>>>
>>>> how can I do this ?
>>>>
>>>> (I'm pretty new to Perl)
>>> perl -pe 'if (/string1/) { chomp; s/$/foobar\n/ }'
>>>

>> ... but this does not solve the OP's problem, which does not mention
>> substitution, but rather an additional line.

>
> How is substituting an empty string with a line not the same
> thing as inserting a line? (although he is missing a newline, he
> should take out the chomp and move \n to the beginning of the
> replacement.
>


Ah. I misread that. Sorry.


DS

John W. Krahn 09-20-2006 05:16 PM

Re: inserting lines
 
PG wrote:
> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.
>
> how can I do this ?


perl -i -pe'$_ .= "foobar\n" if /string1/' inout_file



John
--
use Perl;
program
fulfillment

Ted Zlatanov 09-20-2006 07:21 PM

Re: inserting lines
 
On 20 Sep 2006, noreply@hotmail.com wrote:

> I want to know how to add a line , say "foobar" , after a line which says,
> for example "string1", in a inout file.


This will print the new data:

perl -pe'$_ = "${_}foobar\n" if /string1/;' FILE

This will rewrite FILE with the new data:

perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE

Ted

John W. Krahn 09-20-2006 08:30 PM

Re: inserting lines
 
Ted Zlatanov wrote:
> On 20 Sep 2006, noreply@hotmail.com wrote:
>
>>I want to know how to add a line , say "foobar" , after a line which says,
>>for example "string1", in a inout file.

>
> This will print the new data:
>
> perl -pe'$_ = "${_}foobar\n" if /string1/;' FILE
>
> This will rewrite FILE with the new data:
>
> perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE


That won't work as the -i switch requires an argument which is appended to the
back-up file name:

$ perl -pie'$_ = "${_}foobar\n" if /string1/;' FILE
Unrecognized switch: -= "${_}foobar\n" if /string1/; (-h will show valid
options).


John
--
use Perl;
program
fulfillment


All times are GMT. The time now is 11:05 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.