How to reduce whitespace from JSON on the command line without installing a tool - json

I've used python -m json.tool to pretty print JSON and check for errors for years. It is nice because it works on just about any system with python installed. How can I take out unnecessary whitespace that JSON ignores (i.e. not in string values) without installing on mac/linux/bsd/unix systems without installing anything special? Answers that work on recent baseline mac, freebsd and debian/ubuntu would be great.

I'm not a code golfer but I puzzled out this solution which makes a nice alias with some escaping:
python '-cimport json,sys;json.dump(json.load(sys.stdin),sys.stdout,separators=(",", ":"))'

Related

git log --pretty=format windows strange behavior

I reduce the problem to its lowest terms.
Under linux a git command like this:
git log --pretty=format:{"commit":"%H"}
gives me a valid json element, so something like:
{"commit":"20cafdecc9898113ac6215ae70cd7622dc2cae3b"}
under windows I obtain a not-valid json element, because in some way windows seems to remove the double-quotes elements and I obtain:
{commit:20cafdecc9898113ac6215ae70cd7622dc2cae3b}
Do you know why, or how can I fix it making it work under both os?
Thank you!
I assume that your shell on Linux is NOT a bash shell because my bash shell on Linux gives me the same output as on Windows - without double-quotes.
The doubles-quotes are special characters for most shells - so you have to protect them either by "escaping" or by "quoting".
git log --pretty=format:{\"commit\":\"%H\"}
git log --pretty='format:{"commit":"%H"}'
This works for bash on Linux and bash on Windows. I have not tested it with cmd on Windows.

"Fatal error: 'EXTERN.h' file not found" while installing Perl modules

While trying to install Perl modules like JSON::XS or YAML::XS, i receive the same error:
XS.xs:1:10: fatal error: 'EXTERN.h' file not found
I use MacBook, xCode is up to date, everything else that could help is up to date too.
Since OS X El Capitan, Apple introduced System Integrity Protection which restricts writing to /usr/lib /usr/bin and other sensitive directories (even to root or sudo user) that are used by the installation of Perl bundled with the Operating System. This can cause issues when it comes to installing new modules and also if trying to install XS modules ( those linked to external C libraries ).
For this reason you should not consider the default Perl installation as a working development environment, especially if you are installing custom modules.
Check out this thread on PM and others. I had since El-Capitan managed to solve this before by manually building from tarball and adding a few params or environment variables to set the paths believing that it would be best to retain use of the system Perl but this is not the way to go. This makes your environment difficult to build but also brittle and sensitive to OS updates that may either break things in many different ways.
The best practice seems to be starting with a Perl using brew install perl and work in this environment, remembering to setup your bash_profile as directed by the installer.
Also worth remembering to do a brew link perl. If you receive warnings about this clobbering what looks like system Perl libraries don't worry - these are likely modules that were installed by you over the top and it will cause you less trouble to link over these. If you have concerns, make a note of which module installs will be cleared and re-install them once your environment is configured ( ie your module installer approach is configured using cpanm or sticking with the old perl -MCPAN -e shell etc)
This new Perl setup from brew eliminates the need to continuing running sudo which adds another layer of things that can go wrong as environment variables don't follow through and permission conflicts arise etc.
Finally to simplify package/module installation I suggest doing a brew install cpanminus. If you had previously already installed this, you can ensure the paths etc are configured by doing a brew reinstall cpanminus
If you want to take it another step further then you can install perlbrew as well which will give you the ability to run multiple versions of Perl as your user and configure these with their own libs and modules which can be very useful particularly if aligning with your production environment for testing etc.
One problem you may face if moving from system Perl to this kind of approach is needing to deal with any hangovers from installing things with sudo. It wis worth taking a little time to get all this set up right though and your issues going forward will be greatly reduced and you won't be left with that nagging feeling that you don't want to change anything for fear of it all breaking.
I have also come across a Perl Blog Article that suggests a fix for XS issues with perlbrew on Mojave
This Gist described updating your cpan shell install root though this shouldn't be necessary unless your cpan is stuck in an old config after taking steps above.
I've also raised this as a new issue on PerlMonks
After reading https://developer.apple.com/documentation/xcode_release_notes/xcode_10_release_notes#3035624 and installing the Additional headers via
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /
I successfully compiled without the missing 'EXTERN.h' error
In order to follow the common advice I also tried with Perlbrew to install a dedicated development version of Perl. Especially with the advice in mind First, do not use the system Perl on MacOS. The installed version is for Apple, not for you (see the discussion here: https://www.perlmonks.org/?node_id=1224727).
Unfortunately, the following error occurred:
Test Summary Report
-------------------
porting/libperl.t (Wstat: 65280 Tests: 35 Failed: 0)
Non-zero exit status: 255
Parse errors: No plan found in TAP output
Files=2653, Tests=1217766, 708 wallclock secs (52.74 usr 9.40 sys + 395.38 cusr 49.90 csys = 507.42 CPU)
Result: FAIL
make: *** [test_harness] Error 1
##### Brew Failed #####
Therefore, I decided to install it the following way (and not following the advice due to the error).
Even after having the above mentioned macOS SDK headers already installed on Catalina (macOS 10.15.2) it didn't work for me. I faced the issue during the installation of the Perl module Mac-SystemDirectory-0.13. The following steps (by identifying the missing file in hope of having a more generic approach for more or less equivalent issues) did the trick:
Locate the header file (in this case EXTERN.h)
sudo find /Library -type f -name EXTERN.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE/EXTERN.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Perl/5.28/darwin-thread-multi-2level/CORE/EXTERN.h
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE/EXTERN.h
Ensure the installed Perl version (here 5.18) match the header file:
perl -v | grep version
This is perl 5, version 18, subversion 4 (v5.18.4) built for darwin-thread-multi-2level
Export the path for the C-Compiler (note MacOSX10.15.sdk for Catalina and Perl Version 5.18)
export CPATH=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE
Invoke the Makefile.PL with perl
perl Makefile.PL
BTW — For anybody who's still struggling with this, my workaround was:
bash% module="Sub::Util" # For example
bash% cpanm --configure-args="INC=-I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE" "$module"
Please try this
CPATH=$(dirname $(find /usr/local/Cellar/ -name EXTERN.h)) cpan JSON::XS
For Big Sur and perl 5.30, EXTERN.h is at /Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk/System/Library/Perl/5.30/darwin-thread-multi-2level/CORE
I'm trying to upgrade CPAN itself and got that error. But I have /usr/bin/cpan and I can't write there so I have to tweak it to write the updated version to /usr/local/bin/cpan.
No promises, but yum install perl-devel worked for me.
As #huyz has helpfully pointed out, if you hit this error on a Mac, you don't have this option, even though this is probably your issue, and you need to follow one of the above methods of getting a version of Perl that isn't missing important chunks, as per other answers.
But if, dear reader, you hit this error on a linux host, as I did, then this might be an option for you.
Building on what E Lisse suggested, you might also have luck looking in
/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/
For example:
CPATH=$(dirname $(find /System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/ -name EXTERN.h)) cpan JSON::XS
You could also find where EXTERN.h is located and add that to your shell by default, e.g. in your .bashrc or .zshrc file:
export CPATH=/System/Volumes/Data/Library/Developer/CommandLineTools/SDKs/MacOSX12.sdk/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE/

vim-rest-console displays JSON in a single line

I've found out about vim-rest-console, but after installed and run a test, I noticed the JSON it outputs is only in one line, without any indentation. This is horrible and I'd like to know if there is a way to solve this. I am using NeoVim on Xubuntu 16.04 LTS.
To fix the json indentation in Vim, put the following command to your .vimrc
nmap =j :%!python -m json.tool<CR>
and launch it via the =j command (or remap to different shortcut).

How to use the left and right arrow keys in the tclsh interactive shell (Ubuntu 14.04)?

Why can't I use the left and right arrow keys (actually, the same goes for the up and down keys as well) to move about the line I'm currently on in the tclsh interactive shell? If I try to press either one, I get a bunch of abracadabra instead of moving back and forth. This is not all that convenient when, for example, you make a typo, but you can't move the cursor back to change it. You have to use the backspace key to erase all the stuff that you've typed after the place where the typo is located thereby destroying all your work. Is it possible to fix this, quite frankly, buggy behaviour?
The behaviour isn't buggy. It is inconvenient yes.
To get editing in a shell, usually the GNU readline library is used. If a program doesn't use that library, you don't have that feature.
For tclsh there are licensing reasons (GPL vs. BSD style Tcl license), which make it inconvenient to add readline support directly to tclsh for all those platforms where readline is not part of the operating system (nearly everything but Linux).
You can use the Ubuntu rlwrap package to still get the editing you want.
Install rlwrap:
sudo apt-get install rlwrap
And use it to run tclsh with command line editing:
rlwrap -c tclsh
Another option would be to use the Tk based shell tkcon, which provides a bit more options as a Tcl shell, its available as a ubuntu package too.
Expanding my own answer: You can also build and use tclreadline, as a package in your .tclshrc.
You can use the the generic rlwrap as suggested by schlenk, or you can use tclreadline:
Install tclreadline (e.g. with the following command for debian/ubuntu):
sudo apt install tclreadline
Automatically load it by adding it to your ~/.tclshrc:
if {$tcl_interactive} {
package require tclreadline
::tclreadline::Loop
}
Using tclreadline does not only provide the basic editing features but also includes tab completion, which rlwrap can't do due to it's generic nature.

Can I print html files (with images, css) from the command-line?

I want to print styled html pages with their images from a script. Can anyone suggest an open-source solution?
I'm using linux (Ubuntu 8.04) but would be also be interested in solutions for other operating systems.
You could give html2ps a try, it is written in Perl, so I guess it wil run on any operating system that runs Perl. It does support CSS and images. It does not render as good as you may perhaps want.
To use in Debian/Ubuntu sudo aptitude install html2ps and then pipe the output to lpr to print:
html2ps \
http://stackoverflow.com/questions/286583 \
|lpr
Or pipe the output to ps2pdf to convert to a pdf file:
html2ps \
http://stackoverflow.com/questions/286583 \
|ps2pdf - stackoverflow.pdf
You have a ton of options: html2ps, html2pdf, a huge list at: http://www.hypernews.org/HyperNews/get/www/html/converters.html
But personally I would recommend going with htmldoc it does postscript, PDF and is up to date (most recent release was <2 weeks ago. You'll need to use a tool like wget or elinks to actually download the HTML file and it's components.
If your install has kde on it then you could launch konqueror and use dcop or dbus to send commands to it. It means that you have to have x running though, which may not work for you.
I´m not sure if this code works with mono, but it should work with Windows and the "real" .NET Framework at least:
http://pietschsoft.com/post/2008/07/C-Generate-WebPage-Thumbmail-Screenshot-Image.aspx