CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Editing Files

 



wickedxter
User

Apr 17, 2001, 1:54 PM

Post #1 of 11 (1440 views)
Editing Files Can't Post

Im having a probelm of coming up with a way to edit files.. the file is set up like this
1|stuff
2|stuff
3|stuff

how would i make 2|stuff to 2|somthing else

or change any other thing in the file??



MattWilliams
Deleted

Apr 18, 2001, 1:58 AM

Post #2 of 11 (1437 views)
Re: Editing Files [In reply to] Can't Post

Assuming you know how to read in a file and edit it? I suggest you simply read the file into an array then iterate through the array untill you find your match and do a regex substitution on it? if you were to give us a scenario of what you end goal is, it would help.



wickedxter
User

Apr 18, 2001, 1:47 PM

Post #3 of 11 (1428 views)
Re: Editing Files [In reply to] Can't Post

Im trying to get it to open the file in to an array and edit it but i cant seem to get it delete the line im editing this is what happens

file before editing:
1|stuff
2|stuff
3|stuff

im trying to change 2|stuff to 2|gone and keep everything else.

after edit
1|stuff
2|stuff
2|gone
3|stuff

i hope thats enough..



freddo
User

Apr 21, 2001, 8:05 AM

Post #4 of 11 (1416 views)
Re: Editing Files [In reply to] Can't Post

Hello Wickedxter,

here's my try:

Code
administrator@COKE /cygdrive/h/scripts.pl/perlguru 
$ cat wickedxter.txt
1|stuff
2|stuff
3|stuff

administrator@COKE /cygdrive/h/scripts.pl/perlguru
$ perl -ane 'print; print if s/^2\|stuff/2\|gone/;' wickedxter.txt
1|stuff
2|stuff
2|gone
3|stuff

i hope this helps,
freddo

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?


wickedxter
User

Apr 21, 2001, 9:24 AM

Post #5 of 11 (1414 views)
Re: Editing Files [In reply to] Can't Post

do you think you could explain your code, a little more...

thx, Wickedxter



Pro_4
User

Apr 21, 2001, 11:27 AM

Post #6 of 11 (1412 views)
Re: Editing Files [In reply to] Can't Post

nm

(This post was edited by Pro_4 on Apr 21, 2001, 11:18 AM)


Jasmine
Administrator / Moderator

Apr 21, 2001, 1:19 PM

Post #7 of 11 (1408 views)
Re: Editing Files [In reply to] Can't Post

nm?



Pro_4
User

Apr 21, 2001, 1:31 PM

Post #8 of 11 (1407 views)
Re: Editing Files [In reply to] Can't Post

I had posted something then i realized i was totally wrong so i put nm (nevermind).

Always add fun to Perl
$fun = "fun"; $perl = "Perl";
print " Always add $fun to $perl"; die;


freddo
User

Apr 22, 2001, 5:07 AM

Post #9 of 11 (1393 views)
Re: Editing Files [In reply to] Can't Post

Hi Wickedxter,

Sure, here are some more info, of course the file wickedxter.txt contains:

Code
1|stuff 
2|stuff
3|stuff


so let's look at the command line arguments (more info in: perldoc perlrun on command switches, the following stuff is a copy/paste from there):

Code
perl -ane


-a -- it turns autosplit mode on, and it's not useful here (i use it generally, and it's an error from my part to have put it here, sorry i hope it didnt confused you too much).

-n -- causes Perl to assume the following loop around your program, which makes it iterate over filename arguments somewhat like sed -n or awk:

Code
LINE: 
while (<>) {
... # your program goes here
}


-e commandline -- may be used to enter one line of program. If -e is given, Perl will not look for a filename in the argument list.

another switch that could be of some use to you is also:

-i[extension] specifies that files processed by the <> construct are to be edited in-place. It does this by renaming the input file, opening the output file by the original name, and selecting that output file as the default for print() statements. If no extension is supplied, no backup is made and the current file is overwritten.

So our command line is now:

Code
perl -i '.bak' -ne 'print; print if s/^2\|stuff/2\|gone/;' wickedxter.txt

it will rename wickedxter.txt to wickedxter.txt.bak, and will execute the following code:

Code
1| LINE: 
2| while (defined(($_ = <ARGV>))) {
3| print($_);
4| (s/^2\|stuff/2|gone/ and print($_));
5| }

(you can get this output from perl -MO=Deparse,-p,-q,-sCT -ni'.bak' -e 'print; print if s/^2\|stuff/2\|gone/;' wickedxter.txt, check perldoc B::Deparse, it helps a lot to understand how perl "sees" your programs).

Let's examine this in detail:
Line 1, 2 & 5 are added by the -n switch.

Line 1: LINE: is just a label, meaning that you can refer to it (like with next LINE;)

Line 2: while (defined(($_ = <ARGV>))) { ... } makes it iterate over filename arguments (in our case wickedxter.txt), read a line and place it in $_

Line 3: print($_); prints the line that was just read.

Line 4: (s/^2\|stuff/2|gone/ and print($_)) (was initially print if s/^2\|stuff/2\|gone/; B::Deparse shows us how perl optimize our program).
It first try to make a substitution trying to find 2|stuff at the start of a line (the ^ indicate the start of a line, and the \| prevent the use of the pipe as a part of the regular expression), and replace it with 2|gone (same note for the backslash). Now, you should know that s/// returns the number of substitutions, so that if there is no substitutions made, it returns 0 (which means false for perl).
The and is a "C-style Logical And" (see perldoc perlop), if the left operand is false, the right operand is not even evaluated.
And the last part print($_); prints the line again (that is, if some substitution occured).

That's all ... I hope this helps a little more :-)
freddo

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?

(This post was edited by freddo on Apr 22, 2001, 5:14 AM)


wickedxter
User

Apr 22, 2001, 1:03 PM

Post #10 of 11 (1382 views)
Re: Editing Files [In reply to] Can't Post

thx alot.. =)



freddo
User

Apr 22, 2001, 1:10 PM

Post #11 of 11 (1381 views)
Re: Editing Files [In reply to] Can't Post

you're welcome :-)

;---
Real Programmers are surprised when their cars'odometer don't turn
from 99999 to 9999A!?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives