Ubuntu – How to print values in a text file to columnated file using shell script

bashcommand linescriptstext processing

I have an output.txt from running a shell script as follows:

abc.txt
errorstatus1
Fri Nov 11 02:00:09 2016
def.txt
errorstatus2.txt
Sat Nov 12 03:00:09 2016

The text file has multiple entries line by line in the same manner. I want to print these values into columns: Filename,Status and Timestamp as follows:

Filename      Status        Timestamp
abc.txt     errorstatus1   Fri Nov 11 02:00:09 2016
def.txt     errorstatus2   Sat Nov 12 03:00:09 2016

Best Answer

With paste:

paste - - - <file.txt

this will output the newline separated file content as columns, and three tab separated columns per line.

Adding the header:

echo Filename Status Timestamp; paste - - - <file.txt

To columnize the output, take help from column:

{ echo Filename Status Timestamp; paste - - - <file.txt ;} | column -t

Example:

% cat file.txt
abc.txt
errorstatus1
Fri Nov 11 02:00:09 2016
def.txt
errorstatus2.txt
Sat Nov 12 03:00:09 2016

% { echo Filename Status Timestamp; paste - - - <file.txt ;} | column -t
Filename  Status            Timestamp
abc.txt   errorstatus1      Fri        Nov  11  02:00:09  2016
def.txt   errorstatus2.txt  Sat        Nov  12  03:00:09  2016