predictive predictive -5 years ago 132
Bash Question

Adding blank cells in huge file

I have a huge file that looks like:


I want to add 25 blank cells that way:


I know that there is a way to do that via awk or sed...

Answer Source

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

  • -e means 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 print. The second example above uses this.

  • 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 print. Here each line needs to be manipulated so I explicitly use $_ but most of the time we don't.

There is quite a bit more, see perlrun

Note, provided by andlrc in a comment -- one can also use the$" for single white space

perl -pe '$_ = $"x25 . $_' filename  > outfile

From Special Variables in perlrun

  • $"
    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.
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download