Printing modified data
⇐ Back to One Liners
Questions, comments, or corrections? Send me a message.
Beside the -n switch mentioned in the looping data tip, there is a -p switch which is equivalent to:
while(><){
# program
} continue {
print;
}
So you're -e stuff goes in the middle. This can be leveraged to print some modified data using any perl expressions. For example:
perl -pe 's/(\d+)/$1=chr($1)/e' myfile.txtWould take a file called myfile.txt that contained ascii codes separated by newlines and print NUM=CHARACTER. It is functionally equivalent to:
while(<>){
s/(\d+)/$1=chr($1)/e;
} continue {
print;
}
Which might be easier to understand if you're new to perl as:
open( IN, $ARGV[0] );
while( $line = <IN> ) {
$line =~ s/(\d+)/$1=chr($1)/e;
print $line;
}
close( IN );
Again, more perl runtime stuff at the perlrun manpage.