WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: more shell,...  (Read 9406 times)

Offline mocore

  • Hero Member
  • *****
  • Posts: 641
  • ~.~
more shell,...
« on: May 02, 2019, 06:16:38 PM »

i found some talk about programing history at
 https://web.archive.org/web/20130209012208/http://www.leancrew.com/all-this/2011/12/more-shell-less-egg/

and interesting example of the method + explanation 

Code: [Select]
tr -cs A-Za-z '\n' |
tr A-Z a-z |
sort |
uniq -c |
sort -rn |
sed ${1}q

Quote
If you are not a UNIX adept, you may need a little explanation, but not much, to understand this pipeline of processes.
The plan is easy:

    Make one-word lines by transliterating the complement (-c) of the alphabet into newlines (note the quoted newline), and squeezing out (-s) multiple newlines.
    Transliterate upper case to lower case.
    Sort to bring identical words together.
    Replace each run of duplicate words with a single representative and include a count (-c).
    Sort in reverse (-r) numeric (-n) order.
    Pass through a stream editor; quit (q) after printing the number of lines designated by the script’s first parameter (${1})

 8)


Offline jazzbiker

  • Hero Member
  • *****
  • Posts: 934
Re: more shell,...
« Reply #1 on: May 03, 2019, 12:59:37 AM »
)))
Knuth is like himself and nobody else. He proposed such task knowing well how it can be done in bash, i expect. But he wanted to show, how beautyfuly can be wriiten this simple task using his programming approach. Maybe he wanted to tell, that small can't be beautiful?
TeX is smth beatiful, but in the same time very big one, too.

Offline mocore

  • Hero Member
  • *****
  • Posts: 641
  • ~.~
Re: more shell,...
« Reply #2 on: August 10, 2019, 10:12:14 AM »

 wrt "The Yoda of Silicon Valley" ::)
there is a interesting profile @ nytimes.com/2018/12/17/science/donald-knuth-computers-algorithms-programming.html


Offline PDP-8

  • Hero Member
  • *****
  • Posts: 915
Re: more shell,...
« Reply #3 on: August 17, 2019, 05:26:11 AM »
The author of that article wonders why McIlroy used sed in the last line instead of head - wondering if head hadn't been invented yet.

McIlroy, (the guy who proposed pipes in the mid 60's, but finally got it implemented in unix early on in the 70's with constant brow-beating Ken about it) wouldn't use head or tail - being a Berkeleyism convenience short-circuiting the toolbox concept.

Heh, if you want head or tail, use sed commands to do it and perhaps alias it by those names, or make it an executable script.  Remove those from busybox and relive the true spirit of the day!

Kind of similar to the cat -v "bloat" . :)


« Last Edit: August 17, 2019, 05:30:18 AM by PDP-8 »
That's a UNIX book! - cool  -- Garth

Offline mocore

  • Hero Member
  • *****
  • Posts: 641
  • ~.~
Re: more shell,...
« Reply #4 on: August 20, 2019, 09:59:06 AM »
if you want head or tail,
use sed commands to do it and
perhaps alias it by those names,
or make it an executable script. 
Remove those from busybox and
relive the true spirit of the day!

interesting idea

looking @ the source
https://git.busybox.net/busybox/tree/coreutils/head.c

or even just the usage
Quote
busybox head --help
BusyBox v1.29.3 (2018-12-19 15:48:20 UTC) multi-call binary.

Usage: head [OPTIONS] [FILE]...

Print first 10 lines of each FILE (or stdin) to stdout.
With more than one FILE, precede each with a filename header.

   -n N[kbm]   Print first N lines
   -n -N[kbm]   Print all except N last lines
   -c [-]N[kbm]   Print first N bytes
   -q      Never print headers
   -v      Always print headers

N may be suffixed by k (x1024), b (x512), or m (x1024^2).

i think if a sh / scripted head was created
using only sed it would be a bit of a merde head script !

for simple cases i guess sed is no problem
but if you intend to replace head.c it might get messy if you want to include ENABLE_FEATURE_FANCY_HEAD functionality

eg
the  m/b/k suffixed  bytes options  parsing
 eg:  10k

might well be possible in sed script , my thinking is it would be simpler to write in awk
iv read awk can be used/useful for prototyping code/ideas ... into other lang's 
..so it should be possible to transcribe from somotherlang to awk ?


converting human to bytes
imho would be easy in awk ( perhaps if you exclude the option parsing , and passing vars into awk )
and examples exist
all though as with many things i found this can also be scripted with .sh

 ???
The origin of the linked post was a book about literate programing so with that in mind 

it might be interesting idea to write such a head.sh shell script in markdown  :o ..
with mdsh 
Quote
Multi-lingual, Markdown-based Literate Programming... in run-anywhere bash
markdown
literate-programming
bash
shell-scripting
scripting-language

if you can tolerate bash as build dependency  :P

« Last Edit: August 20, 2019, 10:06:52 AM by mocore »