Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Pages: 1
File wildcards in Perl
This isn’t strictly a Textpattern question, but I’m using Perl to manipulate Textpacks — the Textpattern language translations — and I would be grateful for some assistance.
When a new string is added to Textpacks, it needs to go into each language file on the same line. Take the following example: add search_logs =>
to line 549. Right now, I do this with Perl — mostly because sed
plays differently on OS X to UNIX and Linux — and this is how I do it:
perl -pi -e 'print "search_logs =>\n" if $. == 549' ar-dz.textpack;
That line adds search_logs =>
and a line break to line 549 in the Arabic (Algerian) translations. To change all the Textpacks, I do this:
perl -pi -e 'print "search_logs =>\n" if $. == 549' ar-dz.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' bg-bg.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' bs-ba.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' ca-es.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' cs-cz.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' da-dk.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' de-de.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' el-gr.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' en-gb.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' en-us.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' es-es.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' et-ee.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' fa-ir.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' fi-fi.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' fr-fr.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' gl-es.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' he-il.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' hr-hr.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' hu-hu.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' id-id.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' is-is.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' it-it.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' ja-jp.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' ko-kr.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' lt-lt.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' lv-lv.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' nb-no.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' nl-nl.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' pl-pl.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' pt-br.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' pt-pt.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' ro-ro.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' ru-ru.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' sk-sk.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' sr-rs.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' sr-sp.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' sv-se.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' th-th.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' tr-tr.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' uk-ua.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' ur-in.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' vi-vn.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' zh-cn.textpack;perl -pi -e 'print "search_logs =>\n" if $. == 549' zh-tw.textpack;
That line process the string being added, with each of the Textpacks explicitly named. This is OK, but I’m sure it could be improved to perform the action on all the .textpack
files in the current directory. The trouble I have is that I’m a lapsed Perl bodger, and I was never very good at it.
How would I change this line to process *.textpack
files in the current directory:
perl -pi -e 'print "search_logs =>\n" if $. == 549' ar-dz.textpack;
Thanks in advance.
Offline
Re: File wildcards in Perl
Try this:
perl -pi -e 'print "search_logs =>\n" if $. == 549; close ARGV if eof' *.textpack
‘close ARGV if eof’ is needed to reset the variable ‘$.’ before processing the next file.
Offline
Re: File wildcards in Perl
ruud wrote #296445:
Try this:
perl -pi -e 'print "search_logs =>\n" if $. == 549; close ARGV if eof' *.textpack...
‘close ARGV if eof’ is needed to reset the variable ‘$.’ before processing the next file.
Perfect. That’s it. Thank you, Ruud – excellent work.
Offline
Pages: 1