0

I'm attempting to create a report that breaks down the different dependency files from an original source file, then output that in a report with columns. Thus far, I can get the file lists to generate, but the fourth column is the only column with multiple values and this is what I get:

file='foo.rcf'
gen_file='foo2.rcf'
fs_file='foo3.rcf'
item_file='foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf'

paste <(printf %s "$file |") <(printf %s "$fs_file |") <(printf %s "$gen_file |") <(printf "%10s\n""$item_file")

Output:

foo.rcf |       foo3.rcf |      foo2.rcf |      foo4.rcf
                        foo5.rcf
                        foo6.rcf
                        foo7.rcf

What I'm hoping to be able to output is something along these lines:

foo.rcf |       foo3.rcf |      foo2.rcf |      foo4.rcf
        |                |               |      foo5.rcf
        |                |               |      foo6.rcf
        |                |               |      foo7.rcf

A bash solution would be awesome, but I'm really looking for any solution at this point.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
alvar047
  • 1
  • 1
  • Take a look at the, you might have guessed it, `column` utility. http://man7.org/linux/man-pages/man1/column.1.html – Christoph John Feb 25 '20 at 17:34
  • Does this answer your question? [How can I format the output of a bash command in neat columns](https://stackoverflow.com/questions/6462894/how-can-i-format-the-output-of-a-bash-command-in-neat-columns) – solid.py Feb 25 '20 at 17:45

2 Answers2

1

Use column. First with paste join the lines using some separator and then columnize the output with column with fields separated by that separator:

file='foo.rcf'
gen_file='foo2.rcf'
fs_file='foo3.rcf'
item_file='foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf'

paste -d'|' <(
    printf %s "$file") <(
    printf %s "$fs_file") <(
    printf %s "$gen_file") <(
    printf %s "$item_file") |
column -t -s '|' -o ' |     '

would output:

foo.rcf |     foo3.rcf |     foo2.rcf |     foo4.rcf
        |              |              |     foo5.rcf
        |              |              |     foo6.rcf
        |              |              |     foo7.rcf
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • This worked! Thanks! Was able to simply put in functions instead of file names, with the same results – alvar047 Feb 25 '20 at 21:14
0

You can use awk:

file='foo.rcf'
gen_file='foo2.rcf'
fs_file='foo3.rcf'
item_file='foo4.rcf
foo5.rcf
foo6.rcf
foo7.rcf'

# Lets concatenate the string with separators:
printf "$file|$fs_file|$gen_file|$item_file" |
# Sending modified string to awk with pipe---^
# awk can split it with -F parameter and insert tabs between the chunks.
awk -F'|' 'NR==1 {
    print $1"\t\t|\t"$2"\t|\t"$3"\t|\t"$4
} NR>1 { 
    print "\t\t|\t\t\t|\t\t\t|\t"$1  }'
# -F defines a separator, NR is line number.

Output:

foo.rcf     |   foo3.rcf    |   foo2.rcf    |   foo4.rcf
            |               |               |   foo5.rcf
            |               |               |   foo6.rcf
            |               |               |   foo7.rcf
pegasuspect
  • 991
  • 4
  • 15