Differences between version 26 and previous revision of PerlOneLiners.
Other diffs: Previous Major Revision, Previous Author, or view the Annotated Edit History
Newer page: | version 26 | Last edited on Wednesday, August 24, 2005 4:38:55 pm | by JohnMcPherson | Revert |
Older page: | version 25 | Last edited on Thursday, May 19, 2005 5:56:59 pm | by AristotlePagaltzis | Revert |
@@ -42,8 +42,17 @@
perl -pe 's!\\n!\n!g; s!\\t!\t!g' $file
</verbatim>
Note that you can use any punctuation as the separator in an <tt>s///</tt> command, and if you have backslashes or even need literal slashes in your pattern then doing this can increase clarity.
+
+!! Convert data from rows to columns
+This assumes that each of the input rows is exactly the same length (in terms of number of items), and assumes they are separated by spaces. This is useful if you have data in tabular form, but need it to be in columns instead, (eg you want to use it as input to GnuPlot).
+
+<verbatim>
+perl -e '@rows=();while ($l=<>) {@line=split(/\s+/,$l); push @rows, [@line]}
+ for $i (0 .. @{$rows[0]}) { for $row (@rows) {print $row->[$i] . "\t"} print "\n"}'
+</verbatim>
+This will read from stdin(3) and write to stdout(3).
!! Trace execution in a [Perl] script
Getting a trace showing each executed line of code in sequence (think <tt>sh -x</tt> but for [Perl] scripts) is not obvious. perl(1)'s <tt>-D</tt> switch itself does not provide such functionality, but you can get there like so: