Shell Script – Generate Output of File in the Next Line

filesshellshell-script

I have 3 files ex- abc.txt, def.txt & xyz.txt. I've generate one shell script where I want to content of these 3 files should appear line wise like as below-

abc ---- 1  2  3  4  5  6  7
def ---- 3  5  7  9 11 13 15
xyz ---- 4  8 12 16 20 24 28

Also the content of the files should be like this manner & every value in parallel wise. Could you please help me to generate the shell script.
But in actual when i run script then it is showing like-

abc ---- 1 2 3 4 5 6 7
def ---- 3 5 7 9 11 13 15
xyz ---- 4 8 12 16 20 24 28

So I want to arrange like above content of the output. Where I used in the script for the above requirement-

 for f in abc.txt def.txt xyz.txt; do (cat "${f}"; echo) >> output.txt; done
|awk '{printf "%-10s|%-10s|%-10s|%-10s|%-10s|%-10s|%-10s\n",$1,$2,$3,$4,$5,$6,$7} output.txt > final_output.txt

Is it good approach to generate the script

Best Answer

You can use the columns command

./your_script |  column -t -s' ' 

This will be close to your desired output but the numbers would not be right-justified

abc  ----  1  2  3   4   5   6   7
def  ----  3  5  7   9   11  13  15
xyz  ----  4  8  12  16  20  24  28

To right justify the numbers just pipe the output of your shell script to rev and columns command like this

./your_script | rev | column -t -s' '  | rev
  • The first rev will reverse every line
  • Then the columns command will create columns with space as the delimiter
  • Finally , the last rev will revert back the original line - giving you right aligned numbers.
Related Question