Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I have a file, PoolReport.txt. Here is some sample data:

Thu, Aug 27, 2015 7:56:29 AM
PoolName,Total GB,Used GB ,Available GB ,Percent Full
Oracle-RAID10,6432.539,6179.295,253.244,96.063
VM_File,45210.379,40726.389,4483.991,90.082
Oracle-RAID5,15025.039,12289.707,2735.332,81.795

I need it in the following format. Can somebody help me using shell script?

Thu, Aug 27, 2015 7:56:29 AM
PoolName       Total GB   Used GB    Available GB   Percent Full
Oracle-RAID10  6432.539   6179.295   253.244        96.063
VM_File        45210.379  40726.389  4483.991       90.082
Oracle-RAID5   15025.039  12289.707  2735.332       81.795

With my limited knowledge, I managed to produce a better format (as below), but it does not satisfy the needs.

awk -F "," '{print $1"\t" $2"\t" $3"\t" $4"\t" $5"\t" }' PoolReport.txt 
Thu      Aug 27  2015 7:56:29 AM
PoolName        Total GB         Used GB        Available GB    Percent Full
Oracle-RAID10   6432.539        6179.295        253.244 96.063
VM_File 45210.379       40726.389       4483.991        90.082
Oracle-RAID5    15025.039       12289.707       2735.332        81.795
share|improve this question
    
Wait. You're asking us to ask your question for you? –  MatthewRock Aug 28 at 14:36

2 Answers 2

Change all occurrences of , to {tab}, except on the first line:

sed '2,$s/ *, */\t/g' PoolReport.txt

OR format the fields with tab separators, except on the first line:

awk -F, 'NR==1;NR>1{OFS="\t"; print $1,$2,$3,$4,$5}' PoolReport.txt

Output for either instance:

Thu, Aug 27, 2015 7:56:29 AM
PoolName        Total GB        Used GB Available GB    Percent Full
Oracle-RAID10   6432.539        6179.295        253.244 96.063
VM_File 45210.379       40726.389       4483.991        90.082
 Oracle-RAID5    15025.039       12289.707       2735.332        81.795
share|improve this answer

I'm not sure if I understand your question properly, but I believe you want to align the columns. If that is the case then the command column is handy:

column -s, -t file

Output:

Thu             Aug 27     2015 7:56:29 AM
PoolName       Total GB    Used GB          Available GB   Percent Full
Oracle-RAID10  6432.539   6179.295          253.244        96.063
VM_File        45210.379  40726.389         4483.991       90.082
Oracle-RAID5   15025.039  12289.707         2735.332       81.795

Or, if you want to keep first line as is, then

{ head -n 1; column -s, -t; } <file

Output:

Thu, Aug 27, 2015 7:56:29 AM
PoolName       Total GB    Used GB   Available GB   Percent Full
Oracle-RAID10  6432.539   6179.295   253.244        96.063
VM_File        45210.379  40726.389  4483.991       90.082
Oracle-RAID5   15025.039  12289.707  2735.332       81.795

See man column for possible options you may want to add to tune the output.

share|improve this answer
    
(1) Of course you don’t need the terminal semicolon and the separating spaces if you use parentheses: (head -n 1; something else).  As I’m sure you two (jimmij and Peter.O) know, (command(s)) is functionally equivalent to { command(s); } if the command(s) don’t alter the shell’s state (cd, set variables, etc.).  I believe that the (…) version spawns an extra process, which is resource-wasteful in a trivial way, but I seem to recall hearing otherwise (“the shell needs to fork to run the command(s) either way”).  … (Cont’d) –  Scott Aug 28 at 19:49
    
(Cont’d)  … (2) The original version of the question had blank lines probably because the OP didn’t know how else to get line breaks; it’s unclear whether they ever were present in his data. –  Scott Aug 28 at 19:50
    
@Scott Sure, one can use subshell () as well. –  jimmij Aug 28 at 20:05
    
re: column. Whether or not the OP's input was single or double spaced, the output from column would be the same, as column ignores empty lines. –  Peter.O Aug 28 at 23:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.