Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-11-05 14:37:32

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,259
GitHub

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

#2 2015-11-05 16:23:01

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

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

#3 2015-11-05 17:25:36

gaekwad
Server grease monkey
From: People's Republic of Cornwall
Registered: 2005-11-19
Posts: 4,259
GitHub

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

Board footer

Powered by FluxBB