Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^4: s// All Files In Directory

by perl.j (Pilgrim)
on Apr 02, 2012 at 20:39 UTC ( [id://963115]=note: print w/replies, xml ) Need Help??


in reply to Re^3: s// All Files In Directory
in thread s// All Files In Directory

So perl -p -i -e 's/5/6/g' will do what I want?
--perl.j

Replies are listed 'Best First'.
Re^5: s// All Files In Directory
by stevieb (Canon) on Apr 02, 2012 at 20:53 UTC

    No. You have to add the file names you want to work with after the command. In your case you want to work with all files, so append an asterisk (*). Also, add a file extension to 'i' if you want to create a backup:

    perl -pi.bak -e 's/5/6/g;' *

    ...will create a copy of each file with a .bak extension before performing the search/replace. If you only want to affect certain files, name them explicitly, or use glob patterns. For instance, the following will only work on files with a .txt extension:

    perl -pi -e 's/5/6/g;' *.txt
      That depends entirely upon the shell. Bash will turn *.txt into a list of files. cmd.exe will not.

      This doesn't seem to be working...

      Can't open *: Invalid argument.

      --perl.j
        To tame the cmd you could use the brilliant idea which I read by BrowserUK (cannot find the link just now) - the following one-liner works on my windows pc:
        perl -pi.bak -e "BEGIN{@ARGV=map{glob}@ARGV;}s/5/6/g;" *

        A wild guess: On Windows, you used to have to use *.* to match all files, while a single * would only match files with no extension. Is that still the case?

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (7)
As of 2024-04-19 10:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found