[SOLVED] How to convert the rows of data into the columns?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@grail,
Your script is very nice. Unfortunately it doesn’t work as a wminfo plugin. The wminfo program uses bash to run the plugins and bash doesn’t understand the Ruby commands.
@ntubski,
Your script is really impressing. I was sure it’s possible to code the binary clock using AWK (gawk) in a more elegant way but I didn’t suppose the code could be so compact.
***
The last version of my script using the better method to get the time-related variables is here:
Code:
#!/bin/bash
dectobin() {
for i in $*
do
echo "$i" | perl -e "printf(\"%04b\n\", <STDIN>)"
done
}
rotate1()
{
for (( i=0 ; i<${#1} ; i++ )); do
printf '%s%s%s%s%s%s\n' "${1:i:1}" "${2:i:1}" "${3:i:1}" "${4:i:1}" "${5:i:1}" "${6:i:1}"
done
}
string="$(date +" Hlr=%H Mlr=%M Slr=%S")"
$(echo "$string" | sed -E "s/ / export /g;s/lr/l/g;s/=(.)./=\1/g")
$(echo "$string" | sed -E "s/ / export /g;s/lr/r/g;s/=.(.)/=\1/g")
echo "HH MM SS"
rotate1 $(dectobin $(echo -e "$Hl\n$Hr\n$Ml\n$Mr\n$Sl\n$Sr")) | sed -E "s/0/ /g;s/1/*/g;s/(..)(..)(..)/\1|\2|\3|/"
The above script uses the rotate1 function by David the H.
The CPU usage for the above script using the rotate0 function that I found in Internet is 4.1% – 4.7% (median 4.3%).
The CPU usage for the above script using the rotate2 function by danielbmartin is 4.2% – 4.5% (median 4.3%).
The CPU usage for the above script using the rotate3 function by PTrenholme is 4.0% – 4.4% (median 4.0%).
The CPU usage for the above script using the rotate1 function by David the H. is 3.7% – 4.3% (median 4.0%).
The CPU usage for the script by ntubski from post #15 is 1.8% – 2.0% (median 2.0%).
***
Your solution is really great, ntubski. Thank you very much. I could learn AWK for a long time but I never reach you mastery.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@ntubski,
The sentence from man wminfo you quoted is one of a few sentences which I borrowed from the documentation written by Robert Kling – the author of the wminfo 1.51. In fact I have no idea what he had in mind writing that so it seems it’s the time to remove that statement from the documentation.
Thank you very much for the tricky method that allows to run different scripts with wminfo.
@grail,
I tested the Ruby script using ntubski’s method. The CPU usage for your script from post #14 is 2.2% – 2.4 (median 2.3%). So its performance is slightly worse than the performance of ntubski’s script from post #15 though still impressing.
@all of you,
It was very interesting and instructing discussion. Thank you very much for your participation once again.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
Quote:
Originally Posted by H_TeXMeX_H
Maybe writing them in C would be better ? I mean performance would be better.
As a plugin for wminfo it wouldn't be better because that program doesn't cope with the plugins other than the shell scripts. (See also ntubski's trick which allows to run some other scripts as well.)
The sentence from man wminfo you quoted is one of a few sentences which I borrowed from the documentation written by Robert Kling – the author of the wminfo 1.51. In fact I have no idea what he had in mind writing that so it seems it’s the time to remove that statement from the documentation.
I guess that sentence is aspirational rather than descriptive. I don't think it would be that hard to implement though. I gather that you're the current maintainer of wminfo, yes? Would you accept some patches?
Quote:
Originally Posted by w1k0
Quote:
Originally Posted by H_TeXMeX_H
Maybe writing them in C would be better ? I mean performance would be better.
As a plugin for wminfo it wouldn't be better because that program doesn't cope with the plugins other than the shell scripts.
I expect translating my solution to C would give better performance even with the penalty of a bash wrapper script. Also, I don't think measuring %CPU usage is very accurate, the earlier method of timing multiple runs seems like it would give more reliable results.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@grail,
I tested your script from post #23. The CPU usage for it is 2.2% – 2.4% (median 2.3%). So it’s exactly the same as the CPU usage of the first version. As for the script from post #21 I couldn’t run it with wminfo but I don’t expect that the results for it will be different.
@ntubski,
In post #5 I reported the results of the 25,000 runs of two different rotate functions alone. The first function worked 1m5.928s and used 45.5% of CPU – the second function worked 3m27.683s and used 41.0% of CPU.
In post #17 I reported the results for the complete plugins ran by wminfo. The plugin using the first function used 4.0% of CPU and the plugin using the second function used 4.3% of CPU.
Now I tested these two plugins running 1500 iterations and sending the output to /dev/null. The first script worked 1m51.615s and used 39.9% of CPU – the second script worked 1m52.213s and used 43.0% of CPU.
As you can see these last values of the CPU usage are ten times higher than the results for the real CPU usage from post #17. So both these methods of testing the wminfo plugins are equally good.
But the result such as 39.9% can only scary the user. The result such as 4.0% tells the user how high is the real CPU usage of both wminfo and the plugin using the first rotate function.
***
I’m the maintainer of wminfo since the version 2.00 released in August, 2011. Robert Kling – the author of the original wmInfo – stopped to develop it releasing the version 1.51 in August, 2000. All the official versions of the program are here.
I’ll be more than happy seeing some patches for wminfo or the plugins as well as the new plugins if you have some interesting ideas. Yesterday I added to wminfo the THANKS file to thank you for your version of the binary clock. Depending on your future participation in that project I’ll add the further thanks to that file or I’ll add you to the list of the authors of the program.
I don’t think the binary plugins are good solution especially started by bash wrapper script though you could try to convince me. Or maybe you could modify the wminfo to run binary plugins using some special switch (for example: -w for wrapper). (Not so good idea so see the post #28 for the better one.)
As for the improvements of wminfo and the plugins I did a lot to reduce the CPU power demands of the program and I prepared different versions of the plugins in order to lower the CPU usage. Taking into consideration your abilities I’m sure you could improve these plugins much more.
Now I prepare wminfo 3.1.0. For your convenience I put the pre-release here: http://linux-bsd-unix.strefa.pl/wminfo-pre3.1.0.tar.gz. I’ll update that file at the end of each day assuming I’ll made some changes in it.
By the way: are you Window Maker user or you just like to code? I use Window Maker exclusively since 1998. I published my desktop here: This is my Slackware desktop... – my current desktop is similar to the third screenshot from the first post. In the past I used a lot of different dockable applications. Now I use wminfo only. It can replace all useful dockable applications providing more information in most cases.
@all of you,
Is it the most active solved thread ever?
Last edited by w1k0; 09-20-2012 at 09:17 PM.
Reason: bad idea – good idea
Stung by the "Frankenstein code" comment by H_TexMex_H , here's an "all bash" version:
Code:
#!/bin/bash
date=$(date '+%H|%M|%S')
Hl=${date:0:1}
Hr=${date:1:1}
Ml=${date:3:1}
Mr=${date:4:1}
Sl=${date:6:1}
Sr=${date:7:1}
dectobin() {
local dec bin i
for i in $*
do
dec=$((${i} + 0))
bin=""
while [ ${dec} -gt 0 ]
do
digit=$((dec % 2))
dec=$((dec / 2))
bin=${digit}${bin}
done
bin="0000${bin}"
bin=${bin: -4}
printf -v ${i} "%s" ${bin}
done
}
rotate()
{
local i j k max
max=0
for i in $*
do
[ ${max} -lt ${#i} ] && max=${#i}
done
for (( i=0 ; i<${max} ; i++ ))
do
k=0
for j in $*
do
if [ ${j:${i}:1} == "1" ]
then
printf '*'
else
printf ' '
fi
[ $((k++ % 2)) -eq 1 ] && printf '|'
done
printf '\n'
done
}
dectobin Hl Hr Ml Mr Sl Sr
echo "${date}"
echo "HH|MM|SS"
rotate $Hl $Hr $Ml $Mr $Sl $Sr
(Note that I combined the output and rotate in one function. I seemed to me that printing the rotated binary values and then using perl to format and print them again was, um, somewhat Frankenstein-ish.
Last edited by PTrenholme; 09-20-2012 at 05:00 PM.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@PTrenholme,
H_TexMex_H addressed the “Frankenstein code” comment to you but in fact that code was in part mine and in part yours so we made that monster together. And my original script from post #7 was “Frankensteinish” as well assuming that adjective means a combination of different elements (or languages).
I’ll test the performance of your script tomorrow though I don’t expect the spectacular effects in the comparison to ntubski’s script from post #15.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@PTrenholme,
I was too curious to wait a few hours for the results so I tested your new script now. It uses from 2.0% to 2.0% of CPU (the median is 2.0%). Taking into consideration the median your script has the same performance as the ntubski’s script. It’s also very stable because it uses the same percentage of the CPU power all the time. Your method to get the time-related variables is better than my new method from the script #17.
I like your new script very much so I’ll attach it to the next release of wminfo together with the ntubski’s script.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@ntubski,
After reconsidering the problem which I discussed in post #24 I think that instead of the special switch for the wrapper the better idea is to learn wminfo to distinguish the type of the plugin on the basis of the extension: .wmi or .sh is a shell script, .bin is a binary file, .awk is an AWK script, .pl is a Perl script, .rb is a Ruby script etc.
I assume you could modify wminfo to execute binaries as well as AWK, Perl, and Ruby scripts without the shell wrapper script.
Last edited by w1k0; 09-20-2012 at 09:30 PM.
Reason: assumption
Well it would appear I am not currently knowledgeable enough about Ruby to get any improvement in this situation ... but I think I can improve on the bash:
Code:
#!/bin/bash
current_time=$(date '+%H|%M|%S|')
echo $current_time
echo "HH MM SS"
for (( bin = 8; bin >= 1; bin/=2 ))
do
for (( cnt=0; cnt < ${#current_time}; cnt++ ))
do
num=${current_time:cnt:1}
if [[ $num == '|' ]]
then
echo -n $num
continue
fi
if (( num & bin ))
then
echo -n '*'
else
echo -n ' '
fi
done
echo
done
Now I have the next question. The output of the script is:
011
110
101
100
but I need to present the data in the other way.
Instead of “0” I need “A” and instead of “1” I need “B”.
I tried to do this by modifying my awk several ways, without success.
This is an example of what didn't work.
Code:
VAR1="0111"
VAR2="1100"
VAR3="1010"
echo -e "$VAR1\n$VAR2\n$VAR3" \
|awk -F "" '{for (rownum=1; rownum<=NF; i++) \
a[rownum]=a[rownum]substr("AB",1+$i,1)} \
END {i=1; while (i in a) {print a[i]; i++;}}'
Please, expert awkers, show how this ought to be done.
Thank you.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.