inserting lines

P

PG

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)
 
D

David Squire

PG said:
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
 
A

as4109

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
}
 
D

David Squire

PG ha escrito:


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
 
U

Uri Guttman

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
 
X

xhoster

David Squire said:
... 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
 
D

David Squire

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
 
J

John W. Krahn

PG said:
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
 
T

Ted Zlatanov

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
 
J

John W. Krahn

Ted said:
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
 
T

Ted Zlatanov

On 20 Sep 2006, (e-mail address removed) wrote:

Ted Zlatanov wrote: > On 20 Sep 2006, (e-mail address removed) wrote: >
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).

Yes. -pi -e will work. I was mesmerized by the food reference.

Ted
 
J

Jürgen Exner

PG said:
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 ?

Which part do you have problems with? Have you checked the FAQ 'perldoc -q
insert':
"How do I change one line in a file/delete a line in a file/insert a line in
the middle of a file/append to the beginning of a file?"

jue
 
A

anno4000

Jim Gibson said:
This is a "Frequently Asked Question". Unfortunately, the FAQ for Perl
only has one recommendation: using the Tie::File module. If you are new
to Perl, learning about the "tie" concept may confuse you, as may using
modules, although you will definitely benefit from learning how to use
modules.

I too find it unfortunate that the FAQ doesn't explain the process
in more detail. Typical file-manipulation tools (most notably editors)
give the user the impression that a file is something like an array
of lines on disk. Many beginning programmers have this mental image.
Understanding that a file is really one homogeneous lump of bytes
is a necessary step for a new programmer. This FAQ would be a good
opportunity to set the concept right and explain the consequences.
Instead it does all it can to preserve the inappropriate mental
image.

I think the original content of the FAQ (which explained the manual
process of inserting lines etc.) should be put back in, rewritten if
necessary. A good opportunity to win wealth and fame with a doc
change :)

Anno
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top