File this under #shell #functions I should have written years ago:

function grepc { #Do a grep -c, but skipping files with no results grep -c "$@" |grep -v ':0$' }

#unix #UnixShell #ShellScripting #bash #ksh

@rl_dane you need "$@"

@mirabilos

Really? Even if I'm supplying options like grepc -i foo *.md?
Or should I use IFS=$'\n'?

Seems you were right! I need to skim that part of the manpage again. 😋

@rl_dane yes. And absolutely no, do not touch IFS for this.

"$@" basically means “pass the argv array as-is”.

@mirabilos

What happens if you forget the double quotes?

@rl_dane it becomes equivalent to $* without double quotes

@mirabilos

Interesting, that feels kinda counter-intuitive, since not quoting things usually keeps them separate, and quoting them lumps them together, like:

~ $ string="hello there" ~ $ for x in $string; do echo $x; done hello there ~ $ for x in "$string"; do echo $x; done hello there ~ $
@rl_dane yes, but "$@" is special, just learn it as a term of its own
@mirabilos @rl_dane

it's easier to think of $@ as an array type to me

if you quote a string, it goes as is, if you don't, it is split
if you quote an array, it goes as is, if you don't, each entry is split

this would feel a lot less "special case-y" if we had proper arrays variables other than arguments... i'm working on it...
@navi @rl_dane well it’s the same for ${arr[*]} vs. ${arr[@]}
@navi @rl_dane so the @ means “the array itself” vs. * “all array members as a list of (IFS0-joined) words”
@navi @rl_dane so if you do $* it doesn’t just split all array elements, it joins them first then splits the result (this is true)
@navi @rl_dane this is a very good summary of what it is, really… I’ve been trying to get more “programming language thinking” into shell anyway, e.g. when I say that “a string is not split if expanded in scalar context” like it’s Perl (but foo=$bar or case $baz in is scalar context)

@rl_dane

Oh, didn't know about -c. I usually just pipe to wc -l I guess.

@amin

-c, -l, -h, -H, and -q are my favorite #grep flags. :D

Huh, that almost became a [Marcel Duchamp] reference. 😅

Marcel Duchamp - Wikipedia

@rl_dane

I just use -v and -E

@rl_dane

Heh, I just got to run grep grep on my shell history.

@amin @rl_dane you guys use flags?... :p

@sotolf @amin

🏴‍☠️

@rl_dane @amin

$ Shiver me timbers, comb through this here scroll and fetch me anythin' about pirates, but none o' them feathered devils!

@sotolf @rl_dane

Not usually, but -v is useful to select anything that doesn't match, and -E lets me use extended Regex, which is occasionally useful.

@amin @rl_dane Yeah, it was mostly a joke, I do use -v at some times, but I usually have enough of an idea of what I look for, and am not really doing much complex scripting, so I don't usually need the complex stuff that grep can do :)

@sotolf @amin

grep -vxf file2 file1 is very useful for displaying lines that are in file1 but not file2, like a position/sort-independent diff

-x matches whole lines, and of course -f reads regexps from a file.

@rl_dane @sotolf

I use comm for comparing files.

@amin @sotolf

There are so many little unix utilities I keep forgetting about, like comm. XD

I have used that before. It's nice.

@rl_dane @sotolf

I need to finish my "all the coreutils" blog post…

@amin @rl_dane @sotolf You guys still use grep instead of ripgrep. Tst

@thedoctor @rl_dane @sotolf

I use ripgrep if I want to, say, search a whole codebase.

@thedoctor @amin @sotolf

...and bash instead of zsh
...and grep/awk/sed instead of jq
...and firefox instead of chrome
...and the fediverse instead of facebook

Face it... I'm an unpopular-opinion neckbeard level boss. XD

cc: @mirabilos

@rl_dane @thedoctor @amin @sotolf mksh instead of GNU bash, grep/ed/sed/xmlstarlet/jq instead of awk, lynx (and firefox) instead of firefox, and… well I’m still in IRC and occasionally Usenet…

@rl_dane @thedoctor @sotolf @mirabilos

...and grep/awk/sed instead of jq

Okay, now that one is crazy.

@thedoctor @amin @sotolf @mirabilos

LISTEN, I've used grep/awk/sed for very nearly a quarter century, and I find the syntax of jq bewildering, so 😝

@rl_dane @thedoctor @sotolf @mirabilos

Most of those aren't for handling complex data types, though, just manipulating lines.

@amin @thedoctor @sotolf @mirabilos

So far, I've managed to bodge it together just well enough to do what I needed, but I really would like to get more familiar with jq.

@amin @rl_dane @thedoctor @sotolf jq is absolutely bewildering, especially the insistence on filtering (splitting the input stream is so hard and counter-intuitive), but it’s an ok tool if you have JSON. Or YAML, which I [convert (user=pass public)] to JSON anywwy.

xmlstarlet is lovely!

@mirabilos @amin @thedoctor @sotolf

Is it possible to convert JSON to XML and then use xmlstarlet? 😄

XEP-0239: Binary XMPP

@kabel42 @rl_dane @amin @thedoctor @sotolf … yes, but not in a meaningful way.

@kabel42 @mirabilos @amin @thedoctor @sotolf

Sufficiently evil. XD

I remember when XML was the buzzword back in the late naughties.

@rl_dane @kabel42 @sotolf @thedoctor @amin XML is underrated, and Google hates it

@mirabilos @kabel42 @sotolf @thedoctor @amin

Well, if Google hates it, I'm probably on board by default. XD

@rl_dane @amin @thedoctor @sotolf yes, but you’ll have to find out a schema you want to use.

Of course, you could just convert…

{ "foo": ["bar", "baz"], "bar": true }

… to…

<JSONObject> <element name="foo"><JSONArray> <element><JSONString>bar</JSONString></element> <element><JSONString>baz</JSONString></element> </JSONArray></element> <element name="bar"><JSONBoolean>true</JSONBoolean></element> </JSONObject>

… or worse (Apple “plist”), but I suspect you’d rather have something semantically correct.

A lazy way to do this is with [iXML] (“invisible XML”, via @linguacelta of “AIn’t” fame); note you’ll most likely want the draft spec, not the release, because the latter misses a way to specify an XML alias and just uses the grammar name, which must be unique, for the XML element name (so the above mapping, which has object elements and array elements, would not be possible with the 1.0 spec).

That being said, if you do want such a systematic mapping, I could write you a little jq script to do that.

Do note that U+0000 (ouch! I wish it weren’t), U+0001‥U+0008, U+000B‥U+000C, U+000E‥U+001F, U+FFFE‥U+FFFF (ouch²!) are valid in JSON but not XML (and (X)HTML forbids further codepoints). xmlstarlet will likely refuse a document in which these occur (and no, even writing them as numeric entities like &#1; is not permitted).

Invisible XML

@rl_dane @mirabilos @thedoctor @sotolf

I do not feel up to subjecting my notifications to a revival of this thread right now. Bye y'all. :)

@rl_dane @mirabilos @amin @thedoctor conversion is always a dangerous step

@rl_dane Those are so not comparable!

@amin @sotolf @mirabilos

@thedoctor @rl_dane @amin @mirabilos At least bash and zsh is comparable to grep ripgrep, as zsh is just a strictly better bash ;)
@rl_dane @sotolf @amin @thedoctor strictly even slower 😹
@mirabilos @rl_dane @amin @thedoctor Well, I see you have never tried fish :p
@kabel42 @mirabilos @rl_dane @amin @thedoctor No, it's absolutely not fun, first of all it breaks all scripts. They break all the shortcuts that I use all the time in interactive sessions, it's slow, and if you want to configure it, guess how you configure a command line tool it boots up a web server where you configure it... the whole thing is utterly ridiculous.
@mirabilos @rl_dane @amin @thedoctor @sotolf you probably have some browser installed anyway so why not use it. And you can rewrite all your scripts in a new, more sane, language that is not based on some legacy tech. 
@mirabilos @rl_dane @amin @thedoctor @sotolf extra fun if you install it on a shared server in the lab and make it the default shell.

@kabel42 I see you also like to see the world burn.

@mirabilos @rl_dane @amin @sotolf

@kabel42 @mirabilos @rl_dane @amin @thedoctor On most of the servers that I manage, no I don't have a browser installed, why would I? And fish's language, sane? what you smoking bro? I want some of that as well.
@rl_dane @kabel42 @sotolf @thedoctor @amin I do have lynx, but also the feeling that that isn’t what you are thinking of when you write browser
@rl_dane @sotolf @thedoctor @amin @mirabilos fish, the shell for people that prefer mouse over keyboard