4 ms·
I am firmly in the "ugh" camp. I strongly suspect that the fawning that occurs over pipelines is because of sentimentality more than practicality. Extracting in
by zamalek 6y ago
I am firmly in the "ugh" camp. I strongly suspect that the fawning that occurs over pipelines is because of sentimentality more than practicality. Extracting information from text using a regular expression is fragile. Writing fragile code brings me absolutely no joy as a programmer - unless I am trying to flex my regex skills.
If you really look at how pipelines are typically used is: lines are analogous to objects and [whatever the executable happens to use] delimiters are analogous to fields. Piping bare objects ("plain old shell objects") makes far more sense, involves far less typing and is far less fragile.
- shakna 6y agoIf you're having to extract your data using a regex, then the data probably isn't well-formed enough for a shell pipeline. It's doable, but a bad idea. Regex should not be the first hammer you reach for, because it's a scalpel. I recently wanted cpu cores + 1. That could be a single regex. But this is more maintainable, and readable: echo '1 + '"$(grep 'cpu cores' /proc/cpuinfo | tail -n1 | awk -F ':' '{print $2}')" | bc There's room for a regex there. Grep could have done it, and then I wouldn't need the others... But I wouldn't be able to come back in twelve months and instantly be able to tell you what it is doing.
- zamalek 6y agoWould anyone be able to approach this, without knowledge of /proc/cpuinfo, and understand what `$2` refers to? > Regex should not be the first hammer you reach for, because it's a scalpel. You used a regex tool: grep.
- jandrese 6y agoIt's plaintext, you could just look at it. That's the beauty of text based systems.
- jandrese 6y agoThat seems overly complicated to me. Can you not just: grep '^processor' /proc/cpuinfo | wc -l
- Laremere 6y agoCompare to using a real programing language, such as Go: return runtime.NumCPU() + 1 Sure, there needs to be os to program (and possibly occasionally program to program) communication, but well defined binary formats are easier to parse in a more reliable manner. Plus they can be faster, and moved to an appropriate function.
- rakoo 6y agoThis is where you need to use awk. awk -F ':' '/cpu cores/ {a = $2 + 1} END {print a}' /proc/cpuinfo
- lsofzz 6y ago> This is where you need to use awk. I think need is a strong statement in this context. echo $(( 1 + $(grep 'cpu cores' /proc/cpuinfo | head -1 | awk -F ':' '{print $2}') )) Choosing shell vs. [insert modern] programming is a matter of trade-off of taste, time, $$$, collective consensus (in a team setting) and so forth. [edit]: Forgot code syntax on HN.
- deleted 6y ago[deleted]
- eulers_secret 6y agoOP wants number of CORES, not CPUs (excluding HT 'cores'). I know this is old, but if someone finds their way here looking for a solution... In BASH number of CPUs + 1 is as easy as: echo $(($(getconf _NPROCESSORS_ON)+1)) or echo $(($(nproc)+1)) getconf even works on OS X! Many of the other solutions confuse the two. As far as I can tell, using OPs solution is one of the better ways to get core count. Could also: echo $(($(lscpu -p | tail -n 1 | cut -d',' -f 2)+2)) ...but OPs solution is probably easier to read...