m (→Pattern substitutions) |
m (→One-liners) |
||
(21 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | |||
= Code = | = Code = | ||
Line 10: | Line 9: | ||
# [[stampit]] — to stamp pdf files after their name. | # [[stampit]] — to stamp pdf files after their name. | ||
# [[sanitize]] — to remove accents & special characters from filenames. | # [[sanitize]] — to remove accents & special characters from filenames. | ||
− | # [[putInDir]] — to move files inside directories bearing their name. | + | # [[putInDir]] — to move files inside directories bearing their name or their date. |
# [[uniqname]] — to generate a timestamp which can be used as a unique name. | # [[uniqname]] — to generate a timestamp which can be used as a unique name. | ||
+ | [[bib2wiki]] & [[keys2wiki]] | ||
+ | |||
+ | == One-liners == | ||
+ | |||
+ | * Find [[Mathematica]] notebooks (.nb) in user directory modified in the last 2 days: | ||
+ | |||
+ | <pre> | ||
+ | find ~/ -mtime -2 -name "*.nb" -ls | ||
+ | </pre> | ||
+ | |||
+ | * Find files (also nb here) modified on a given date, here {{thisday|15|October|2023}}: | ||
+ | |||
+ | <pre> | ||
+ | find ~/ -type f -name "*.nb" -newermt 2023-10-15 ! -newermt 2023-10-16 | ||
+ | </pre> | ||
+ | |||
+ | == Conversion == | ||
+ | |||
+ | === From a list of png to mp4 === | ||
+ | |||
+ | Essentially use mencoder on files converted to jpg. See [[Convert from a list of png to mp4|this page]] for more details. | ||
+ | |||
+ | === From pdf to png === | ||
+ | |||
+ | Better than convert: (-f and -l specify first & last page): | ||
+ | |||
+ | <pre>pdftoppm -png -f 2 -l 2 input.pdf > output.png</pre> | ||
+ | |||
+ | == Apply a command to all files == | ||
+ | |||
+ | For instance converting png files to pdf (without the <nowiki>${f%%.*}</nowiki> part, file.png would be converted to file.png.pdf instead of file.pdf): | ||
+ | |||
+ | <code lang='bash'>for f in *.png; do convert "$f" ""${f%%.*}".pdf"; done</code> | ||
== Pattern substitutions == | == Pattern substitutions == | ||
− | See [http:// | + | See [http://goo.gl/C7Wsp Jukka “Yucca” Korpela]'s cheatsheet for regexps ([[Media:Regular_expressions_in_Perl_-_a_summary_with_examples.war|archived]]) |
=== Replace comma-separated digits by their point-separated counterpart === | === Replace comma-separated digits by their point-separated counterpart === | ||
Line 25: | Line 57: | ||
=== Sanitize CVS files === | === Sanitize CVS files === | ||
− | The following will sanitize [http://en.wikipedia.org/wiki/Comma-separated_values CSV files] from trailing text (headers, comments on lines ''following'' the CSV, etc.): | + | The following will sanitize all [http://en.wikipedia.org/wiki/Comma-separated_values CSV files] (here with extension .prf) from trailing text (headers, comments on lines ''following'' the CSV, etc.): |
<code lang='bash'>for f in *.prf; do cat "$f" | perl -ne 'print "$1\n" if /^([ \t]*([-+]?\d*\.?\d+([eE][-+]?\d+)?,)*[ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?)/' > $f.dat ; done</code> | <code lang='bash'>for f in *.prf; do cat "$f" | perl -ne 'print "$1\n" if /^([ \t]*([-+]?\d*\.?\d+([eE][-+]?\d+)?,)*[ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?)/' > $f.dat ; done</code> | ||
Line 33: | Line 65: | ||
<code lang='bash'>for f in *.prf; do cat "$f" | perl -ne 'print "$1\n" if /^(([ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?,){1}[ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?)/' > $f.dat ; done</code> | <code lang='bash'>for f in *.prf; do cat "$f" | perl -ne 'print "$1\n" if /^(([ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?,){1}[ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?)/' > $f.dat ; done</code> | ||
− | Be sure that you understand the script so that the sanitization goes to the depth you wish it to. | + | Be sure that you understand the script so that the sanitization goes to the depth you wish it to (for instance in its present form the first line of code will keep lines in the CSV file with a number but no comma as a valid line [with one value]). |
+ | |||
+ | === Changes footnote to inline references === | ||
+ | |||
+ | In a {{latex}} document, if you need to migrate from some superscript quoting like this,<sup>7</sup> to some inline one like this~[7], you will likely need to replace | ||
+ | |||
+ | :text like this,\quote{ref7} | ||
+ | |||
+ | to | ||
+ | |||
+ | :text like this∼\quote{ref7}, | ||
+ | |||
+ | since usage wants that superscript quotation goes after the punctuation mark. If there is no punctuation, you'll want ''this\quote{ref7}'' to become ''this∼\quote{ref7}''. The following line of code does such a substitution: (the punctuation you want to be taken care of is in the square bracket [\.,;!?]) | ||
+ | (caution: replace the [[tilde]] below by a proper one) | ||
+ | |||
+ | <code lang='bash'>perl -p -e 's/([\.,;!?]*)(\\cite\{(\w+,)*\w+\})/∼\2\1/g;' superscript.tex > inline.tex</code> | ||
+ | |||
+ | Note that APS has an option ''citeautoscript'' to take care of superscript quoting regardless of how you quote respectively to punctuation, but this is for the PRB style only [http://goo.gl/W2LxI]. | ||
+ | |||
+ | <code lang='latex'>\usepackage[aps,prb,citeautoscript]{revtex4-1}</code> | ||
+ | |||
+ | == Change path == | ||
+ | |||
+ | In {{LaTeX}}, to have: | ||
+ | |||
+ | <pre> | ||
+ | \includegraphics[width=0.75\textwidth]{halfsoliton.pdf} | ||
+ | </pre> | ||
+ | |||
+ | be replaced by: | ||
+ | |||
+ | <pre> | ||
+ | \includegraphics[width=0.75\textwidth]{fig/10/halfsoliton.pdf} | ||
+ | </pre> | ||
+ | |||
+ | <code lang='bash'>cat chap10.tex | perl -p -e 's/\\includegraphics(.*)\{(\w+)\.pdf\}/\\includegraphics\1\{fig\/10\/\2\.pdf\}/g;' > newchap10.tex</code> | ||
+ | |||
+ | This version allows for over file extensions (not only pdf): | ||
+ | |||
+ | <code lang='bash'>cat chap10.tex | perl -p -e 's/\\includegraphics(.*)\{(\w+)\.(\w+)\}/\\includegraphics\1\{fig\/10\/\2\.\3\}/g;' > newchap10.tex</code> | ||
+ | |||
+ | == Expand {{LaTeX}} commands == | ||
+ | |||
+ | To replace | ||
+ | |||
+ | <pre> | ||
+ | \ket{\psi_a} | ||
+ | </pre> | ||
+ | |||
+ | to | ||
+ | |||
+ | <pre> | ||
+ | |\psi_a\rangle | ||
+ | </pre> | ||
+ | |||
+ | <syntaxhighlight lang="latex"> | ||
+ | cat file.tex | perl -p -e 's/\\ket\{(\S[^}]+)\}/---|\1\\rangle----/g;' | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | This does not address issues with nested brackets like <tt>\ket{\psi_{ab}}</tt> | ||
== Pretty print == | == Pretty print == | ||
− | We use [http://www.mediawiki.org/wiki/User:Gri6507 Paul Grinberg]'s [http://www.mediawiki.org/wiki/Extension:Code Code extension] for [[Mediawiki]] | + | We use [https://www.mediawiki.org/wiki/Extension:SyntaxHighlight Extension:SyntaxHighlight] to pretty-print source through [http://qbnz.com/highlighter/ GeSHi] on our web. |
+ | |||
+ | Previously, we used [http://www.mediawiki.org/wiki/User:Gri6507 Paul Grinberg]'s [http://www.mediawiki.org/wiki/Extension:Code Code extension] for [[Mediawiki]] until it was discontinued. |
Contents |
This page is still largely in progress.
This is a list of code we make available with no guarantee, beside the one that it did once work for its intended purpose.
Beware, version below one (e.g., v°0.1) are $\beta$-version. It might be that's all you find here.
bib2wiki & keys2wiki
find ~/ -mtime -2 -name "*.nb" -ls
find ~/ -type f -name "*.nb" -newermt 2023-10-15 ! -newermt 2023-10-16
Essentially use mencoder on files converted to jpg. See this page for more details.
Better than convert: (-f and -l specify first & last page):
pdftoppm -png -f 2 -l 2 input.pdf > output.png
For instance converting png files to pdf (without the ${f%%.*} part, file.png would be converted to file.png.pdf instead of file.pdf):
for f in *.png; do convert "$f" ""${f%%.*}".pdf"; done
See Jukka “Yucca” Korpela's cheatsheet for regexps (archived)
E.g, 123,45 → 123.45. To change in all .dat files:
perl -pi -w -e 's/(\d+),(\d+)/$1\.$2/g;' *dat
The following will sanitize all CSV files (here with extension .prf) from trailing text (headers, comments on lines following the CSV, etc.):
for f in *.prf; do cat "$f" | perl -ne 'print "$1\n" if /^([ \t]*([-+]?\d*\.?\d+([eE][-+]?\d+)?,)*[ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?)/' > $f.dat ; done
This is a variation to keep all lines which have exactly 2 values (replace {1} by {$n-1$} to have exactly $n$ values per line):
for f in *.prf; do cat "$f" | perl -ne 'print "$1\n" if /^(([ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?,){1}[ \t]*[-+]?\d*\.?\d+([eE][-+]?\d+)?)/' > $f.dat ; done
Be sure that you understand the script so that the sanitization goes to the depth you wish it to (for instance in its present form the first line of code will keep lines in the CSV file with a number but no comma as a valid line [with one value]).
In a $\mathrm{\LaTeX}$ document, if you need to migrate from some superscript quoting like this,7 to some inline one like this~[7], you will likely need to replace
to
since usage wants that superscript quotation goes after the punctuation mark. If there is no punctuation, you'll want this\quote{ref7} to become this∼\quote{ref7}. The following line of code does such a substitution: (the punctuation you want to be taken care of is in the square bracket [\.,;!?]) (caution: replace the tilde below by a proper one)
perl -p -e 's/([\.,;!?]*)(\\cite\{(\w+,)*\w+\})/∼\2\1/g;' superscript.tex > inline.tex
Note that APS has an option citeautoscript to take care of superscript quoting regardless of how you quote respectively to punctuation, but this is for the PRB style only [1].
\usepackage[aps,prb,citeautoscript]{revtex4-1}
In $\mathrm{\LaTeX}$, to have:
\includegraphics[width=0.75\textwidth]{halfsoliton.pdf}
be replaced by:
\includegraphics[width=0.75\textwidth]{fig/10/halfsoliton.pdf}
cat chap10.tex | perl -p -e 's/\\includegraphics(.*)\{(\w+)\.pdf\}/\\includegraphics\1\{fig\/10\/\2\.pdf\}/g;' > newchap10.tex
This version allows for over file extensions (not only pdf):
cat chap10.tex | perl -p -e 's/\\includegraphics(.*)\{(\w+)\.(\w+)\}/\\includegraphics\1\{fig\/10\/\2\.\3\}/g;' > newchap10.tex
To replace
\ket{\psi_a}
to
|\psi_a\rangle
cat file.tex | perl -p -e 's/\\ket\{(\S[^}]+)\}/---|\1\\rangle----/g;'
This does not address issues with nested brackets like \ket{\psi_{ab}}
We use Extension:SyntaxHighlight to pretty-print source through GeSHi on our web.
Previously, we used Paul Grinberg's Code extension for Mediawiki until it was discontinued.