0

I have the following bash script called countscript.sh

  1 #!/bin/bash
  2 echo "Running" $0
  3 tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed $1 q

But I don't understand how to pass the argument correctly: ( "3" should be the argument $1 of sed).

$ echo " one two two three three three" | ./countscript.sh 3
Running ./countscript.sh
sed: -e expression #1, char 1: missing command

This works fine:

    $ echo "one two three four one one four" | tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed 3q
  3 one
  2 four
  1 two

Thanks.

PS: Anybody else noticed the

bug in this script on page 10, https://www.cs.tufts.edu/~nr/cs257/archive/don-knuth/pearls-2.pdf ?

4
  • The whitespace is a bit off; the pipeline in the linked paper should probably be read as sed ${1}q, not sed ${1} q. Commented Apr 29, 2022 at 12:40
  • The paper was also written in a different time; it should be quoted as sed "${1}q", at least. Commented Apr 29, 2022 at 12:41
  • By way of explanation, the sed script 3q indicates that sed should exit after reading the 3rd line. By default, sed outputs each line that it reads. Commented Apr 29, 2022 at 12:42
  • Thanks a lot, the extra white space was the problem. Commented Apr 29, 2022 at 12:51

2 Answers 2

1

In the quoted paper, I think you are misreading

sed ${1}q

as

sed ${1} q

and sed does not consider 3 by itself a valid command. The separate argument q is treated as an input file name. If the value of $1 did result in a single valid sed script, you would have likely gotten an error for the missing input file q.

Proper shell programming would dictate this be written as

sed "${1}q"

or

sed "${1} q"

instead; with the space as part of the script, sed correctly outputs the first $1 lines of input and exits.


It's somewhat curious that the authors used sed instead of head - "$1" to output the first few lines, as one of them (McIlroy) essentially invented the idea of the Unix pipeline as a series of special-purpose, narrowly focused tools. Not having read the full paper, I don't know what Knuth and McIlroy's contributions to the paper were; perhaps Bentley just likes sed. :)

Sign up to request clarification or add additional context in comments.

Comments

1

When running the following command:

$ echo " one two two three three three" | ./countscript.sh 3

the special variable $1 will be replaced by 3, your first argument. Hence, the script runs:

tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed 3 q

Notice the space between the 3 and the q. sed does not know what to do, because you give it no command (3 is not a command).

Remove the space, and you should be fine.

tr -cs A-Za-z '\n' | tr A-Z a-z | sort | uniq -c | sort -rn | sed "${1}q"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.