As always in Unix, there are options.
paste
Use the paste
utility with a blank LHS file, for example:
cat ~/.bashrc | paste /dev/null -
The cat
command is a placeholder for your second script.
The paste
command is designed to take two files and put them together, e.g.:
$ paste file1 file2
file 1 line 1 <TAB> file 2 line 1
file 1 line two <TAB> file 2 line 2
file 1 line 3 <TAB> file 2 line iii
The way I'm using it above is to use /dev/null
as file1
, and STDIN
as file2
, specified by -
. When used as input, /dev/null
returns NULL characters. This means every line of file2
, the output of your second script, is preceded by NULL followed by a TAB character.
You can go further: paste
has a --delimiter
option, but specifying two spaces doesn't give the expected effect: delimiter 1 is used between the first and second columns, delimiter 2 is used between the second and third, and so on.
paste|expand
To get a two-space indent, you would use the plain paste
piped again through expand -2
: this turns all tabs into two spaces:
cat ~/.bashrc | paste /dev/null - | expand -2
This will behave exactly as you specified.
sed
or awk
Yet another approach is to use sed
or awk
:
cat ~/.bashrc | sed 's/^/ /'
This searches for the beginning of line ("^
"), and substitutes, or really inserts a pair of spaces.
cat ~/.bashrc | awk '{printf " %s\n",$0}'
This takes each full line ("$0
") and formats it with printf
, using the format specifier of two spaces, followed by the string to be printed, followed by a newline.
Bear in mind all of the above commands can eliminate the cat
piece of the pipeline, i.e. paste /dev/null ~/.bashrc
, or paste /dev/null ~/.bashrc|expand -2
, equally sed 's/^/ /' ~/.bashrc
or awk '{printf " %s\n",$0}' ~/.bashrc
. It's often taken as a beginner error to use cat
first in a pipeline.