Tag Archives: Perl

Perl: Command-line regex

A few command-line tricks for Perl: Filter based on regex (like grep -E) cat file.txt | perl -n -e ‘print if /regex.*matched/’ Replace based on regex cat file.txt | perl -n -e ‘s/regex.*matched/replacement/gi’ Replace based on regex inside file (will change file) perl -pi -e ‘s/regex.*matched/replacement/gi’ *.txt Replace based on regex inside file (will change file) and make… Read More »

Perl: Modify certain line number in text-file

Just wrote up a small pseudocode for a previous post here. [sourcecode type=”php”] open (IF, "infile.txt"); open (OF, "outfile.txt");   while (my $line = <IF>) {     $count++;     if ($count == 10) {       # Modify       print OF "somethingelse\r\n";     } else {       # Don’t modify       print OF $line;     }   } close(OF);… Read More »