0

I am using a program called "dsk" to run analyses. I am interested in only a few lines of the analyses, and am using the following to retrieve those lines:

dsk -file <filename> | grep -Ei 'string1|string2|string3'

which gives me something like:

string1                                : 13
string2                                : 46
string3                                : 39

*Edit: Reformatted to be more true to output (not tab limited, with 32 spaces, a colon, and another space in between fields)

However, I would like the output to look something like this:

string1 string2 string3
13      46      39

The answer Glenn provided generates the following, likely because the spaces are causing confusion:

string1 string2 string3
:   :   :

Geode
  • 1

2 Answers2

1

Using instead of

dsk -file "$file" | awk '
    tolower($1) ~ /string1|string2|string3/ { n++; vars[n] = $1; vals[n] = $2 }
    END {
        for (i = 1; i <= n; i++) printf "%s\t", vars[i]
        print ""
        for (i = 1; i <= n; i++) printf "%s\t", vals[i]
        print ""
    }
'
glenn jackman
  • 85,964
0

The general-purpose tools mentioned in the answer linked by @SteelDriver are highly suitable, as is @glenn jackman's answer.

However, if your data is formatted as simply as you indicate -- that is, if there are no spaces in the string1 .. stringN pieces -- then at the expense of reading your input file twice, you can also do this:

dsk -file <filename> | grep -Ei 'string1|string2|string3' > tempfile.txt

readarray labels < <(awk '{print $1}' tempfile.txt)
readarray values < <(awk '{print $2}' tempfile.txt)

printf '%s\t' ${labels[@]}
printf '\n'

printf '%s\t' ${values[@]}
printf '\n'

That formatting may be somewhat simplistic, and in particular might not align the entries as nicely as you might like. But if all you need is tab-delimited output, it will do that.

Jim L.
  • 7,997
  • 1
  • 13
  • 27