Question:
8 bits representing the number 7 look like this:

00000111

Three bits are set.

What are the algorithms to determine the number of set bits in a 32-bit integer?

Answer:
This is known as the '
Hamming Weight
', 'popcount' or 'sideways addition'.
Some CPUs have a single built-in instruction to do it and others have parallel instructions which act on bit vectors.  Instructions like x86's
```
popcnt
```
(on CPUs where it's supported) will almost certainly be fastest for a single integer.  Some other architectures may have a slow instruction implemented with a microcoded loop that tests a bit per cycle (
citation needed
- hardware popcount is normally fast if it exists at all.).
The 'best' algorithm really depends on which CPU you are on and what your usage pattern is.
Your compiler may know how to do something that's good for the specific CPU you're compiling for, e.g.
C++20
```
std::popcount()
```
, or C++
```
std::bitset<32>::count()
```
, as a portable way to access builtin / intrinsic functions (see
another answer
on this question).  But your compiler's choice of fallback for target CPUs that don't have hardware popcnt might not be optimal for your use-case.  Or your language (e.g. C) might not expose any portable function that could use a CPU-specific popcount when there is one.
Portable algorithms that don't need (or benefit from) any HW support
A pre-populated table lookup method can be very fast if your CPU has a large cache and you are doing lots of these operations in a tight loop. However it can suffer because of the expense of a 'cache miss', where the CPU has to fetch some of the table from main memory.  (Look up each byte separately to keep the table small.)  If you want popcount for a contiguous range of numbers, only the low byte is changing for groups of 256 numbers,
making this very good
.
If you know that your bytes will be mostly 0's or mostly 1's then there are efficient algorithms for these scenarios, e.g. clearing the lowest set with a bithack in a loop until it becomes zero.
I believe a very good general purpose algorithm is the following, known as 'parallel' or 'variable-precision SWAR algorithm'. I have expressed this in a C-like pseudo language, you may need to adjust it to work for a particular language (e.g. using uint32_t for C++ and >>> in Java):
GCC10 and clang 10.0 can recognize this pattern / idiom and compile it to a hardware popcnt or equivalent instruction when available, giving you the best of both worlds. (
https://godbolt.org/z/qGdh1dvKK
)
```
```
int numberOfSetBits(uint32_t i)
{
     // Java: use int, and use >>> instead of >>. Or use Integer.bitCount()
     // C or C++: use uint32_t
     i = i - ((i >> 1) & 0x55555555);        // add pairs of bits
     i = (i & 0x33333333) + ((i >> 2) & 0x33333333);  // quads
     i = (i + (i >> 4)) & 0x0F0F0F0F;        // groups of 8
     return (i * 0x01010101) >> 24;          // horizontal sum of bytes
}
```
```
For JavaScript:
coerce to integer
with
```
|0
```
for performance: change the first line to
```
i = (i|0) - ((i >> 1) & 0x55555555);
```
This has the best worst-case behaviour of any of the algorithms discussed, so will efficiently deal with any usage pattern or values you throw at it.  (Its performance is not data-dependent on normal CPUs where all integer operations including multiply are constant-time.  It doesn't get any faster with "simple" inputs, but it's still pretty decent.)
References:
https://graphics.stanford.edu/~seander/bithacks.html
https://catonmat.net/low-level-bit-hacks
for bithack basics, like how subtracting 1 flips contiguous zeros.
https://en.wikipedia.org/wiki/Hamming_weight
http://gurmeet.net/puzzles/fast-bit-counting-routines/
http://aggregate.ee.engr.uky.edu/MAGIC/#Population%20Count%20(Ones%20Count)
How this SWAR bithack works:
```
```
i = i - ((i >> 1) & 0x55555555);
```
```
The first step is an optimized version of masking to isolate the odd / even bits, shifting to line them up, and adding.  This effectively does 16 separate additions in 2-bit accumulators (
SWAR = SIMD Within A Register
).  Like
```
(i & 0x55555555) + ((i>>1) & 0x55555555)
```
.
The next step takes the odd/even eight of those 16x 2-bit accumulators and adds again, producing 8x 4-bit sums.  The
```
i - ...
```
optimization isn't possible this time so it does just mask before / after shifting.  Using the same
```
0x33...
```
constant both times instead of
```
0xccc...
```
before shifting is a good thing when compiling for ISAs that need to construct 32-bit constants in registers separately.
The final shift-and-add step of
```
(i + (i >> 4)) & 0x0F0F0F0F
```
widens to 4x 8-bit accumulators.  It masks
after
adding instead of before, because the maximum value in any 4-bit accumulator is
```
4
```
, if all 4 bits of the corresponding input bits were set.  4+4 = 8 which still fits in 4 bits, so carry between nibble elements is impossible in
```
i + (i >> 4)
```
.
So far this is just fairly normal SIMD using SWAR techniques with a few clever optimizations.  Continuing on with the same pattern for 2 more steps can widen to 2x 16-bit then 1x 32-bit counts.  But there is a more efficient way on machines with fast hardware multiply:
Once we have few enough "elements",
a multiply with a magic constant can sum all the elements into the top element
.  In this case byte elements.  Multiply is done by left-shifting and adding, so
a multiply of
```
x * 0x01010101
```
results in
```
x + (x<<8) + (x<<16) + (x<<24)
```
.
Our 8-bit elements are wide enough (and holding small enough counts) that this doesn't produce carry
into
that top 8 bits.
A 64-bit version of this
can do 8x 8-bit elements in a 64-bit integer with a 0x0101010101010101 multiplier, and extract the high byte with
```
>>56
```
.  So it doesn't take any extra steps, just wider constants.  This is what GCC uses for
```
__builtin_popcountll
```
on x86 systems when the hardware
```
popcnt
```
instruction isn't enabled.  If you can use builtins or intrinsics for this, do so to give the compiler a chance to do target-specific optimizations.
With full SIMD for wider vectors (e.g. counting a whole array)
This bitwise-SWAR algorithm could parallelize to be done in multiple vector elements at once, instead of in a single integer register, for a speedup on CPUs with SIMD but no usable popcount instruction.  (e.g. x86-64 code that has to run on any CPU, not just Nehalem or later.)
However, the best way to use vector instructions for popcount is usually by using a variable-shuffle to do a table-lookup for 4 bits at a time of each byte in parallel.  (The 4 bits index a 16 entry table held in a vector register).
On Intel CPUs, the hardware 64bit popcnt instruction can outperform an
SSSE3
```
PSHUFB
```
bit-parallel implementation
by about a factor of 2, but only
if your compiler gets it just right
.  Otherwise SSE can come out significantly ahead.  Newer compiler versions are aware of the
popcnt false dependency
problem on Intel
.
https://github.com/WojciechMula/sse-popcount
state-of-the-art x86 SIMD popcount for SSSE3, AVX2, AVX512BW, AVX512VBMI, or AVX512 VPOPCNT.  Using Harley-Seal across vectors to defer popcount within an element.  (Also ARM NEON)
Counting 1 bits (population count) on large data using AVX-512 or AVX-2
related:
https://github.com/mklarqvist/positional-popcount
- separate counts for each bit-position of multiple 8, 16, 32, or 64-bit integers.  (Again, x86 SIMD including AVX-512 which is really good at this, with
```
vpternlogd
```
making Harley-Seal
very
good.)


Question:
Ok, I get the basics of video format - there are some container formats and then you have core video/audio formats. I would like to write a web based application that determines what video/audio codec a file is using.

How best can I programmatically determine a video codec? Would it be best to use a standard library via system calls and parse its output? (eg ffmpeg, transcode, etc?)

Answer:
```
mplayer -identify
```
will do the trick. Just calling
```
ffmpeg
```
on a file will also work--it will automatically print a set of info at the start about the input file regardless of what you're telling ffmpeg to actually do.
Of course, if you want to do it from your program without an exec call to an external program, you can just include the avcodec libraries and run its own identify routine directly.
While you could implement your own detection, it will surely be inferior to existing routines given the absolutely enormous number of formats that libav* supports.  And it would be a rather silly case of reinventing the wheel.
Linux's "file" command can also do the trick, but the amount of data it prints out depends on the video format.  For example, on AVI it gives all sorts of data about resolution, FOURCC, fps, etc, while for an MKV file it just says "Matroska data," telling you nothing about the internals, or even the video and audio formats used.


Question:
I followed this tutorial on configuring the Rails plugin ExceptionNotifier.

I know that I have ActionMailer configured correctly because I am getting mail from other forms. I even have local_addresses.clear set so that it should be delivering mail no matter what.

I am using Apache with a mongrel_cluster running in the backend.

What am I missing?

Answer:
You're using the SVN version of the plugin, which is probably unmaintained.
Latest version can be found here
.
Second thing which you can do is check the production log. Mailings get written to the log, so you'll see if Rails ever even
tried
to send it. If there are no entries, that means things are silently failing, which probably happens because -- for some reason -- exceptions are not caught properly.


Question: Where can I find a complete reference of the ncurses C API?

Answer:
You can buy this book. I have it and recommend: John Strang, Programming with curses, O'Reilly, ISBN 0-937175-02-1
The best online source information:
http://invisible-island.net/ncurses/ncurses-intro.html
I learned a lot about ncurses reading the minicom source code and the iptraf linux network monitor.


Question:
I want to download this open source application, and they are using Git.  What do I need to download the code base?

Update
How do I change the working directory when I am using Git Bash?  (I want to download the repo at a certain directory, using pwd tells me I will be downloading the repo where I don't want it.

Answer:
Download
Git on Msys
. Then:
```
```
git clone git://project.url.here
```
```


Question: As the title says, how can I find the current operating system in python?

Answer:
https://docs.python.org/library/os.html
To complement Greg's post, if you're on a posix system, which includes MacOS, Linux, Unix, etc. you can use os.uname() to get a better feel for what kind of system it is.


Question:
I'm developing a performance critical application for Intel Atom processor.

What are the best gcc optimization flags for this CPU?

Answer:
There is a cool framework called
Acovea (Analysis of Compiler Options via Evolutionary Algorithm)
, by Scott Rober Ladd, one of the GCC hackers. It's a genetic/evolutionary algorithm framework that tries to optimize GCC optimization flags for a specific piece of code via natural selection.
It works something like this: you write a little piece of benchmark code (it
really
has to be little, because it will be re-compiled and executed several thousand times) that represents the performance characteristics of the larger program you want to optimize. Then Acovea randomly constructs some dozens of different GCC commandlines and compiles and runs your benchmark with each of them. The best of these commandlines are then allowed to "mate" and "breed" new "children" which (hopefully) inherit the best "genes" from their "parents". This process is repeated for a couple dozen "generations", until a stable set of commandline flags emerges.


Question:
I want to create some text in a canvas:

myText = self.canvas.create_text(5, 5, anchor=NW, text="TEST")

Now how do I find the width and height of myText?

Answer:
```
```
bounds = self.canvas.bbox(myText)  # returns a tuple like (x1, y1, x2, y2)
width = bounds[2] - bounds[0]
height = bounds[3] - bounds[1]
```
```
See the
TkInter reference
.


Question:
Whenever I indent HTML in PHP mode, emacs (22.1.1, basic install on Redaht Linux over Putty-SSH) pops up a frame and tells me to get MUMODE or somesuch extra add-on.  I installed PHP Mode without a big hassle, but I don't know how to get this multi-mode rolling.

I'd like to know 2 things

How to install and configure multi-mode
How to disable pop-ups in Emacs

Answer:
If you're running emacs 22, you should just be able to run:
```
```
M-x nxhtml-mumamo
```
```
when editing an html document.  You might want to add it to your auto-mode-alist to get it to automatically load for html docs.  See here for more info:
http://www.emacswiki.org/cgi-bin/wiki/MuMaMo
http://www.emacswiki.org/cgi-bin/wiki/PhpMode


Question: What is the best way to resolve a conflict when doing a git svn rebase, and the git branch you are on becomes "(no-branch)"?

Answer:
You can use
```
git mergetool
```
to view and edit the conflicts in the usual fashion. Once you are sure the conflicts are resolved do
```
git rebase --continue
```
to continue the rebase, or if you don't want to include that revision do
```
git rebase --skip
```


Question:
I really like Araxis Merge for a graphical DIFF program for the PC. I have no idea what's available for linux, though.  We're running SUSE linux on our z800 mainframe.
I'd be most grateful if I could get a few pointers to what programs everyone else likes.

Answer:
I know of two graphical diff programs:
Meld
and
KDiff3
. I haven't used KDiff3, but Meld works well for me.
It seems that both are in the standard package repositories for openSUSE 11.0


Question: I use the screen command for command-line multitasking in Linux and I set my scrollback buffer length to a very large value. Is there a key combination to clear the buffer for a certain tab when I don't want it sitting there anymore?

Answer:
```
C-a C
```
will clear the screen, including the prompt
```
clear
```
(command, not key combination) will clear the screen, leaving a prompt
ETA: misread the original question; these will just clear the visible text, but will not clear the buffer!


Question: Is there a simple way to hook into the standard 'Add or Remove Programs' functionality using PowerShell to uninstall an existing application? Or to check if the application is installed?

Answer:
```
```
$app = Get-WmiObject -Class Win32_Product | Where-Object {
    $_.Name -match "Software Name"
}

$app.Uninstall()
```
```
Edit:
Rob found another way to do it with the Filter parameter:
```
```
$app = Get-WmiObject -Class Win32_Product `
                     -Filter "Name = 'Software Name'"
```
```


Question: In terms of Webserver and low package size installation.

Answer:
To be honest, the best distro for VMWare is the one the admin has the most experience with.  With the GUI stuff all disabled I've not found any difference in performance between RedHat, Centos and Ubuntu when running VMWare.
Picking the distro that you can adminster easiest will save you hassle.  If you already have a few linux systems using the same flavour makes the admins job a lot easier.


Question: Is it possible to automount a TrueCrypt volume when logging in to Ubuntu 8.04? It's already storing the wireless network keys using the Seahorse password manager. Could TrueCrypt be made to fetch its volume password from the same keyring? Currently this would seem like the most convenient way to store my source code on the USB stick I carry around to boot from.

Answer:
Although I'm currently not a Gentoo user (on Ubuntu now), I used to be one, for years, and had learned, that it's a good thing to search for linux answers on
forums.gentoo.org
and the
Gentoo wiki
.
I had found these, HTH:
http://forums.gentoo.org/viewtopic-t-691788-highlight-truecrypt.html
http://forums.gentoo.org/viewtopic-t-657404-highlight-truecrypt.html


Question: In Bash, how do I count the number of non-blank lines of code in a project?

Answer:
```
```
awk '/^[[:space:]]*$/ {++x} END {print x}' "$testfile"
```
```


Question:
In your opinion, what are the best options for adding video to a website assuming it would be rendered as FLV.  What are the key considerations?

Would you use a 3rd party service (youtube.com, vimeo.com, etc.) or host yourself?  Why?
If you used a service, which one?  If you hosted yourself is it as simple as using an existing embeddable flash FLV player to access FLV files via HTTP or is there something more you would do in terms of content management, etc.?

Answer:
Whether you decide to host the video yourself depends greatly on your requirements, hosting environment and technology you use. If it's a small personal site, than it's prefectly ok to host it on youtube or another hosting service, but if you are making a corporate site, it looks much more professional if you host it yourself. Or if the video won't change very frequently it's pretty easy to just host it yourself.
To host it yourself it's just simply puting in a web accessible directory on the server and setting the URL in the player.
If you need to do content management, than keep in mind the possible upload limits you will have on the server, and the fact, that HTTP is not the ideal protocol for uploading large files.
If you have to recode the video on the server, than don't forget that it will be a serious performance hit to it while the encoding is running.
To recode the video on the server I prefer to use FFMPEG or mencoder (both have windows and linux/unix versions).


Question:
Could anyone could point me to some code/give me ideas on how to create a smooth scrolling vertical marquee for VFP 8 or 9?

Any help is appreciated.

Answer:
Whether you decide to host the video yourself depends greatly on your requirements, hosting environment and technology you use. If it's a small personal site, than it's prefectly ok to host it on youtube or another hosting service, but if you are making a corporate site, it looks much more professional if you host it yourself. Or if the video won't change very frequently it's pretty easy to just host it yourself.
To host it yourself it's just simply puting in a web accessible directory on the server and setting the URL in the player.
If you need to do content management, than keep in mind the possible upload limits you will have on the server, and the fact, that HTTP is not the ideal protocol for uploading large files.
If you have to recode the video on the server, than don't forget that it will be a serious performance hit to it while the encoding is running.
To recode the video on the server I prefer to use FFMPEG or mencoder (both have windows and linux/unix versions).


Question: PowerShell is definitely in the category of dynamic languages, but would it be considered strongly typed?

Answer:
It can be if you need it to be.
Like so:
```
```
[1] » [int]$x = 5
[2] » $x
5
[3] » $x = 'haha'
Cannot convert value "haha" to type "System.Int32". Error: "Input string was not in a correct format."
At line:1 char:3
+ $x  <<<< = 'haha'
[4] »
```
```
Use the [type] notation to indicate if you care about variables being strongly typed.
EDIT
:
As
edg
pointed out, this doesn't prevent PowerShell from interpreting "5" as an integer, when executing (5 + "5"). I dug a little more, and according to Bruce Payette in
Windows PowerShell in Action
, PowerShell is actually a "type-promiscuous language." So, I guess, my answer is "sort of."


Question: How do I add an empty directory (that contains no files) to a Git repository?

Answer:
You can't.  See the
Git FAQ
.
Currently the design of the git index
  (staging area) only permits files to
  be listed, and nobody competent enough
  to make the change to allow empty
  directories has cared enough about
  this situation to remedy it.
Directories are added automatically
  when adding files inside them. That
  is, directories never have to be added
  to the repository, and are not tracked
  on their own.
You can say "
```
git add <dir>
```
" and it
  will add files in there.
If you really need a directory to
  exist in checkouts you should create a
  file in it. .gitignore works well for
  this purpose; you can leave it empty,
  or fill in the names of files you
  expect to show up in the directory.


Question: I'm fine working on Linux using gcc as my C compiler but would like a Windows solution. Any ideas? I've looked at Dev-C++ from Bloodshed but looking for more options.

Answer:
MinGW
would be a direct translation off gcc for windows, or you might want to check out
LCC
, vanilla c (more or less) with an IDE.
Pelles C
seems to be based off lcc and has a somewhat nicer IDE, though I haven't used it personally. Of course there is always the
Express Edition
of MSVC which is free, but that's your call.


Question: When writing an app that one wants to have compile on mac, linux and windows, what is the best way of managing the different libraries that will need to be included on the various operating systems. For example, using the glut opengl toolkit requires different includes on each operating system.

Answer:
Several projects I've worked on use an autoconf-based configure script which builds a Makefile, hence the reason you can build all of them from source with a simple:
```
```
./configure
make
make install
```
```


Question: How do you clear the IRB console screen?

Answer:
On
Mac OS X
or
Linux
you can use
Ctrl
+
L
to clear the
IRB
screen.


Question:
I would like to be able to monitor my major system health indicators from inside our Java-based system.  Major points of interest include CPU temperature, motherboard temperature, fan speed, etc.

Is there a package available that:

Makes this sort of data available to Java?

Works on Windows or Linux or both?

Is open / free / cheap?

Answer:
A few months ago I looked for such a library and I found nothing interesting. It is not impossible to create one, so I would recommend doing so. You'll probably need to access native libraries, to do that use
JNA
(it's easier than JNI). Start by adding support for a few things on one platform, then start adding support for other features and platforms.
The share it with us! People will starts using it, maybe even help with development and soon you'll have a fully featured system monitoring library for Java.


Question: My ASP.NET MVC application will take a lot of bandwidth and storage space. How can I setup an ASP.NET upload page so the file the user uploaded will go straight to Amazon S3 without using my web server's storage and bandwidth?

Answer:
Update Feb 2016:
The AWS SDK can handle a lot more of this now.  Check out
how to build the form
, and
how to build the signature
.  That should prevent you from needing the bandwidth on your end, assuming you need to do no processing of the content yourself before sending it to S3.


Question: do you use a tool?  or just manually make them?

Answer:
We use something locally based on
http://opentcdb.org/
but that does scrum tracking, and draws pretty graphs.


Question: I just wanted to compare different solutions used when implementing ACLs in Rails.

Answer:
There's a plugin called acl_system2 which operates by having a users table and a roles table. There's a lot more useful information in the
README
and the project is on github too.


Question: Like it says.

Answer:
From
http://www.digital-web.com/articles/writing_semantic_markup/
:
semantic markup is markup that is descriptive enough to allow us and the machines we program to recognize it and make decisions about it. In other words, markup
means
something when we can identify it and do useful things with it. In this way, semantic markup becomes more than merely descriptive. It becomes a brilliant mechanism that allows both humans and machines to “understand” the same information.


Question: Know of an OCAML/CAML IDE?  Especially one that runs on Linux?

Answer:
Emacs in
Caml mode
, or
Tuareg mode
, or
TypeRex mode
.  TypeRex adds auto-completion to Taureg in emacs - a really nice feature for people who prefer the more graphical IDE's.


Question:
The problem is not about randomness itself (we have rand), but in cryptographically secure PRNG. What can be used on Linux, or ideally POSIX? Does NSS have something useful?

Clarification: I know about /dev/random, but it may run out of entropy pool. And I'm not sure whether /dev/urandom is guaranteed to be cryptographically secure.

Answer:
The
```
/dev/random
```
device is intended to be a source of cryptographically secure bits.


Question:
What logging solutions exist for j2me?

I'm specifically interested in easily excluding logging for "release" version, to have a smaller package & memory footprint.

Answer:
I've used
MIDPLogger
to some acceptable level in a production application, although I have found it has more use after integrating into the application rather than as another Midlet in suite or so forth. I also found
MicroLog
but haven't used it to any great detail.


Question: How do I detect what browser (IE, Firefox, Opera) the user is accessing my site with?  Examples in Javascript, PHP, ASP, Python, JSP, and any others you can think of would be helpful.  Is there a language agnostic way to get this information?

Answer:
If it's for handling the request, look at the
User-Agent
header on the incoming request.
UPDATE: If it's for reporting, configure your web server to log the User-Agent in the access logs, then run a log analysis tool, e.g.,
AWStats
.
UPDATE 2: FYI, it's
usually
(not always, usually) a bad idea to change the way you're handling a request based on the User-Agent.


Question:
What makes a language strongly typed? I'm looking for the most important aspects of a strongly typed language.

Yesterday I asked if PowerShell was strongly typed, but no one could agree on the definition of "strongly-typed", so I'm looking to clarify the definition.

Feel free to link to wikipedia or other sources, but don't just cut and paste for your answer.

Answer:
Strongly typed means you declare your variables of a certain type, and your compiler will throw a hissy fit if you try to convert that variable to another type without casting it.
Example (in java mind you):
```
```
int i = 4;
char s = i; // Type mismatch: cannot convert from int to char
```
```


Question:
I would like to know how to unload an assembly that is loaded into the main AppDomain.

I have the following code:

var assembly = Assembly.LoadFrom( FilePathHere );

I need/want to be able to unload this assembly when I am done.

Thanks for your help.

Answer:
For .net versions core 3.0 and later:
You can now unload assemblies.  Note that appdomains are no longer available in .net core.  Instead, you can create one or more AssemblyLoadContext, load your assemblies via that context, then unload that context.  See
AssemblyLoadContext
, or
this tutorial that simulates loading a plugin then unloading it
.
For .net versions before .net core 3, including netframework 4 and lower
You can not unload an assembly from an appdomain.  You can destroy appdomains, but once an assembly is loaded into an appdomain, it's there for the life of the appdomain.
See Jason Zander's explanation of
Why isn't there an Assembly.Unload method?
If you are using 3.5, you can use the AddIn Framework to make it easier to manage/call into different AppDomains (which you
can
unload, unloading all the assemblies).  If you are using versions before that, you need to create a new appdomain yourself to unload it.


Question:
We are using Zabbix for services monitoring.

There are some essential monitoring configured.
I want to have timeline of version strings of my service along with this monitorings. That would give me opportunity to see that upgrading to this version altered overall error-count.

Is it possible?

Answer:
Yes, it's possible.
You can pass arbitrary data from your Zabbix agent to the Zabbix server by using "UserParameter" fields in zabbix_server.conf, i.e. agent configuration file.
General syntax is:
UserParameter=section[id], command
For example, let's assume you want to monitor how many users are logged in. You would use:
UserParameter=sys[num_users], who | wc -l
(I assume you know how to configure the Zabbix server to receive this data, it's pretty straightforward - just create a new item, bind it to a template and connect a template to a server or server group).
If you want to monitor some file for a specific string, just use grep, sed, cut, tr and other standard Unix tools. If you need more complex things, just write a shell script.


Question:
does any one know how to get the current motherboard, processor or HD temperature statistics?

In GNU/Linux, I know I could use something like hddtemp or sensord to get the info, and then parse it... but in Windows: How can I do this?  And, Can it be done with with C# or Java or any other hight level programming language?

Thanks!

Answer:
I would argue that when the right configurations are in place, it can be superior to windows's one.
http://www.lm-sensors.org/
is what does all the work. I had that plugged into RRDgraph & Munin and I was monitoring the temperature of my room over a period of almost a year and had nice pretty graphs.  Also showed me my CPU fan was slowly wearing down, and I could see the line sloping down over a long period and know it was on the way out.
http://www.lm-sensors.org/browser/lm-sensors/trunk/doc/developers/applications
is what you want.
(Oh wait, I fail. You're on *nix wanting to do it on Windows, my bad :(  ..um.. well. Good luck. Maybe I'll leave this here in case somebody finds your post while searching for the contrary)
Back when I did use windows, all I recall is Ye' Old
Motherboard Monitor ( Discontinued )
.
Wiki article says there is
speedfan
and that looks like your best option. Programmatically, I guess you'll have to find the hardware specs and dig through Windows API and stackloads of arbitrary bus address offsets.


Question: When I create an Issue Tracking list in SharePoint, I am unable to remove the Categories choice field that it adds by default as part of the Issue content type.  I looked in the list definition but I can't find anything explicit about not allowing the column to be deleted.  Does anybody know why the Delete button isn't showing up?  Is there a way to delete this field?

Answer:
I know that I have had a similar issue with field in a variety of fields where once the field is added, it is not possible to remove it.
Sometimes it is possible to create code to delete the field, but in most of the situations I have come across we have had to hide the field to prevent it from appearing.
This requires moving to using Powershell and the SharePoint object model to make the changes.
In most of our implementations, we have found it much better to create a custom solution with a custom feature that adds the custom lists and fields using the XML format for doing this. A list template can then be create exactly for what you need.
Doing it this way gives us more control over the result in a repeatable manner.


Question:
Is there a way in PHP to make asynchronous HTTP calls? I don't care about the response, I just want to do something like file_get_contents(), but not wait for the request to finish before executing the rest of my code. This would be super useful for setting off "events" of a sort in my application, or triggering long processes.

Any ideas?

Answer:
You can do trickery by using exec() to invoke something that can do HTTP requests, like
```
wget
```
, but you must direct all output from the program to somewhere, like a file or /dev/null, otherwise the PHP process will wait for that output.
If you want to separate the process from the apache thread entirely, try something like (I'm not sure about this, but I hope you get the idea):
```
```
exec('bash -c "wget -O (url goes here) > /dev/null 2>&1 &"');
```
```
It's not a nice business, and you'll probably want something like a cron job invoking a heartbeat script which polls an actual database event queue to do real asynchronous events.


Question:
How exactly do DLL files work? There seems to be an awful lot of them, but I don't know what they are or how they work.

So, what's the deal with them?

Answer:
What is a DLL?
Dynamic Link Libraries (DLL)s are like EXEs but they are not directly executable.  They are similar to .so files in Linux/Unix.  That is to say, DLLs are MS's implementation of shared libraries.
DLLs are so much like an EXE that the file format itself is the same.  Both EXE and DLLs are based on the Portable Executable (PE) file format.  DLLs can also contain COM components and .NET libraries.
What does a DLL contain?
A DLL contains functions, classes, variables, UIs and resources (such as icons, images, files, ...) that an EXE, or other DLL uses.
Types of libraries:
On virtually all operating systems, there are 2 types of libraries.  Static libraries and dynamic libraries.  In windows the file extensions are as follows: Static libraries (.lib) and dynamic libraries (.dll).  The main difference is that static libraries are linked to the executable at compile time; whereas dynamic linked libraries are not linked until run-time.
More on static and dynamic libraries:
You don't normally see static libraries though on your computer, because a static library is embedded directly inside of a module (EXE or DLL).  A dynamic library is a stand-alone file.
A DLL can be changed at any time and is only loaded at runtime when an EXE explicitly loads the DLL.  A static library cannot be changed once it is compiled within the EXE.
A DLL can be updated individually without updating the EXE itself.
Loading a DLL:
A program loads a DLL at startup, via the Win32 API LoadLibrary, or when it is a dependency of another DLL.  A program uses the GetProcAddress to load a function or LoadResource to load a resource.
Further reading:
Please check
MSDN
or
Wikipedia
for further reading.   Also the sources of this answer.


Question: Whats the available solutions for PHP to create word document in linux environment?

Answer:
real Word documents
If you need to produce "real" Word documents you need a Windows-based web server and COM automation. I highly recommend
Joel's article
on this subject.
fake
HTTP headers for tricking Word into opening raw HTML
A rather common (but unreliable) alternative is:
```
```
header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment; filename=document_name.doc");

echo "";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "";
echo "Fake word document";
echo "";
echo ""
```
```
Make sure you don't use external stylesheets. Everything should be in the same file.
Note that this does
not
send an actual Word document. It merely tricks browsers into offering it as download and defaulting to a
```
.doc
```
file extension. Older versions of Word may often open this without any warning/security message, and just import the raw HTML into Word. PHP sending sending that misleading
```
Content-Type
```
header along does not constitute a real file format conversion.


Question:
for working with MS word files in python, there is python win32 extensions, which can be used in windows. How do I do the same in linux?
Is there any library?

Answer:
Take a look at
how the doc format works
and
create word document using PHP in linux
. The former is especially useful.
Abiword
is my recommended tool. There are
limitations
though:
However, if the document has complicated tables, text boxes, embedded spreadsheets, and so forth, then it might not work as expected. Developing good MS Word filters is a very difficult process, so please bear with us as we work on getting Word documents to open correctly. If you have a Word document which fails to load, please open a Bug and include the document so we can improve the importer.


Question: Given a string file path such as /foo/fizzbuzz.bar, how would I use bash to extract just the fizzbuzz portion of said string?

Answer:
look at the basename command:
```
```
NAME="$(basename /foo/fizzbuzz.bar .bar)"
```
```
instructs it to remove the suffix
```
.bar
```
, results in
```
NAME=fizzbuzz
```


Question:
Is it possbile to execute linux commands with java? I am trying to create a web servlet to allow ftp users to change their passwords without ssh login access. I would like to execute the next commands:

# adduser -s /sbin/nologin clientA -d /home/mainclient/clientA
# passwd clientA
# cd /home/mainclient; chgrp -R mainclient clientA
# cd /home/mainclient/clientA; chmod 770 .

Answer:
Check out
this
.
However, doing what you are talking about is way outside spec, and I wouldnt reccommend it. To get it to work you are going to either run your app server as root, or use some other mechanism to give the user the app server is running as permission to execute these privileged commands. One small screw-up somewhere and you are "owned".


Question:
Since the release of Adobe AIR I am wondering why Java Web Start has not gained more attention in the past as to me it seems to be very similar, but web start is available for a much longer time.

Is it mainly because of bad marketing from Sun, or are there more technical concerns other than the need of having the right JVM installed? Do you have bad experiences using Web Start? If yes, which? What are you recommendations when using Web Start for distributing applications?

Answer:
In my company we used Java Web Start to deploy Eclipse RCP applications.  It was a pain to setup, but it works very well once in place.  So the only recommendation I could make is to start small, to get the hang of it.  Deploying one simple application first.  Trying to deploy a complete product that is already made without experience with JWS gets complicated rather quickly.
Also, learning how to pass arguments to the JWS application was invaluable for debugging.  Setting the Environment variable JAVAWS_VM_ARGS allows setting any arbitrary property to the Java Virtual machine.  In my case:
-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=4144
Helpful when you need to check problems during start-up (suspend=y)
I think the main problem for the acceptance of Java Web Start is that it is relatively difficult to setup.  Also, somehow there is this dissonance:  When you have a desktop application, people expects a installer they can double click.  When you have a web application, people expects they can use it right from the browser.  Java Web Start is neither here not there...
It is widely used in intranets, though.


Question: To my amazement I just discovered that the C99 stdint.h is missing from MS Visual Studio 2003 upwards.  I'm sure they have their reasons, but does anyone know where I can download a copy?  Without this header I have no definitions for useful types such as uint32_t, etc.

Answer:
Turns out you can download a MS version of this header from:
https://github.com/mattn/gntp-send/blob/master/include/msinttypes/stdint.h
A portable one can be found here:
http://www.azillionmonkeys.com/qed/pstdint.h
Thanks to the
Software Rambling
s blog.
NB:
The Public Domain version of the header, mentioned by
Michael Burr
in a comment, can be find as an archived copy
here
. An updated version can be found in
the Android source tree for libusb_aah
.


Question:
I saw this on reddit, and it reminded me of one of my vim gripes: It shows the UI in German. I want English. But since my OS is set up in German (the standard at our office), I guess vim is actually trying to be helpful.

What magic incantations must I perform to get vim to switch the UI language? I have tried googling on various occasions, but can't seem to find an answer.

Answer:
Start vim with a changed locale:
```
```
LC_ALL=en_GB.utf-8 vim
```
```
Or export that variable per default in your bashrc/profile.


Question:
  $a = '{ "tag": "<b></b>" }';
  echo json_encode( json_decode($a) );

This outputs:

{"tag":"<b><\/b>"}

when you would think it would output exactly the input.  For some reason json_encode adds an extra slash.

Answer:
Because it's part of the JSON standard
http://json.org/
char
```
```
any-Unicode-character-
    except-"-or-\-or-
    control-character
\"
\\
\/ <---- see here?
\b
\f
\n
\r
\t
\u four-hex-digits
```
```


Question: Is there a way to run an Amazon EC2 AMI image in Windows?  I'd like to be able to do some testing and configuration locally.  I'm looking for something like Virtual PC.

Answer:
If you build your images from scratch you can do it with VMware (or insert your favorite VM software here).
Build and install your linux box as you'd like it, then run the AMI packaging/uploading tools
in
the guest. Then, just keep backup copies of your VM image in sync with the different AMI's you upload.
Some caveats: you'll need to make sure you're using compatible kernels, or at least have compatible kernel modules in the VM, or your instance won't boot on the EC2 network. You'll also have to make sure your system can autoconfigure itself, too (network, mounts, etc).
If you want to use an existing AMI, it's a little trickier. You need to download and unpack the AMI into a VM image, add a kernel and boot it. As far as I know, there's no 'one click' method to make it work. Also, the AMI's might be encrypted (I know they are at least signed).
You may be able to do this by having a 'bootstrap' VM set up to specifically extract the AMI's into a virtual disk using the AMI tools, then boot that virtual disk separately.
I know it's pretty vague, but those are the steps you'd have to go through. You could probably do some scripting to automate the process of converting AMI's to vdks.
The Amazon forum is also helpful. For example, see
this article
.
Oh,
this article
also talks about some of these processes in detail.


Question: How do I make an asynchronous call to a web service using the PHP SOAP Extension?

Answer:
My immediate answer should be: You can't.
PHP does not have threading abilities that can be used in "userland".
Now if you really want to do it, there are some ways you can go around it:
Use the exec functions to spawn another process, in the background, and monitor it through the database/file system or whatever.
Use the fork function to spawn another process and monitor it through the database/file system or whatever.
Setbacks of these 2 approaches is that you can make it asynchronous but if you want a callback then it's going to be very tricky and not at all trivial. Well, it ain't even gonna be a callback since you'll not be able to wait for it on the script that makes the async call. This means that you can only have some kind of monitoring scheme. I would suggest AJAX.


Question: I want a true deep copy. In Java, this was easy, but how do you do it in C#?

Answer:
Important Note
BinaryFormatter
has been deprecated, and will no longer be available in .NET after November 2023.  See
BinaryFormatter Obsoletion Strategy
I've seen a few different approaches to this, but I use a generic utility method as such:
```
```
public static T DeepClone<T>(this T obj)
{
 using (var ms = new MemoryStream())
 {
   var formatter = new BinaryFormatter();
   formatter.Serialize(ms, obj);
   ms.Position = 0;

   return (T) formatter.Deserialize(ms);
 }
}
```
```
Notes:
Your class MUST be marked as
```
[Serializable]
```
for this to work.
Your source file must include the following code:
```
```
using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
```
```


Question:
I have an Open Source app and I currently only post the binary for the Windows build. At this point Linux users have to get the source and compile it. Is there a standard way for posting a Linux binary?

My app is in c / c++ and compiled with gcc, the only external Linux code I use is X Windows and CUPS.

Answer:
The most common way would be to package it in a
.rpm file
for
RedHat
-based distros like
Fedora
, or a
.deb file
for
Debian
-based distros like
Ubuntu
.


Question: I need to simulate a low bandwidth, high latency connection to a server in order to emulate the conditions of a VPN at a remote site. The bandwidth and latency should be tweakable so I can discover the best combination in order to run our software package.

Answer:
In the past, I have used a bridge using the Linux
Netem
(Network Emulation) functionality.  It is highly configurable -- allowing the introduction of delays (the first example is for a WAN), packet loss, corruption, etc.
I'm noting that Netem worked very well for my applications, but I also ended up using
WANem
several times. The provided bootable ISO (and virtual appliance images) made it quite handy.


Question:
What is a .snk file for?  I know it stands for Strongly Named Key, but all explanations of what it is and how it works goes over my head.

Is there any simple explanation on how a strongly named key is used and how it works?

Answer:
The .snk file is used to apply a
strong name to a .NET assembly
. such a strong name consists of
a simple text name, version number,
and culture information (if
provided)—plus a public key and a
digital signature.
The SNK contains a unique key pair - a private and public key that can be used to ensure that you have a unique strong name for the assembly. When the assembly is strongly-named, a "hash" is constructed from the contents of the assembly, and the hash is encrypted with the private key. Then this signed hash is placed in the assembly along with the public key from the .snk.
Later on, when someone needs to verify the integrity of the strongly-named assembly,
they
build a hash of the assembly's contents, and use the public key from the assembly to decrypt the hash that came with the assembly - if the two hashes match, the assembly verification passes.
It's important to be able to verify assemblies in this way to ensure that nobody swaps out an assembly for a malicious one that will subvert the whole application. This is why non-strong-named assemblies aren't trusted in the same way that strongly-named assemblies are, so they can't be placed in the GAC. Also, there's a chain of trust - you can't generate a strongly-named assembly that references non-strongly-named assemblies.
The article "
The Secrets of Strong Naming (archived at the Wayback Machine)
". Does an excellent job of explaining these concepts in more detail. With pictures.


Question: What techniques and/or modules are available to implement robust rate limiting (requests|bytes/ip/unit time) in apache?

Answer:
The best
mod_evasive
(Focused more on reducing DoS exposure)
mod_cband
(Best featured for 'normal' bandwidth control)
and the rest
mod_limitipconn
mod_bw
mod_bwshare


Question: How can I send an email from C++? Is there a good cross-platform (MS Windows, Linux etc) library that I can use? I'm using GCC (cygwin on MS Windows).

Answer:
Look at
VMime
.
VMime is an all-in-one Internet mail library. This well designed, powerful C++ class library allows you to parse/build/modify MIME messages. With the messaging module, you can connect to POP3/IMAP/SMTP/Maildir very easily, and with the same code!


Question: Are there any good alternatives that support writing regexps in different flavors and allow you to test them?

Answer:
Here's a list of the Regex tools mentioned across the threads:
Regulator
Expresso
.NET Regular Expression Designer
Regex-Coach
larsolavtorvik online tool
Regex Pal
Regular Expression Workbench
Rubular
Reggy
RegExr


Question:
I'd like to find a way to determine how long each function in PHP, and each file in PHP is taking to run.  I've got an old legacy PHP application that I'm trying to find the "rough spots" in and so I'd like to locate which routines and pages are taking a very long time to load, objectively.

Are there any pre-made tools that allow for this, or am I stuck using microtime, and building my own profiling framework?

Answer:
take a look into
xdebug
, which allows in-depth profiling.  And
here's an explanation
of how to use xdebug.
Xdebug's Profiler is a powerful tool
  that gives you the ability to analyze
  your PHP code and determine
  bottlenecks or generally see which
  parts of your code are slow and could
  use a speed boost. The profiler in
  Xdebug 2 outputs profiling information
  in the form of a cachegrind compatible
  file.
Kudos to
SchizoDuckie
for
mentioning
Webgrind
.  This is the first I've heard of it.  Very useful (+1).
Otherwise, you can use
kcachegrind
on linux or its lesser derivative
wincachegrind
.  Both of those apps will read xdebug's profiler output files and summarize them for your viewing pleasure.


Question: While writing a file using ofstream, how do I know when the file's size has reached the OS' maximum file size - more specifically linux's maximum file size - ??

Answer: I think (not 100% sure) that you'd just have to compare the stream's current size after a write to whatever the OS's max file size is. Otherwise I'm guessing the underlying implementation will just let you keep writing until the actual OS io calls fail.


Question: What are the advantages/disadvantages of the major Ruby VMs (things like features, compatibility, performance, and quirks?) I know there are also some bonus features like being able to use Java interfaces through JRuby, too. Those would also be helpful to note. Does any VM have a clear advantage at this point, and in what contexts?

Answer:
The Full Code
You'll find below the full code, copy/pasted from a Visual C++ 2008 .cs file. As I'm now on Linux, and without any Mono compiler or knowledge about its use, there's no way I can do tests now. Still, a couple of hours ago, I saw this code work and its bug:
```
```
using System;
using System.Threading;

public class Example
{
    private int nValue;
    public int N { get { return nValue; } }

    // The Hash property is slower because it clones an array. When
    // KeepAlive is not used, the finalizer sometimes runs before
    // the Hash property value is read.

    private byte[] hashValue;
    public byte[] Hash { get { return (byte[])hashValue.Clone(); } }
    public byte[] Hash2 { get { return (byte[])hashValue; } }

    public int returnNothing() { return 25; }

    public Example()
    {
        nValue = 2;
        hashValue = new byte[20];
        hashValue[0] = 2;
    }

    ~Example()
    {
        nValue = 0;

        if (hashValue != null)
        {
            Array.Clear(hashValue, 0, hashValue.Length);
        }
    }
}

public class Test
{
    private static int totalCount = 0;
    private static int finalizerFirstCount = 0;

    // This variable controls the thread that runs the demo.
    private static bool running = true;

    // In order to demonstrate the finalizer running first, the
    // DoWork method must create an Example object and invoke its
    // Hash property. If there are no other calls to members of
    // the Example object in DoWork, garbage collection reclaims
    // the Example object aggressively. Sometimes this means that
    // the finalizer runs before the call to the Hash property
    // completes.

    private static void DoWork()
    {
        totalCount++;

        // Create an Example object and save the value of the
        // Hash property. There are no more calls to members of
        // the object in the DoWork method, so it is available
        // for aggressive garbage collection.

        Example ex = new Example();

        // Normal processing
        byte[] res = ex.Hash;

        // Supposed inlined processing
        //byte[] res2 = ex.Hash2;
        //byte[] res = (byte[])res2.Clone();

        // successful try to keep reference alive
        //ex.returnNothing();

        // Failed try to keep reference alive
        //ex = null;

        // If the finalizer runs before the call to the Hash
        // property completes, the hashValue array might be
        // cleared before the property value is read. The
        // following test detects that.

        if (res[0] != 2)
        {
            finalizerFirstCount++;
            Console.WriteLine("The finalizer ran first at {0} iterations.", totalCount);
        }

        //GC.KeepAlive(ex);
    }

    public static void Main(string[] args)
    {
        Console.WriteLine("Test:");

        // Create a thread to run the test.
        Thread t = new Thread(new ThreadStart(ThreadProc));
        t.Start();

        // The thread runs until Enter is pressed.
        Console.WriteLine("Press Enter to stop the program.");
        Console.ReadLine();

        running = false;

        // Wait for the thread to end.
        t.Join();

        Console.WriteLine("{0} iterations total; the finalizer ran first {1} times.", totalCount, finalizerFirstCount);
    }

    private static void ThreadProc()
    {
        while (running) DoWork();
    }
}
```
```
For those interested, I can send the zipped project through email.


Question:
How do I display a leading zero for all numbers with less than two digits?

1    →  01
10   →  10
100  →  100

Answer:
```
```
x = [1, 10, 100]
for i in x:
    print '%02d' % i
```
```
results in:
```
```
01
10
100
```
```
Read
more information about string formatting using %
in the documentation.


Question: I'm trying to confirm a user a unplugged my embedded device from a network before performing some maintenance. I'm considering "ping"ing all IP address on my sub-net, but that sounds crude. Is there a broadcast/ARP method that might work better?

Answer:
You can try a broadcast ping (this is from linux):
```
```
ping -b 255.255.255.255
```
```
Another option is to download
Nmap
and do a ping-scan.


Question:
I've seen several products that will track the sales rank of an item on Amazon. Does Amazon have any web-services published that I can use to get the sales rank of a particular item?

I've looked through the AWS and didn't see anything of that nature.

Answer:
You should be able to determine the Sales Rank by querying for the SalesRank response group when doing an ItemLookup with the Amazon Associates Web Service.
Example query:
```
```
http://ecs.amazonaws.com/onca/xml?
Service=AWSECommerceService&
AWSAccessKeyId=[AWS Access Key ID]&
Operation=ItemLookup&
ItemId=0976925524&
ResponseGroup=SalesRank&
Version=2008-08-19
```
```
Response:
```
```
<Item>
  <ASIN>0976925524</ASIN>
  <SalesRank>68</SalesRank>
</Item>
```
```
See the documentation here:
http://docs.amazonwebservices.com/AWSECommerceService/2008-08-19/DG/index.html?RG_SalesRank.html


Question:
How do you find the version of an installed Perl module?

This is in an answer down at the bottom, but I figure it important enough to live up here.  With these suggestions, I create a function in my .bashrc

function perlmodver {
    perl -M$1 -e 'print "Version " . $ARGV[0]->VERSION . " of " . $ARGV[0] . \
    " is installed.\n"' $1
}

Answer:
Most modules (especially ones from The CPAN) have a $VERSION variable:
```
```
perl -MSome::Module -le 'print $Some::Module::VERSION'
```
```


Question:
I'm running git-diff on a file, but the change is at the end of a long line.

If I use cursor keys to move right, it loses colour-coding—and worse the lines don't line up—making it harder to track the change.

Is there a way to prevent that problem or to simply make the lines wrap instead?

I'm running Git 1.5.5 via mingw32.

Answer:
Not a perfect solution, but
```
gitk
```
and
```
git-gui
```
can both show this information,
and have scrollbars.


Question: I'm working on an embedded linux system in C, I'm looking for the source code to the equivalet of SendARP in Windows. Any pointers?

Answer:
This may be of interest:
http://cvs.linux-ha.org/viewcvs/viewcvs.cgi/linux-ha/resources/heartbeat/SendArp.in?rev=1.4
It is an implmenetation in a Bourne Shell script.


Question:
I need to find the PID of the current running process on a Linux platform (it can be a system dependent solution).  Java does not support getting the process ID, and JRuby currently has a bug with the Ruby method, Process.pid.

Is there another way to obtain the PID?

Answer:
If you have
procfs
installed, you can find the process id via the /proc/self symlink, which points to a directory whose name is the pid (there are also files here with other pertinent information, including the PID, but the directory is all you need in this case).
Thus, with Java, you can do:
```
```
String pid = new File("/proc/self").getCanonicalFile().getName();
```
```
In JRuby, you can use the same solution:
```
```
pid = java.io.File.new("/proc/self").canonical_file.name
```
```
Special thanks to the #stackoverflow channel on free node for helping me solve this! (specifically,
Jerub
,
gregh
, and
Topdeck
)


Question: Are there any ncurses libraries in C/C++ for Windows that emulate ncurses in native resizable Win32 windows (not in console mode)?

Answer:
Such a thing probably does not exist "as-is". It doesn't really exist on Linux or other UNIX-like operating systems either though.
ncurses is only a library that helps you manage interactions with the underlying terminal environment. But it doesn't provide a terminal emulator itself.
The thing that actually displays stuff on the screen (which in your requirement is listed as "native resizable win32 windows") is usually called a Terminal Emulator. If you don't like the one that comes with Windows (you aren't alone; no person on Earth does) there are a few alternatives. There is
Console
, which in my experience works sometimes and appears to just wrap an underlying Windows terminal emulator (I don't know for sure, but I'm guessing, since there is a menu option to actually get access to that underlying terminal emulator, and sure enough an old crusty Windows/DOS box appears which mirrors everything in the Console window).
A better option
Another option, which may be more appealing is
puttycyg
. It hooks in to Putty (which, coming from a Linux background, is pretty close to what I'm used to, and free) but actually accesses an underlying cygwin instead of the Windows command interpreter (
```
CMD.EXE
```
). So you get all the benefits of Putty's awesome terminal emulator, as well as nice
```
ncurses
```
(and many other) libraries provided by cygwin. Add a couple command line arguments to the Shortcut that launches Putty (or the Batch file) and your app can be automatically launched without going through Putty's UI.


Question:
is it possible to extend vim functionality via custom extension (preferably, written in Python)?

What I need ideally is custom command when in command mode. E.g.

ESC

:do_this

:do_that

Answer:
vim supports scripting in python (and in perl as well, I think).
You just have to make sure that the vim distribution you are using has been compiled with python support.
If you are using a Linux system, you can download the source and then compile it with
```
```
./configure --enable-pythoninterp
make
sudo make install
```
```
Inside vim, you can type
```
```
:version
```
```
to list the available features; if it has python support, you should see a '+python' somewhere (a '-python' otherwise).
Then, to check the usage of the python module, you can type
```
```
:help python
```
```
P.S: if you're going to compile the vim sources, make sure to check the available configure options, you might need to specify --with-python-config-dir as well.
P.P.S: to create a "custom command in command mode" (if I understand correctly what you mean), you can create a function "MyFunction" in a vim script (using python or the vim scripting language) and then invoke it with
```
```
:Call MyFunction()
```
```
Check
```
```
:help user-functions
```
```
for details


Question:
For testing purposes I have to generate a file of a certain size (to test an upload limit).

What is a command to create a file of a certain size on Linux?

Answer:
Use this command:
```
dd if=$INPUT-FILE of=$OUTPUT-FILE bs=$BLOCK-SIZE count=$NUM-BLOCKS
```
To create a big (empty) file, set
```
$INPUT-FILE=/dev/zero
```
.
Total size of the file will be
```
$BLOCK-SIZE * $NUM-BLOCKS
```
.
New file created will be
```
$OUTPUT-FILE
```
.


Question:
On Linux/NPTL, threads are created as some kind of process.

I can see some of my process have a weird cmdline:

cat /proc/5590/cmdline
hald-addon-storage: polling /dev/scd0 (every 2 sec)

Do you have an idea how I could do that for each thread of my process? That would be very helpful for debugging.

/me now investigating in HAL source

Answer:
```
argv
```
points to writable strings. Just write stuff to them:
```
```
#include <string.h>
#include <unistd.h>

int
main(int argc, char** argv)
{
    strcpy(argv[0], "Hello, world!");
    sleep(10);
    return 0;
}
```
```


Question: I have a custom performance counter category. Visual Studio Server Explorer refuses to delete it, claiming it is 'not registered or a system category'. Short of doing it programmatically, how can I delete the category? Is there a registry key I can delete?

Answer:
As far as I know, there
is no way
to safely delete them except programatically (they're intended for apps to create and remove during install) but it is trivial to do from a
PowerShell
command-line console. Just run this command:
```
```
[Diagnostics.PerformanceCounterCategory]::Delete( "Your Category Name" )
```
```
HOWEVER: (EDIT)
You
can
delete the registry key that's created, and that will make the category vanish.
For a category called "Inventory" you can delete the whole key at
```
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Inventory
```
... and although
I wouldn't be willing to bet that cleans up everything
, it
will
make the category disappear. (If you run
Process Monitor
while running the Delete() method, you can see can a lot of other activity happening, and there doesn't seem to be any other
changes
made).
It's important to note that
as I said originally
: when you get that error from Visual Studio, it might be that it's already deleted and you need to refresh the view in VS.  In my testing, I had to restart applications in order to get them to actually get a clean list of the available categories.
You can check the full list of categories from PowerShell to see if it's listed:
```
```
[Diagnostics.PerformanceCounterCategory]::GetCategories() | Format-Table -auto
```
```
But if you check them, then delete the registry key ... they'll still show up, until you restart PowerShell (if you start another instance, you can run the same query over there, and it will NOT show the deleted item, but re-running GetCategories in the first one will continue showing it.
By the way, you can filter that list if you want to using -like for patterns, or -match for full regular expressions:
```
```
[Diagnostics.PerformanceCounterCategory]::GetCategories() | Where {$_.CategoryName -like "*network*" } | Format-Table -auto
[Diagnostics.PerformanceCounterCategory]::GetCategories() | Where {$_.CategoryName -match "^SQL.*Stat.*" } | Format-Table -auto
```
```


Question:
I downloaded Hex Workshop, and I was told to read a .dbc file.

It should contain 28,315 if you read
  offset 0x04 and 0x05

I am unsure how to do this? What does 0x04 mean?

Answer:
0x04 is hex for 4 (the 0x is just a common prefix convention for base 16 representation of numbers - since many people think in decimal), and that would be the fourth byte (since they are saying offset, they probably count the first byte as byte 0, so offset 0x04 would be the 5th byte).
I guess they are saying that the 4th and 5th byte together would be 28315, but did they say if this is little-endian or big-endian?
28315 (decimal) is 0x6E9B in hexadecimal notation, probably in the file in order 0x9B 0x6E if it's little-endian.
Note: Little-endian and big-endian refer to the order bytes are written.  Humans typical write decimal notation and hexadecimal in a big-endian way, so:
256 would be written as 0x0100 (digits on the left are the biggest scale)
But that takes two bytes and little-endian systems will write the low byte first: 0x00 0x01.  Big-endian systems will write the high-byte first: 0x01 0x00.
Typically Intel systems are little-endian and other systems vary.


Question:
I have a RHEL box that I need to put under a moderate and variable amount of CPU load (50%-75%).

What is the best way to go about this? Is there a program that can do this that I am not aware of? I am happy to write some C code to make this happen, I just don't know what system calls will help.

Answer:
You can probably use some load-generating tool to accomplish this, or run a script to take all the CPU cycles and then use
nice
and
```
renice
```
on the process to vary the percentage of cycles that the process gets.
Here is a sample bash script that will occupy all the free CPU cycles:
```
```
#!/bin/bash
while true ; do
true
done
```
```


Question: I am testing a Ruby Rails website and wanted to get started with Unit and Functional testing.

Answer:
Cucumber
and
RSpec
are worth a look.  They encourage testing in a
behaviour-driven
, example-based style.
RSpec is a library for unit-level testing:
```
```
describe "hello_world"
  it "should say hello to the world" do
    # RSpec comes with its own mock-object framework built in,
    # though it lets you use others if you prefer
    world = mock("World", :population => 6e9)
    world.should_receive(:hello)
    hello_world(world)
  end
end
```
```
It has special support for Rails (e.g. it can test models, views and controllers in isolation) and can replace the testing mechanisms built in to Rails.
Cucumber (formerly known as the RSpec Story Runner) lets you write high-level acceptance tests in (fairly) plain English that you could show to (and agree with) a customer, then run them:
```
```
Story: Commenting on articles

  As a visitor to the blog
  I want to post comments on articles
  So that I can have my 15 minutes of fame

  Scenario: Post a new comment

    Given I am viewing an article
    When I add a comment "Me too!"
    And I fill in the CAPTCHA correctly
    Then I should see a comment "Me too!"
```
```


Question:
How do I deploy an ADF business component to the middle tier and call it using RMI?

I just cannot find any good documentation on this!

Answer:
I guess you mean something like:
How to install ADF 10 runtime and deploy ADF BC app to Oracle WebLogic 103 running on Linux
Oracle Application Development Framework - Development Guidelines (10g)
(PDF format).


Question:
I'm wondering if mono.net is included in the default installation of Ubuntu, Kubuntu, and Fedora, and other popular distros?

If so, does anyone have a good reason why NOT to use it to develop a new GUI application targeted mainly for linux?

Answer:
I'm wondering if mono.net is included in the default installation of Ubuntu, Kubuntu, and Fedora, and other popular distros?
I don't believe Ubuntu comes with it by default, Kubuntu certainly doesn't.
Ubuntu Intrepid, and probably Hardy, comes with Mono by default. Don't know about its status on other distros.
If so, does anyone have a good reason why NOT to use it to develop a new GUI application targeted mainly for linux?
If it's targeted mainly at Linux, why not use a technology that integrates better with the Linux desktop? Python and Java are good choices. Only use .NET if there's a large amount of legacy code you'll be re-using.


Question:
Could any one give an explanation on how a DHT works?

Nothing too heavy, just the basics.

Answer:
DHTs provide the same type of interface to the user as a normal hashtable (look up a value by key), but the data is distributed over an arbitrary number of connected nodes.  Wikipedia has a good basic introduction that I would essentially be regurgitating if I write more -
http://en.wikipedia.org/wiki/Distributed_hash_table


Question:
Greetings,

I'm trying to find either a free .NET library or a command-line executable that lets me convert M4A files to either MP3s or WMA files.  Please help :).

Answer:
Found it!
http://pieter.wigleven.com/it/archives/3
There may be other solutions, but this is gold for what I was looking for.
P.S. I've written
a .NET DLL
which handles this behind-the-scenes. It's pretty terrible code, but it gets the job done.


Question:
Can you use the bash "getopts" function twice in the same script?

I have a set of options that would mean different things depending on the value of a specific option.  Since I can't guarantee that getopts will evaluate that specific option first, I would like to run getopts one time, using only that specific option, then run it a second time using the other options.

Answer:
Yes, just reset OPTIND afterwards.
```
```
#!/bin/bash

set -- -1
while getopts 1 opt; do
    case "${opt}" in
        1) echo "Worked!";;
        *) exit 1;
    esac
done

OPTIND=1
set -- -2
while getopts 2 opt; do
    case "${opt}" in
        2) echo "Worked!";;
        *) exit 1;
    esac
done
```
```


Question:
I need to setup LookAndFeel Files in JDK 1.6.
I have two files:

napkinlaf-swingset2.jar

napkinlaf.jar

How can I set this up and use it?

I would like a GTK look and feel OR Qt look and feel, Are they available?

Answer:
This page explains how the work with Look&Feels:
http://java.sun.com/docs/books/tutorial/uiswing/lookandfeel/plaf.html
You can do it commandline:
```
```
java -Dswing.defaultlaf=com.sun.java.swing.plaf.gtk.GTKLookAndFeel MyApp
```
```
Or in code:
```
```
UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
```
```
You need to make sure the jars containing the look&feel are on the application classpath. How this works depends on the application. A typical way would be to put it in a lib folder.
Look&Feels that are available by default in the JDK are:
```
```
com.sun.java.swing.plaf.gtk.GTKLookAndFeel
com.sun.java.swing.plaf.motif.MotifLookAndFeel
com.sun.java.swing.plaf.windows.WindowsLookAndFeel
```
```
Quioting the link above:
The GTK+ L&F will only run on UNIX or
  Linux systems with GTK+ 2.2 or later
  installed, while the Windows L&F runs
  only on Windows systems. Like the Java
  (Metal) L&F, the Motif L&F will run on
  any platform.


Question:
I'm looking for:

documentation

blogs

books

ready-to-use pre-configured slice images

services

wrappers

libraries

tutorials

...anything that would make it easier to start using EC2 to host a Rails application.

Answer:
ElasticFox is a must have utility for overseeing your instances
http://developer.amazonwebservices.com/connect/entry.jspa?externalID=609
Capazon is an awesome utility to bring instance management into Capistrano
http://soylentfoo.jnewland.com/articles/2007/03/27/capazon-capistrano-meets-amazon-ec2
(there is a newer version out somewhere)
I like these AMI's (I dig Ubuntu)
http://alestic.com/
If you're using Heroku or EngineYard (the main cloud hosting solutions today - they build on top of Amazon EC2) you can also use git to manage your code and both Heroku and EngineYard have great instructions on how to use integrate git with them:
Heroku:
http://devcenter.heroku.com/articles/git
Engine Yard:
http://docs.engineyard.com/host-your-code-on-github.html


Question: In an HTML form post what are valid characters for creating a multipart boundary?

Answer:
According to
RFC 2046
, section 5.1.1:
```
```
boundary := 0*69<bchars> bcharsnospace

 bchars := bcharsnospace / " "

 bcharsnospace := DIGIT / ALPHA / "'" / "(" / ")" /
                  "+" / "_" / "," / "-" / "." /
                  "/" / ":" / "=" / "?"
```
```
So it can be between 1 and 70 characters long, consisting of alphanumeric, and the punctuation you see in the list. Spaces are allowed except at the end.


Question:
I'm using Zend Studio for Eclipse (Linux), and I'm trying to generate getter and setters methods in a PHP class.

I try to do this: http://files.zend.com/help/Zend-Studio-Eclipse-Help/creating_getters_and_setters.htm
but I haven't "Generate Getters and Setters" option in Source Menu, it's missed!

Could u help me? Thanks!

Answer:
It has to be there under the menu - source in Eclipse. Could you provide a snapshot of your Eclipse to verify.
EDITED: I guess it is not possible to generate getters and setters automatically in your version, though you would be able to create templates for the same and use it as per your requirements. Omnipotent (0 seconds ago)


Question: I'm trying to consume a SOAP webservice from an Adobe Flex 3 application, but the server tell me "Invalid SOAP Envelope. SOAP Body does not contain a message nor a fault". I already wrote other test clients (with both Delphi and C#) and I'm sure it's all ok on the server side, so I need to examine the SOAP envelope Flex is sending out to the server. How to do that? I think it should be some event to listen (in the BaseSys class?) to get the envelope before it will be sent.

Answer:
The easiest way is to run a proxy. Paros is an easy one, written in Java, and therefore multi-platform by nature :
http://www.parosproxy.org/index.shtml
Also, if you do not use it already, you should install firebug :
https://addons.mozilla.org/fr/firefox/addon/1843
The network monitoring tab should fit your needs.


Question:
How can you remove all of the trailing whitespace of an entire project? Starting at a root directory, and removing the trailing whitespace from all files in all folders.

Also, I want to to be able to modify the file directly, and not just print everything to stdout.

Answer:
In Bash:
```
find dir -type f -exec sed -i 's/ *$//' '{}' ';'
```
Note: If you're using
```
.git
```
repository, try adding:
```
-not -iwholename '.git'
```
.


Question:
I am seeing some rather weird behavior with PowerShell, it looks like custom functions might need a "parenthesis wrapper" to evaluate as you might expect them. Given a simple PowerShell function:

function Return-True { return $true }

and then some sample code to invoke it:

PS C:\> Return-True
True
PS C:\> Return-True -eq $false
True
PS C:\> (Return-True) -eq $false
False

Ideas? Comments?

Answer:
The second line is not doing a boolean evaluation. Look at what happens if you do the same thing with strings.
```
```
PS C:\> function Return-True { return "True string" }
PS C:\> Return-True
True string
PS C:\> Return-True -eq "False string"
True string
PS C:\> (Return-True) -eq "False string"
False
```
```
The second line is simply returning the value of the function, and not doing a comparison. I'm not sure exactly why this behavior is happening, but it makes the behavior easier to see than when using boolean values that are being converted to the strings "True" and "False".


Question: We have a Windows machine running SQL Server 2005, and we need to be able to run some database queries on it from a Linux box.  What are some of the recommended ways of doing this?  Ideally, we would want a command-line utility similar to sqlcmd on Windows.

Answer:
FreeTDS
+
unixODBC
or
iODBC
Install first FreeTDS, then configure one of the two ODBC engines to use FreeTDS as its ODBC driver. Then use the commandline interface of the ODBC engine.
unixODBC has isql, iODBC has iodbctest
You can also use your favorite programming language (I've successfully used Perl, C, Python and Ruby to connect to MSSQL)
I'm personally using FreeTDS + iODBC:
```
```
$more /etc/freetds/freetds.conf
[10.0.1.251]
    host = 10.0.1.251
    port = 1433
    tds version = 8.0

$ more /etc/odbc.ini
[ACCT]
Driver = /usr/local/freetds/lib/libtdsodbc.so
Description = ODBC to SQLServer via FreeTDS
Trace = No
Servername = 10.0.1.251
Database = accounts_ver8
```
```


Question: In a C++ Linux app, what is the simplest way to get the functionality that the Interlocked functions on Win32 provide?  Specifically, a lightweight way to atomically increment or add 32 or 64 bit integers?

Answer:
Upon further review,
this
looks promising.  Yay stack overflow.


Question: Does it make sense to qualify bit fields as signed / unsigned?

Answer: if a 'bit' is signed, then you have a range of -1, 0, 1, which then becomes a ternary digit. I don't think the standard abbreviation for that would be suitable here, but makes for interesting conversations :)


Question:
I believe that we can allow Firefox to sent NTLM data to SharePoint sites to do automatic authentication, and I think that this is doable with IIS.

I'd like to do the same thing with an internal Rails site.

Does anyone know of way that I could authenticate NTLM type user information through a Apache/mongrel setup (provided of course that it's already running on a Windows box inside of an Active Directory domain)?

Answer:
I'm assuming you've already worked out which HTTP headers you need to send in order to get firefox and IE to send back the NTLM authentication stuff, and are just needing to handle that on the server side?
You could use some of ruby's
win32 libraries
to access the underlying windows authentication functions which handle the NTLM.
I'd suggest the path of least resistance might be to see if there is a COM component which can do the authentication for you, and if so, to use it using the Win32OLE ruby library.
If there's no COM component, you might be able to find something in one of those other libraries which can invoke the native win32 methods for you.
If you can't find that, you'd have to write a ruby C extension. I've done this on linux, and extending ruby is pretty easy, but you may find the microsoft authentication API's a bit painful.
Hope that gets you started on the right track :-)


Question:
I'm no UNIX Guru, but I've had to set up a handful of slices for various web projects. I've used the articles on there to set up users, a basic firewall, nginx or apache, and other bits and pieces of a basic web server.

I foresee more slice administration in my future. Is there a more efficient way to set up users, permissions, and software on a clean slice than configuration by hand?

Answer:
Depending on the number of machines you might find it makes sense to use something like CFEngine, or Puppet, to configure the new installs.
That brings your work down to configuring each new machine as a CFEngine, for example, client.  Then that may be used to install the packages, edit files, & etc.
There are a few articles I wrote on the subject, with a Debian bias, here:
http://www.debian-administration.org/tag/cfengine


Question:
I have a table with rowID, longitude, latitude, businessName, url, caption. This might look like:

rowID | long  | lat |  businessName | url | caption

  1      20     -20     Pizza Hut   yum.com  null

How do I delete all of the duplicates, but only keep the one that has a URL (first priority), or keep the one that has a caption if the other doesn't have a URL (second priority) and delete the rest?

Answer:
Here's my looping technique.  This will probably get voted down for not being mainstream - and I'm cool with that.
```
```
DECLARE @LoopVar int

DECLARE
  @long int,
  @lat int,
  @businessname varchar(30),
  @winner int

SET @LoopVar = (SELECT MIN(rowID) FROM Locations)

WHILE @LoopVar is not null
BEGIN
  --initialize the variables.
  SELECT
    @long = null,
    @lat = null,
    @businessname = null,
    @winner = null

  -- load data from the known good row.
  SELECT
    @long = long,
    @lat = lat,
    @businessname = businessname
  FROM Locations
  WHERE rowID = @LoopVar

  --find the winning row with that data
  SELECT top 1 @Winner = rowID
  FROM Locations
  WHERE @long = long
    AND @lat = lat
    AND @businessname = businessname
  ORDER BY
    CASE WHEN URL is not null THEN 1 ELSE 2 END,
    CASE WHEN Caption is not null THEN 1 ELSE 2 END,
    RowId

  --delete any losers.
  DELETE FROM Locations
  WHERE @long = long
    AND @lat = lat
    AND @businessname = businessname
    AND @winner != rowID

  -- prep the next loop value.
  SET @LoopVar = (SELECT MIN(rowID) FROM Locations WHERE @LoopVar < rowID)
END
```
```


Question:
In Linux environment, when getting "glibc detected *** free(): invalid pointer" errors, how do I identify which line of code is causing it?

Is there a way to force an abort? I recall there being an ENV var to control this?

How to set a breakpoint in gdb for the glibc error?

Answer:
In general, it looks like you might have to recompile glibc, ugh.
You don't say what environment you're running on, but if you can recompile your code for OS X, then its version of libc has a free() that listens to this environment variable:
```
```
MallocErrorAbort             If set, causes abort(3) to be called if an
                              error was encountered in malloc(3) or
                              free(3) , such as a calling free(3) on a
                              pointer previously freed.
```
```
The man page for free() on OS X has more information.
If you're on Linux, then try
Valgrind
, it can find some impossible-to-hunt bugs.


Question:
I need to scan uploaded files for viruses on a Linux server, but I'm not sure how to go about it.

What are my options, if any? I'm also interested in how the scanners perform when multiple users send multiple files at the same time.

Answer:
I would have a look at
Clam AntiVirus
. It provides a
```
clamscan
```
program that can scan a given file and return a pass/fail indication. It's free and automatically updates its database regularly.
As for integrating such a product into your file upload process, that would be specific to whatever file upload process you actually use.


Question:
So how does someone create a music visualizer? I've looked on Google but I haven't really found anything that talks about the actual programming; mostly just links to plug-ins or visualizing applications.

I use iTunes but I realize that I need Xcode to program for that (I'm currently deployed in Iraq and can't download that large of a file). So right now I'm just interested in learning "the theory" behind it, like processing the frequencies and whatever else is required.

Answer:
Typically, you take a certain amount of the audio data, run a frequency analysis over it, and use that data to modify some graphic that's being displayed over and over. The obvious way to do the frequency analysis is with an
FFT
, but simple
tone detection
can work just as well, with a lower lower computational overhead.
So, for example, you write a routine that continually draws a series of shapes arranged in a circle. You then use the dominant frequencies to determine the color of the circles, and use the volume to set the size.


Question: For some reason the Windows command prompt is "special" in that you have to go to a properties dialog to resize it horizontally rather than just dragging the corner of the window like every other app. Unsurprisingly this feature made it into P-P-P-Powershell as well -- is there any way around this via command prompt replacement or Windows hackery?

Answer:
2019 Update:
Microsoft has released the terminal app on
Github
& the
Windows Store
, and it has tabs, panels, acrylic transparency, and other features.
2016 Update:
Windows 10's default conhost UI has more features, including free resize, transparency, etc (this includes cmd & powershell)
I now use
ConEmu
(walkthrough
here
) which has many features including tabs & split panes.
Other options include
Cmder
(which comes with additional tools built in), and
ConsoleZ
(a fork of Console2).
Console
appears to no longer be updated


Question:
Is it possible to define a timestamp column in a MySQL table that will automatically be updated every time a field in the same row is modified? Ideally this column should initially be set to the time a row was inserted.

Cheers,
Don

Answer:
That is the default functionality of the
timestamp
column type.  However, note that the format of this type is yyyymmddhhmmss (all digits, no colons or other separation).
EDIT:
The above comment about the format is only true for versions of MySQL < 4.1... Later versions format it like a
DateTime


Question: How exactly can I create a new directory using Emacs? What commands do I use? (If possible, please provide an example)

Answer:
to create the directory
```
dir/to/create
```
, type:
```
```
M-x make-directory RET dir/to/create RET
```
```
to create directories
```
dir/parent1/node
```
and
```
dir/parent2/node
```
, type:
```
```
M-! mkdir -p dir/parent{1,2}/node RET
```
```
It assumes that Emacs's inferior shell is
```
bash
```
/
```
zsh
```
or other compatible shell.
or in a
```
Dired
```
mode
```
```
+
```
```
It doesn't create nonexistent parent directories.
Example:
```
```
C-x d *.py RET ; shows python source files in the CWD in `Dired` mode
+ test RET     ; create `test` directory in the CWD
```
```
```
CWD
```
stands for Current Working Directory.
or just create a new file with non-existing parent directories using
```
C-x C-f
```
and type:
```
```
M-x make-directory RET RET
```
```
Emacs asks to create the parent directories automatically while saving a new file in recent Emacs versions. For older version, see
How to make Emacs create intermediate dirs - when saving a file?


Question: Please provide tips for effectively using git with svn. What are your "best practices"?

Answer:
Here's some that I recently learned:
always do
```
git svn rebase
```
before doing
```
git svn dcommit
```
when you are doing
```
dcommit
```
, do it from a temporary staging branch - if you (or git) mess up, it's much easier to recover by just deleting the branch and starting over
When
```
svn dcommit
```
dies halfway through a large commit and you seem to have lost all of your history,
do this
:
How To Recover:
First, open .git/logs/HEAD
Find the hash of the commit that's the
  head of your git repo. Hopefully you
  remember the commit message and can
  figure it out, but it should be pretty
  obvious
Back in your now f-ed up working-dir:
git reset --hard <hash from log>
This gets your working dir back to
  where it was before you did a git- svn
  dcommit.  Then:
git-svn rebase git-svn dcommit


Question:
What GNU/Linux command-line tool would I use for performing a search and replace on a file?

Can the search text, and replacement, be specified in a regex format?

Answer:
Perl was invented for this:
```
```
perl -pi -e 's/foo/bar/g;' *.txt
```
```
Any normal s/// pattern in those single quotes. You can keep a backup with something like this:
```
```
perl -pi.bak -e 's/foo/bar/g;' *.txt
```
```
Or pipeline:
```
```
cat file.txt | perl -ne 's/foo/bar/g;' | less
```
```
But that's really more sed's job.


Question: I have a table of users which has a username column consisting of a six digit number e.g 675381, I need to prepend a zero to each of these usernames e.g. 0675381 would be the final output of the previous example, is there a query that could handle this?

Answer:
```
```
UPDATE Tablename SET Username = Concat('0', Username);
```
```


Question:
I need to list all files whose names start with 'SomeLongString'. But the case of 'SomeLongString' can vary. How?

I am using zsh, but a bash solution is also welcome.

Answer:
Depending on how deep you want to have this listing,
```
find
```
offers quite a lot
in this regard:
```
```
find . -iname 'SomeLongString*' -maxdepth 1
```
```
This will only give you the files in the current directory. Important here is
the
```
-iname
```
parameter instead of
```
-name
```
.


Question: I want to run a command as soon as a certain text appears in a log file. How do I do that in Bash?

Answer:
Use command
```
```
tail -f file.log | grep --line-buffered "my pattern" | while read line
do
  echo $line
done
```
```
The
```
--line-buffered
```
is the key here, otherwise the read will fail.


Question:
My team has been using SVN for a few years. We now have the option of switching to Perforce.

What would be the benefits (and pitfalls) of making such a switch?

Answer:
I currently use both on different projects.
The perforce branching mechanism is superior.
The perforce conflict resolve tool is better.
I really like perforce's strong notion of a changelist.
Perforce seems faster.
It's easier to set up and get running.
Some of our members really like the MS Office plugin for perforce, I'm on a Mac so I can't use it.
But
The SVN clients are better, especially the eclipse plugin.
Perforce is more expensive.
These are merely opinions, so perhaps this is a poor answer :)
If I was already using one or the other, I'd be very hard pressed to switch since neither seems to offer really significant benefits over the other, but the disruption in switching could be large.
Update: Since writing this, I've completely switched to using GIT for both personal and commercial purposes.  I would pick it over either SVN or Perforce any day.


Question:
I'm about to write a little GUI app that will sit in the system tray, doing a little FTP and ODBC.

I'd like to develop in Linux, if possible.

What would you recommend?

Thanks a bunch!

Answer: Mono by Miguel de Izaca - now owned / sponsored by Novell. It gives you 90% of the .NET framework in Linux.


Question: I have some decimal data that I am pushing into a SharePoint list where it is to be viewed.  I'd like to restrict the number of significant figures displayed in the result data based on my knowledge of the specific calculation.  Sometimes it'll be 3, so 12345 will become 12300 and 0.012345 will become 0.0123.  Occasionally it will be 4 or 5.  Is there any convenient way to handle this?

Answer:
As I remember it "significant figures" means the number of digits after the dot separator so 3 significant digits for 0.012345 would be 0.012 and not 0.0123, but that really doesnt matter for the solution.
I also understand that you want to "nullify" the last digits to a certain degree if the number is > 1. You write that 12345 would become 12300 but im not sure whether you want 123456 to become 1230000 or 123400 ? My solution does the last. Instead of calculating the factor you could ofcourse make a small initialized array if you only have a couple of variations.
```
```
private static string FormatToSignificantFigures(decimal number, int amount)
{
    if (number > 1)
    {
        int factor = Factor(amount);
        return ((int)(number/factor)*factor).ToString();
    }

    NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
    nfi.NumberDecimalDigits = amount;

    return(number.ToString("F", nfi));
}

private static int Factor(int x)
{
    return DoCalcFactor(10, x-1);
}

private static int DoCalcFactor(int x, int y)
{
    if (y == 1) return x;
    return 10*DoCalcFactor(x, y - 1);
}
```
```
Kind regards
Carsten


Question: According to the manual, git dcommit “will create a revision in SVN for each commit in git.” But is there a way to avoid multiple Subversion revisions? That is, to have git merge all changes prior to performing the svn commit?

Answer:
If you work on a branch in git, you can
```
git-merge --squash
```
, which does that within git.  You could then push that one squashed commit to SVN.
Of course, lots of small commits are good, so why would you want to squash them?


Question: Could someone explain to me in simple terms the easiest way to change the indentation behavior of Vim based on the file type? For instance, if I open a Python file it should indent with 2 spaces, but if I open a Powershell script it should use 4 spaces.

Answer:
Put autocmd commands based on the file suffix in your ~/.vimrc
```
```
autocmd BufRead,BufNewFile   *.c,*.h,*.java set noic cin noexpandtab
autocmd BufRead,BufNewFile   *.pl syntax on
```
```
The commands you're looking for are probably ts= and sw=


Question:
I mean 100+ MB big; such text files can push the envelope of editors.

I need to look through a large XML file, but cannot if the editor is buggy.

Any suggestions?

Answer:
Free read-only viewers:
Large Text File Viewer
(Windows) – Fully customizable theming (colors, fonts, word wrap, tab size). Supports horizontal and vertical split view. Also support file following and regex search. Very fast, simple, and has small executable size.
klogg
(Windows, macOS, Linux) – A maintained fork of
glogg
. Its main feature is regular expression search. It supports monitoring file changes (like
```
tail
```
), bookmarks, highlighting patterns using different colors, and has serious optimizations built in. But from a UI standpoint, it's rather minimal.
LogExpert
(Windows) – "A GUI replacement for
```
tail
```
." It's really a log file analyzer, not a large file viewer, and in one test it required 10 seconds and 700 MB of RAM to load a 250 MB file. But its killer features are the columnizer (parse logs that are in CSV, JSONL, etc. and display in a spreadsheet format) and the highlighter (show lines with certain words in certain colors). Also supports file following, tabs, multifiles, bookmarks, search, plugins, and external tools.
Lister
(Windows) – Very small and minimalist. It's one executable, barely 500 KB, but it still supports searching (with regexes), printing, a hex editor mode, and settings.
Free editors:
Your regular editor or IDE. Modern editors can handle surprisingly large files. In particular,
Vim
(Windows, macOS, Linux),
Emacs
(Windows, macOS, Linux),
Notepad++
(Windows),
Sublime Text
(Windows, macOS, Linux), and
VS Code
(Windows, macOS, Linux) support large (~4 GB) files, assuming you have the RAM.
Large File Editor
(Windows) – Opens and edits TB+ files, supports Unicode, uses little memory, has XML-specific features, and includes a binary mode.
GigaEdit
(Windows) – Supports searching, character statistics, and font customization. But it's buggy – with large files, it only allows overwriting characters, not inserting them; it doesn't respect LF as a line terminator, only CRLF; and it's slow.
Builtin programs (no installation required):
less
(macOS, Linux) – The traditional Unix command-line pager tool. Lets you view text files of practically any size. Can be installed on Windows, too.
Notepad
(Windows) – Decent with large files, especially with word wrap turned off.
MORE
(Windows) – This refers to the Windows
```
MORE
```
, not the Unix
```
more
```
. A console program that allows you to view a file, one screen at a time.
Web viewers:
readfileonline.com
– Another HTML5 large file viewer. Supports search.
Paid editors/viewers:
010 Editor
(Windows, macOS, Linux) – Opens giant (as large as 50 GB) files.
SlickEdit
(Windows, macOS, Linux) – Opens large files.
UltraEdit
(Windows, macOS, Linux) – Opens files of more than 6 GB, but the configuration must be changed for this to be practical: Menu » Advanced » Configuration » File Handling » Temporary Files » Open file without temp file...
EmEditor
(Windows) – Handles very large text files nicely (officially up to 248 GB, but as much as 900 GB according to one report).
BssEditor
(Windows) – Handles large files and very long lines. Don’t require an installation. Free for non commercial use.
loxx
(Windows) – Supports file following, highlighting, line numbers, huge files, regex, multiple files and views, and much more. The free version can not: process regex, filter files, synchronize timestamps, and save changed files.


Question:
I've been using this long command:

svn st | awk '/\?/ {print $2}' | xargs svn add

Similarly, to svn rm files I accidentally deleted with normal rm with :

svn st | awk '/\!/ {print $2}' | xargs svn rm --force

I guess I can write a bash function to do these two, but I'd prefer an interactive add/rm like the one git has.

Answer:
I use a generalization of the command line that you run, called
```
svnapply.sh
```
. I did not write it, but I don't remember where I found it. Hopefully, the original author will forgive me for reposting it here:
```
```
#!/bin/bash
#
# Applies arbitrary commands to any svn status. e.g.
#
# Delete all non-svn files (escape the ? from the shell):
# svnapply \? rm
#
# List all conflicted files:
# svnapply C ls -l

APPLY=$1
shift

svn st | egrep "^\\${APPLY}[ ]+" | \
sed -e "s|^\\${APPLY}[ ]*||" | \
sed -e "s|\\\\|/|g" | \
xargs -i "$@" '{}'
```
```
Per the comments, the script allows you to run arbitrary commands against all files with the same status.
Update:
It would not be too difficult to write a script that takes a file path as an argument and prompts the user for add/delete and then does the appropriate thing for that file. Chaining that together with the above script would get you what you want.


Question: I'm a Linux guy and I'm used to copying directory trees with cp -a. OS X doesn't have -a option on cp... but it does have the ditto command. I'm reading the man on ditto now, but is there anything I should specifically be looking out for?

Answer:
As j04t pointed out that should be
```
cp -pR
```
On OS X, you would do
```
```
cp -dRL src target
```
```
cp preserves resources in newer version of OS X (was it 10.3 when that happened?)
Hey d is kinda like an upsidedown p, right ;)


Question:
I'm running autoconf and configure sets SHELL to '/bin/sh'.
This creates huge problems. How to force SHELL to be '/bin/bash' for autoconf?

I'm trying to get this running on osx, it's working on linux. Linux is using SHELL=/bin/bash. osx defaults to /bin/sh.

Answer:
```
ln -f /bin/bash /bin/sh
```
:-P (No, it's not a serious answer. Please don't hose your system by doing it!)


Question: How do I resolve merge conflicts in my Git repository?

Answer:
Identify which files are in conflict (Git should tell you this).
Open each file and examine the diffs; Git demarcates them.  Hopefully it will be obvious which version of each block to keep.  You may need to discuss it with fellow developers who committed the code.
Once you've resolved the conflict in a file
```
git add the_file
```
.
Once you've resolved
all
conflicts, do
```
git rebase --continue
```
or whatever command
Git said to do when you completed.


Question:
When running performance tests file system cache hit or miss can significantly influence test results. Therefore generally before running such tests used files are evicted from system cache. How to do that on Linux?

Clarification: If possible, the solution should not require root privileges.

Answer:
As a superuser you can do the following:
To free pagecache:
echo 1 > /proc/sys/vm/drop_caches
To free dentries and inodes:
echo 2 > /proc/sys/vm/drop_caches
To free pagecache, dentries and inodes:
echo 3 > /proc/sys/vm/drop_caches
This operation will not "lose" any data (caches are written out to disk before their data is dropped), however, to really make sure all cache is cleaned, you should sync first. E.g. all caches should be cleared if you run
```
```
sync; echo 3 > /proc/sys/vm/drop_caches
```
```
As I said, only a superuser (root) may do so.