/server/server1/Defaults
/server/server2/Defaults
......
/server/servern/Defaults
and process it, for any other sub directories just ignore them. How to
do this in perl?
Please help me, thanks.
The obvious question is: What have you tried so far and where did your
code not meet your expectations?
Hint:
foreach (</server/server*/Defaults>) {
# ...
}
Josef
--
These are my personal views and not those of Fujitsu Technology Solutions!
Josef M�llers (Pinguinpfleger bei FTS)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://de.ts.fujitsu.com/imprint.html
One way:
glob() do get the file list, for() to loop through it, open() to access
the file, <> to get the content, print() to write the processed content
back, close() to, well, close the file again.
Which area do you have problems withj?
Not a completely Perl based solution, but I would do:
echo /server/server*/Defaults | xargs perl -pi <script>
(or use some other flags to perl, depending on your requirements)
(btw, using (build in!) echo avoids command line length limits, don't use
ls. Gnu find is another good solution, but echo is wayyy simpler in this
case.)
(note: does NOT work if you have spaces or other whitespace characters in
file names!)
M4
>> /server/server1/Defaults
>> /server/server2/Defaults
>> ......
>> /server/servern/Defaults
So: Use "find -print0" and "xargs -0r". Or, in this particu-
lar case, "perl -pi $SCRIPT /server/server*/Defaults". Oh,
and always use "-i.bak" while you're at it.
Tim
>> (btw, using (build in!) echo avoids command line length limits, don't
>> use ls. Gnu find is another good solution, but echo is wayyy simpler in
>> this case.)
>
>> (note: does NOT work if you have spaces or other whitespace characters
>> in file names!)
>
> So: Use "find -print0" and "xargs -0r". Or, in this particu- lar case,
> "perl -pi $SCRIPT /server/server*/Defaults". Oh, and always use "-i.bak"
> while you're at it.
This does not do what the OP wants without additional flags, and is Gnu
specific as well. It can work with something like (untested):
find /server -path '/server/server*/' -midepth 2 -maxdepth 2 -name
Default -print0 | xargs -0r ......
Otherwise you may run into command line limits again. A non Gnu solution
would be something like (again untested):
echo /server/server*/Defaults | while read f; do perl -pi $SCRIPT; done
This still does not account for file names with embedded newlines, but
those are rare enough not to worry about them in most cases.
M4
M4
Martijn> echo /server/server*/Defaults | while read f; do perl -pi $SCRIPT; done
I don't think that'll do it.
echo will generate something like:
/server/server1/Defaults /server/server2/Defaults /server/server3/Defaults
which "read f" will read all at once making $f to be
"/server/server1/Defaults /server/server2/Defaults /server/server3/Defaults"
--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
>>>>>> "Martijn" == Martijn Lievaart <m...@rtij.nl.invlalid> writes:
>
> Martijn> echo /server/server*/Defaults | while read f; do perl -pi
> $SCRIPT; done
>
> I don't think that'll do it.
>
> echo will generate something like:
>
> /server/server1/Defaults /server/server2/Defaults
> /server/server3/Defaults
>
> which "read f" will read all at once making $f to be
>
> "/server/server1/Defaults /server/server2/Defaults
> /server/server3/Defaults"
You're right. I'll think about it some more. Shows again this simple
problem is not simple solvable in shell without gnu utilities. So there
is a lot to be said for the Perl solution after all.
OK, how about this:
ls /server | grep ^server | while read f; do if [ -e "/server/$f/
Default" ]; then perl -pi $SCRIPT "$f" ; fi; done
Oh, this is a Perl newsgroup, I'll let it go at this.
M4
echo /server/server*/Defaults | xargs -n 1 |
while read f; do perl -pi "$SCRIPT"; done
The xargs(1) man page says
... xargs reads items from the standard input ... and executes the
command (default is /bin/echo) one or more times ...
-n max-args
Use at most max-args arguments per command line. ...
But I, too, still don't like that it doesn't handle shell
metacharacters in the filenames.
--
Tim McDaniel, tm...@panix.com