<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>zInformatik &#187; Einzeiler</title>
	<atom:link href="http://zinformatik.de/tag/einzeiler/feed/" rel="self" type="application/rss+xml" />
	<link>http://zinformatik.de</link>
	<description>Computer, Informatik und Mikrocontroller Blog</description>
	<lastBuildDate>Sat, 22 Oct 2011 09:51:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Perl Einzeiler auf der Konsole</title>
		<link>http://zinformatik.de/linux/perl-einzeiler-auf-der-konsole/</link>
		<comments>http://zinformatik.de/linux/perl-einzeiler-auf-der-konsole/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 18:59:11 +0000</pubDate>
		<dc:creator>zimon</dc:creator>
				<category><![CDATA[Linux]]></category>
		<category><![CDATA[Programmieren]]></category>
		<category><![CDATA[Ubuntuusers-Planet]]></category>
		<category><![CDATA[Einzeiler]]></category>
		<category><![CDATA[Konsole]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://zinformatik.de/?p=1373</guid>
		<description><![CDATA[<p>Ähnlich wie bei <a href="Keine Angst vor awk - ein Schnelleinstieg" title="http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/" class="liinternal">awk</a> kann man perl das Programm auf der Konsole direkt übergeben. Dazu ruft man perl mit der Option <code>-e</code> auf. Das Programm muss dann in Hochkommata eingeschlossen werden.<br />
Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print &#34;Hello World!\n&#34;'</span></pre></div></div>

<p>Um jede print-Anweisung mit einem &#8220;newline&#8221; abzuschließen, kann&#8230;</p>]]></description>
			<content:encoded><![CDATA[<p>Ähnlich wie bei <a href="Keine Angst vor awk - ein Schnelleinstieg" title="http://zinformatik.de/linux/keine-angst-vor-awk-ein-schnelleinstieg/" class="liinternal">awk</a> kann man perl das Programm auf der Konsole direkt übergeben. Dazu ruft man perl mit der Option <code>-e</code> auf. Das Programm muss dann in Hochkommata eingeschlossen werden.<br />
Beispiel:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print &quot;Hello World!\n&quot;'</span></pre></div></div>

<p>Um jede print-Anweisung mit einem &#8220;newline&#8221; abzuschließen, kann man zusätzlich die Option <code>-l</code> benutzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-l</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print &quot;Hello World!&quot;; print &quot;Hello Ubuntuusers!&quot;'</span></pre></div></div>

<p>Um eine Datei Zeile für Zeile durchzugehen, gibt es die Option <code>-n</code>. Das Program beschreibt dann ähnlich wie bei awk was mit jeder Zeile geschehen soll:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-l</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print $_ if $_ =~ /foo/&quot;'</span> datei.txt</pre></div></div>

<p>gibt alle Zeilen aus, die &#8220;foo&#8221; enthalten.<br />
Die Option -n entspricht folgendem Code:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;"># Der Code, der übergeben wird</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die Option -a bringt perl noch einen Schritt näher zu awk. Damit werden die Wörter einer Zeile (die durch Leerzeichen getrennt sind) in das Array <code>@F</code> geladen. Den Worttrenner kann man mit der Option <code>-F</code> setzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-a</span> <span style="color: #660033;">-F</span>; <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'print $F[$#F]'</span> datei.csv</pre></div></div>

<p>gibt von jeder Zeile das letzte Element einer csv (Comma Separated Value) Datei aus.<br />
Dies entspricht folgendem Code:</p>

<div class="wp_syntax"><div class="code"><pre class="perl" style="font-family:monospace;"><span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&lt;&gt;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #0000ff;">@F</span> <span style="color: #339933;">=</span> <span style="color: #000066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">$pattern</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;"># Der übergebene Code</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>wobei <code>$pattern</code> das mit -F übergebene Pattern (im obigen Beispiel <code>";"</code>) ist. Dadurch sind auch reguläre Ausdrücke wie <code>-F/[0-9]+/</code> möglich.</p>
<p>Es können auch die Schlüsselwörter BEGIN und END gesetzt werden um einen Block vor oder nach der Hauptschleife auszuführen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-n</span> <span style="color: #660033;">-l</span> <span style="color: #660033;">-a</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'BEGIN{$x=0} {$x+=$F[$#F]} END{print $x}'</span> preisliste.txt</pre></div></div>

<p>errechnet den Gesammtpreis einer Preisliste, in der am Ende jeder Zeile der Preis steht. Es wird also von jeder Zeile das letzte Wort addiert. Man könnte das Programm jetzt auch noch etwas kürzer schreiben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-nlae</span> <span style="color: #ff0000;">'$x+=$F[$#F]; END{print $x}'</span> preisliste.txt</pre></div></div>

<p>Dies entspricht dem awk-Programm:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #ff0000;">'{x+=$NF} END{print x}'</span> preisliste.txt</pre></div></div>

<p>Für das Suchen und Ersetzen mit Regulären Ausdrücken eignet sich die Option <code>-p</code>. Diese Funktioniert genau wie <code>-n</code> mit dem Unterschied, dass jede Zeile automatisch ausgegeben wird. So kann man wie mit sed Dateien ändern:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pe</span> <span style="color: #ff0000;">'s/foo/bar/g'</span> datei.txt <span style="color: #000000; font-weight: bold;">&gt;</span> datei_new.txt</pre></div></div>

<p>ersetzt jedes Vorkommen von &#8220;foo&#8221; durch &#8220;bar&#8221; und schreibt das Ergebnis in die Datei &#8220;datei_new.txt&#8221;.</p>
<p>Um die Datei direkt zu ändern, kann man die Option <code>-i</code> verwenden. Dieser kann optional ein String mitgegeben, der an den verwendeten Dateinamen angehängt den Dateinamen einer anzulegenden Sicherheitskopie ergibt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pi</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/foo/bar/g'</span> <span style="color: #000000; font-weight: bold;">*</span>.txt
<span style="color: #c20cb9; font-weight: bold;">perl</span> -pi.orig <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'s/foo/bar/g'</span> datei.txt</pre></div></div>

<p>Beim ersten Befehl jede Text-Datei im aktuellen Verzeichnis direkt geändert. Beim zweiten wird die &#8220;datei.txt&#8221; nach &#8220;datei.txt.orig&#8221; kopiert bevor jedes Vorkommen von &#8220;foo&#8221; durch &#8220;bar&#8221; ersetzt wird. (Man kann natürlich auch mit dem 2. Befehl beliebig viele Dateien bearbeiten, wenn man * benutzt.)</p>
<p><strong>Module</strong><br />
Die Option <code>-M</code> erlaubt es einem beliebige Module einzubinden, die dann im Programm benutzt werden können. So kann man z.B. mit dem folgenden Befehl mit dem Modul LWP::Simple eine Webite laden und den HTML-Code ausgeben:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> -MLWP::Simple <span style="color: #660033;">-e</span> <span style="color: #ff0000;">'getprint(&quot;http://zinformatik.de&quot;)'</span></pre></div></div>

<p>Mit dem folgenden Befehl werden von allen iso Dateien im Verzeichnis MD5-Summen erstellt:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #000000; font-weight: bold;">*</span>.iso <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> -MDigest::MD5 <span style="color: #660033;">-nle</span> <span style="color: #ff0000;">'open(FILE,$_); $d=Digest::MD5-&gt;new; $d-&gt;addfile(FILE); print $d-&gt;hexdigest'</span></pre></div></div>

<p><strong>Fazit</strong><br />
Für kleinere kosmetische Änderungen eines Textes oder einer Ausgabe sowie zum extrahieren von Informationen daraus ist awk meist besser geeignet. Bei komplexeren Aufgabenstellungen ist die Mächtigkeit von perl jedoch vorteilhafter.<br />
Vor allem durch die Möglichkeit der Dateioperationen sollen manche Aktionen beschleunigt werden können:<br />
Statt des Aufrufs:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.bak&quot;</span> <span style="color: #660033;">-exec</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #ff0000;">&quot;{}&quot;</span> \;</pre></div></div>

<p>der für jede gefundene Datei einen neuen Prozess startet um sie zu löschen kann man folgenden Befehl nutzen:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">find</span> <span style="color: #660033;">-name</span> <span style="color: #ff0000;">&quot;*.bak&quot;</span> <span style="color: #660033;">-print</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-nle</span> <span style="color: #ff0000;">'unlink'</span></pre></div></div>

<p>Es ist jedenfalls nicht verkehrt beides zu kennen, ganz nach dem Motto: Für jeden Zweck die richtige Programmiersprache.</p>
<p><strong>[UPDATE]</strong>Mehr informationen liefert der Befehl <code>perldoc perlrun</code> (perldoc muss installiert sein) oder die Seite <a href="http://p3rl.org/perlrun" title="Dokumentation von perlrun" target="_blank" class="liexternal">http://p3rl.org/perlrun</a><strong>[/UPDATE]</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://zinformatik.de/linux/perl-einzeiler-auf-der-konsole/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

