Reputation: 3177
I am pretty confused.
I have a value output like:
jstat -gc 7110
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
52416.0 52416.0 178.1 0.0 419456.0 261407.3 524288.0 30663.4 50272.0 30745.7 898 10.393 6 0.243 10.636
I have already retrieved each of the key and value but just want to display the results like:
SOC : 52416.0
S1C : 52416.0
Help!! My code:
jstat -gc 7110 | tr -s '' | cut -d '' -f 1,14 | while read line
do
#echo $line
lineinfo=(`echo $line | tr -s '' | cut -d '' -f 1,15`)
for (( i=0; i<${#lineinfo[@]}; i++ ))
do
echo "$i" : ${lineinfo[i]}
printf "%s\n"
done
done
Upvotes: 0
Views: 110
Reputation: 37404
tr
twice and pr
for output control:
$ tr -s \ '\n' file | pr -2 -t -s" : "
S0C : 52416.0
S1C : 52416.0
S0U : 178.1
S1U : 0.0
EC : 419456.0
EU : 261407.3
OC : 524288.0
OU : 30663.4
PC : 50272.0
PU : 30745.7
YGC : 898
YGCT : 10.393
FGC : 6
FGCT : 0.243
GCT : 10.636
Or awk
and pr
:
$ awk '$1=$1' OFS="\n" file |pr -2 -t -s" : "
ie. $1=$1
rebuilds the record and OFS="\n"
changes the output field separator to newline. pr
makes sweet columns.
Upvotes: 0
Reputation: 18371
awk -v OFS=: 'NR==1 {for(i=1;i<=NF;i++) a[i]=$i;next} {for(i=1;i<=NF;i++) print a[i],$i}' infile
S0C:52416.0
S1C:52416.0
S0U:178.1
S1U:0.0
EC:419456.0
EU:261407.3
OC:524288.0
OU:30663.4
PC:50272.0
PU:30745.7
YGC:898
YGCT:10.393
FGC:6
FGCT:0.243
GCT:10.636
Upvotes: 1
Reputation: 74605
If the columns match up perfectly, you could do something like this with awk:
awk -v OFS=' : ' '{ for (i = 1; i <= NF; ++i) if (NR == 1) key[i] = $i; else val[i] = $i }
END { for (i = 1; i <= NF; ++i) print key[i], val[i] }' file
Loop through the fields on each line, assigning keys and values to separate arrays. After reading both lines, print the combined output.
print
inserts the Output Field Separator OFS
between each field, so set it to ' : '
to get your desired output.
Upvotes: 1
Reputation: 23667
$ cat ip.txt
S0C S1C S0U S1U EC EU OC OU PC PU YGC YGCT FGC FGCT GCT
52416.0 52416.0 178.1 0.0 419456.0 261407.3 524288.0 30663.4 50272.0 30745.7 898 10.393 6 0.243 10.636
$ sed -E 's/\s+/\n/g' ip.txt | pr -2ts' : '
S0C : 52416.0
S1C : 52416.0
S0U : 178.1
S1U : 0.0
EC : 419456.0
EU : 261407.3
OC : 524288.0
OU : 30663.4
PC : 50272.0
PU : 30745.7
YGC : 898
YGCT : 10.393
FGC : 6
FGCT : 0.243
GCT : 10.636
Using sed
to replace white-spaces with newline, then using pr
to style the output
Upvotes: 1