[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.
Well ntubski inspired me to do a more straightforward solution too
Code:
ruby -e 'puts "HH MM SS";3.downto(0){ |x| Time.now.strftime("%H|%M|%S|").each_char{ |c| print c == "|"?c:(c.to_i[x]==0?" ":"*")}; puts }'
Can I inspire you to put some newlines in your code too? If I read this correctly you are calling strftime 4 times, not sure if that would cause significant slow down.
Quote:
Originally Posted by w1k0
By the way: are you Window Maker user or you just like to code?
I just like to code, I couldn't pass up the chance to post a solution that's so much simpler. Also, I find this concept of a binary clock rather appealing.
Quote:
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 had in mind just to do a fork()/exec() on the file; this will work for any binary file and scripts with a correct #! line regardless of name.
Now about performance, I was surprised that the ruby solutions did so poorly, so I decided to check things myself. This mostly confirms w1k0's measurements:
Code:
#!/bin/bash
# do-timings.sh
repeats=${1-1}
repeat() {
for _ in $(seq $1) ; do
bash "$2" > /dev/null
done
}
for script in [0-9][0-9]* ; do
TIMEFORMAT=$(printf 'time for %-24.24s: %s' "$script" '%2R real, %2U user, %2S sys, %P%% CPU')
time repeat "$repeats" "$script"
done
Code:
~/tmp/bclock$ ls
00-nothing 18-heredoc-grail
07-rotate1-w1k0 21-arg-grail
08-rotate-multi-PTrenholme 23-no-transpose-grail
_14-transpose-grail 25-bash-PTrenholme
15-gawk-ntubski 29-bash-grail
17-simpler-time-vars-w1k0 do-timings.sh
~/tmp/bclock$ cat 00-nothing
#!/bin/sh
true
~/tmp/bclock$ ./do-timings.sh 20
time for 00-nothing : 0.11 real, 0.03 user, 0.06 sys, 83.78% CPU
time for 07-rotate1-w1k0 : 2.18 real, 0.25 user, 0.52 sys, 35.47% CPU
time for 08-rotate-multi-PTrenhol: 2.20 real, 0.38 user, 0.60 sys, 44.94% CPU
time for 15-gawk-ntubski : 0.23 real, 0.06 user, 0.11 sys, 73.69% CPU
time for 17-simpler-time-vars-w1k: 1.49 real, 0.25 user, 0.66 sys, 60.96% CPU
time for 18-heredoc-grail : 1.38 real, 1.07 user, 0.23 sys, 94.44% CPU
time for 21-arg-grail : 1.38 real, 1.00 user, 0.31 sys, 94.85% CPU
time for 23-no-transpose-grail : 1.37 real, 1.00 user, 0.31 sys, 95.80% CPU
time for 25-bash-PTrenholme : 0.37 real, 0.18 user, 0.07 sys, 68.94% CPU
time for 29-bash-grail : 0.33 real, 0.18 user, 0.07 sys, 77.08% CPU
It looks like the ruby interpreter is not very efficient.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@grail,
Your new script from post #29 has the same performance as PTrenholme’s script from post #25: it uses from 2.0% to 2.0% of CPU (the median is 2.0%). I like it and I’ll attach it as a third version of the vertical 4-bits binary clock to the next release of wminfo together with ntubski’s and PTrenholme’s scripts.
In the real world one binary clock is enough. I’ll attach to wminfo each well designed binary clock because of two reasons: I respect well done work and I admire nice coded programs.
@ntubski,
Quote:
I just like to code, I couldn't pass up the chance to post a solution that's so much simpler. Also, I find this concept of a binary clock rather appealing.
I wondered why so many people decided to code that binary clock. I see two reasons. The first reason is fun. In fact after preparing all useful plugins for wminfo that I could imagine I wrote binary 4-bits vertical clock, binary 6-bits horizontal clock, and hexadecimal clock – all of them just for fun. The second reason is a paradox. On the very basic level the programming is an art of changing the strings of bits into something more intelligible. The binary clock changes something intelligible for all the people into the strings of bits.
Quote:
I had in mind just to do a fork()/exec() on the file; this will work for any binary file and scripts with a correct #! line regardless of name.
It sounds great. You know better than me how to put these things together in a right way.
***
I ran 50 iterations of your wrapper:
./do-timings.sh
Code:
time for 00-nothing : 0.50 real, 0.39 user, 0.09 sys, 96.26% CPU
time for 07-rotate1-w1k0 : 6.24 real, 4.17 user, 1.25 sys, 87.04% CPU
time for 08-rotate-multi-PTrenhol: 6.08 real, 3.99 user, 1.31 sys, 87.24% CPU
time for 15-gawk-ntubski : 0.75 real, 0.71 user, 0.21 sys, 122.79% CPU
time for 17-simpler-time-vars-w1k: 3.69 real, 2.65 user, 0.93 sys, 97.16% CPU
time for 18-heredoc-grail : 1.34 real, 1.02 user, 0.25 sys, 94.50% CPU
time for 21-arg-grail : 1.34 real, 1.06 user, 0.25 sys, 97.99% CPU
time for 23-no-transpose-grail : 1.32 real, 1.01 user, 0.25 sys, 95.73% CPU
time for 25-bash-PTrenholme : 1.23 real, 1.00 user, 0.19 sys, 96.81% CPU
time for 29-bash-grail : 1.30 real, 1.08 user, 0.19 sys, 97.72% CPU
The best scripts #15, #25, and #29 have the shortest real times. So it’s easy to check quickly which script is the best and which is the worst running your wrapper and checking these times. But I have no idea how to interpret the CPU percentages. It seems the higher values correspond to the better performance but it isn’t the strict rule. It works for two worst scripts #07 and #08 as well as for the best script #15. But it’s unimaginable that the script #15 has much better performance than the script #00:
Code:
#!/bin/sh
true
It’s unimaginable also that the scripts from #17 to #29 have the performance similar to the script #00.
But I have no idea how to interpret the CPU percentages. It seems the higher values correspond to the better performance but it isn’t the strict rule.
As explained the bash manual, the CPU percentage is simply (user+sys)/real. The user time is how long the CPU was working for the program, sys time is how long the CPU was working in the kernel on the program's behalf. For script #15 we can see that user+sys time is greater than the real time, we can therefore deduce you have a multicore system. My box only pretends to be multicore via hyperthreading, so you can see in my timings the CPU is waiting a lot more.
Possibly not what you might want to use, but I was looking for something else, and noticed that there is a program (binclock) in the Fedora repository that offers a "ncurses" implementation of a binary clock. You should be able to get that code to work on almost any OS.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@PTrenholme,
Fedora's binclock is funny application but it couldn't work as a wminfo plugin without the extensive modifications. I'm glad having three different vertical binclocks. It would be the waste of the time to code the next one. Anyway thanks for that information.
Can I inspire you to put some newlines in your code too? If I read this correctly you are calling strftime 4 times, not sure if that would cause significant slow down.
The newlines are not a problem, I was trying to get a one liner that the OP would be happy to place in their bash wrapper.
You are also correct on catching the call to strftime being performed 4 times ... which does create an overhead.
Code:
#!/bin/bash
date '+%H|%M|%S|' | ruby -ne 'puts "HH MM SS";
3.downto(0){ # Iterate from 3 to 0 step -1
|x|
$_.each_char{ # Iterate over each character in string $_ (like Perl this is the read line)
|c|
print c == "|"?c:(c.to_i[x]==0?" ":"*") # Print | or if the integer rep array is 0 print <space> else print *
};
puts
}'
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@grail,
Your new Ruby script from post #37 uses from 2.3% to 2.7% of CPU (the median is 2.5%) so it’s worse than your old Ruby script from post #14 which uses from 2.2% to 2.4% of CPU (the median is 2.3%). Of course both of them are worse than your script from post #29 that I added to the next version of wminfo.
I am tempted to write a C program and beat all those attempts by a huge margin ... but I'm just not motivated enough. I mean it's a cool idea, but I doubt I would use it on a daily basis ... or for more than a few minutes.
I believe that some of them could be modified to provide the required output.
Quote:
I am tempted to write a C program and beat all those attempts by a huge margin ... but I'm just not motivated enough. I mean it's a cool idea, but I doubt I would use it on a daily basis ... or for more than a few minutes.
ntubski prepares the wminfo version that will be capable to run binary files. I’m sure there are the people considering the binary clock cool enough to use it all the time.
After writing the binary clock I stated I like that idea very much so I searched e-bay for some binary watches. There’s a lot of so called “binary watches” there unfortunately some of them aren’t binary watches at all (though they display dots) and most of the rest display only hours and minutes in AM/PM format (hours in 4-bits format and minutes in 6-bits format). Moreover they don’t display the time continuously but only for a few seconds after you press the button. I need the binary watch that displays hours, minutes, and seconds in 24-hours format – all of them in 4-bits format and continuously.
Remove comments for continuous output every second.
EDIT:
Code:
bash-4.1$ ./do-timings.sh 100
time for 00-nothing : 0.29 real, 0.12 user, 0.09 sys, 72.49% CPU
time for 07-rotate1-w1k0 : 3.05 real, 0.40 user, 0.73 sys, 37.43% CPU
time for 08-rotate-multi-PTrenhol: 3.07 real, 0.43 user, 0.72 sys, 37.61% CPU
time for 15-gawk-ntubski : 0.45 real, 0.21 user, 0.20 sys, 91.22% CPU
time for 17-simpler-time-vars-w1k: 2.04 real, 0.42 user, 0.76 sys, 57.83% CPU
time for 18-heredoc-grail : 1.23 real, 0.78 user, 0.33 sys, 90.86% CPU
time for 21-arg-grail : 1.14 real, 0.76 user, 0.33 sys, 96.08% CPU
time for 23-no-transpose-grail : 1.13 real, 0.71 user, 0.31 sys, 90.52% CPU
time for 25-bash-PTrenholme : 0.58 real, 0.29 user, 0.12 sys, 71.41% CPU
time for 29-bash-grail : 0.53 real, 0.30 user, 0.11 sys, 77.87% CPU
time for 41-binclock : 0.06 real, 0.00 user, 0.00 sys, 20.56% CPU
Last edited by H_TeXMeX_H; 09-22-2012 at 02:01 PM.
The minor problem is that your clock displays the time upside down. The major problem is that it uses 6-bits format for 3 digits (H, M, S) while it should use 4-bits format for 6 digits (HH, MM, SS). As a result the above output doesn’t fit the wminfo window.
I’m sure you can adjust your program to produce the desirable output format. I suppose tomorrow ntubski will finish to adjust wminfo to accept binary plugins. Then I’ll attach your plugin to wminfo.
Distribution: Slackware (personalized Window Maker), Mint (customized MATE)
Posts: 1,309
Original Poster
Rep:
@PTrenholme,
It works perfect with the preliminary version of the wminfo accepting binary plugins. At the moment I can't test its performance using my method because that preliminary version of wminfo has some problems concerning performance.
Most of the code is yours so I'm not sure whether it's H_TeXMeX_H's plugin modified by you or it's yours plugin.
@PTrenholme,
...
Most of the code is yours so I'm not sure whether it's H_TeXMeX_H's plugin modified by you or it's yours plugin.
I started from his posted code and changed it 'till I got output that looked more like your specification. What H_TeXMeX_H's code seemed to be doing was trying to write the HH|MM|SS values as six-binary-digit numbers. Somewhat harder to "read," but a "true" binary clock. (Being 72 years old, I'm not mentally flexible enough to understand binary notation as "intuitively" as decimal. )
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.