I was having a really bad day and needed a quick solution to create some csv files. And this I needed to do for multiple data kinds and patterns, so I created this small script to do the job for me…
#Number of rows required in the output.
$rows = 100;
#The config and the output file
open (CF_FILE, \”<Config.test\”);
open (OUT_FILE, \”>test.csv\”);
# No need to change anything below this.
$count = 0 ;
#read a line from config file of the type
#also any line starting with # is treated as comment.
# startingvalue incrementvalue
# so you can have any number of fields here and all of them you would be joined with , to write to the output file.
/^#/ && next;
@vars = split(/ /);
$arr[$count] = $vars;
$arr_inc[$count] = $vars;
for ($i=0; $i < $rows; $i++)
$line = join (\”,\”,@arr);
print OUT_FILE \”$line\\n\”;
for ($j=0; $j < $count; $j++)
$arr[$j] += $arr_inc[$j];
| perl -n -e \'print \"$_\" if 1 ... /<regex>/;# This command line will display the output of , from the first line of output, until the first time it sees a pattern matching .
You could specify the regex pattern /^$/ to look for the first blank line,
or /^foobar/ to look for the first line that starts with the word foobar.
by David Winterbottom (codeinthehole.com)
There are couple of other ways you can do so, like using sed, awk.
<command> |sed –quiet \’/svn_/,$ !p\’
This is the third part of a seven-part article on famous Perl one-liners. In this part I will create various one-liners for calculations. See part one for introduction of the series.
Famous Perl one-liners is my attempt to create “perl1line.txt” that is similar to “awk1line.txt” and “sed1line.txt” that have been so popular among Awk and Sed programmers.
The article on famous Perl one-liners will consist of at least seven parts:
- Part I: File spacing.
- Part II: Line numbering.
- Part III: Calculations (this part).
- Part IV: String creation. Array creation.
- Part V: Text conversion and substitution.
- Part VI: Selective printing and deleting of certain lines.
- Part VII: Release of perl1line.txt.
After I’m done explaining all these one-liners, I’ll publish an ebook. Everyone who’s subscribed to my blog will get a free copy! Subscribe now!
The one-liners will make heavy use of Perl special variables. A few years ago I compiled all the Perl special variables in a single file and called it Perl special variable cheat-sheet. Even tho it’s mostly copied out of perldoc perlvar, it’s still handy to have in front of you, so print it.