I have a huge file that looks like:
Both of these read each line of the file
filename and print it out with 25 blank spaces prepending it, what is all redirected to a file. You can use either
perl -ne 'print " " x 25 . $_' filename > outfile
perl -pe '$_ = " " x 25 . $_' filename > outfile
The commands are the same for all practical purposes, and both read the file line by line which is what you want if the file is large. The
> outfile redirects the output to a file with that name.
Each of the examples above is a complete command-line program, that is entered and executed in the terminal. The switches
-emeans that everything that is in between the following single quotes,
'...', is to be executed by Perl as a program. It can be any valid Perl code.
However, these one-liner mini programs come with extra conveniences.
-n sets up a loop over the standard input, so a file submitted after the command (
filename above) is read line by line and whatever code is between
'' is executed on each of its lines in turn. So we don't have to open a file and loop over its lines by hand.
-p does the same as
-n with an additional convenience -- it prints the line once it's processed. So if we want each line printed we can use
-p and don't have to say
Each line is read into the default variable
$_, and many facilities in Perl operate on it by default (if no variable is passed to them). For example, to print a line we often just say
$_ but most of the time we don't.
There is quite a bit more, see
Note, provided by
andlrc in a comment -- one can also use the
$" for single white space
perl -pe '$_ = $"x25 . $_' filename > outfile
When an array or an array slice is interpolated into a double-quoted string or a similar context such as /.../ , its elements are separated by this value. Default is a space.