Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: updating Input file

by ambrus (Abbot)
on Feb 23, 2004 at 21:09 UTC ( [id://331209]=note: print w/replies, xml ) Need Help??


in reply to updating Input file

For such a small file, a simple text solution should be enough, no need to use databases. However take care not to lose data because of an error in the script.

Let me give an example code. This code deletes gorge and changes John's age to two less.

perl -i~ -we '$_=<>; @f= split; print; while (<>) { @a{@f}= split or w +arn next; $a{stdNam} eq "John" and $a{stdAge}-= 2; $a{stdNam} eq "gor +ge" and next; print join ("\t", @a{@f}), $/; }' file

Update: as an alternative, just open the file in your editor, and filter the whole text through the same perl command just without the -i~ switch and the filename. I use perl one-liners a lot of times this way. This is convenient as you can immediately see the result, and undo the changes if the script does not work at first. (In vi, you just press gg!G and type the command, starting with perl. In emacs, use ^[<^[>^U^[|. In joe, ^KB^KK^K/.)

Replies are listed 'Best First'.
Re: Re: updating Input file
by arden (Curate) on Feb 23, 2004 at 21:36 UTC
    You need to be careful not to delete the wrong (multiple) students too. For the examples that waswas-fng and pzbagel gave, if you attempt to delete student 12 (John), it will also delete users 120-129. . . You might want to consider using a hash with the student number as the key instead of the arrays to prevent that.

    - - arden.

      Noone yet has answered what the problem is witheyour original script you gave.

      The problem is

      1. it chops newlines but does not write them back. Change print FILECOPY; to print FILECOPY $_,$/;.
      2. You did not chomp $std. This is neccessary as otherwise the regexp wont match.
      3. It would be wise to change s/$_// to next.
      4. Also the filename "/home/stdInfo" may be wrong.

      5. Also arden says:

        For the examples that waswas-fng and pzbagel gave, if you attempt to delete student 12 (John), it will also delete users 120-129. . .

        That is also true for the original script in the question. So change /^$std/ to /^\Q$std\E\b/.

      Apart from these your script is correct.
        thanks much

      I belive that my solution is correct in this sense. $a{stdNum}==12 will match only student number 12.

      You only have to make sure that there are no spaces in the stdNam field or change split to split /\s\s+/.

      (Maybe you wanted to post this as a reply to the original question not my post. I'm not sure.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://331209]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 16:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found