1

Kind of amused here, I'm trying to pass the prereqs to get into the Udacity Course on High Performance Computer Architecture by Georgia Institute of Technology For the life of me I can't figure out what they want,

Question reads,

To list the contents of a file, with line numbers for non-blank line shown use the command with an option: ________ ___ filename.

I've tried nl. It didn't work. Most bizarre question ever.

Question

1
  • 4
    Not sure we need an image of the textual question, given that you've reproduced the question in text...
    – Jeff Schaller
    Apr 18, 2018 at 0:27

3 Answers 3

7

While cat's numbering is not POSIX, nl is, and POSIX also defines the numbering styles that nl uses:

−b type  Specify which logical page  body  lines  shall  be  numbered.
         Recognized types and their meaning are:

         a       Number all lines.

         t       Number only non-empty lines.

         n       No line numbering.

         pstring Number  only  lines  that  contain  the basic regular
                 expression specified in string.

         The default type for logical page body shall be t (text lines
         numbered).

So, even though it is the default:

nl -bt filename
0
1

Apparently, cat also has that option,

-b, --number-nonblank number nonempty output lines, overrides -n

So now I guess cat -b and nl are the same. Joys!

$ cat -b ./foo.py 
     1  a = 5
     2  a = a + 1

     3  print "foobarbaz" + str(a);

$ nl ./foo.py 
     1  a = 5
     2  a = a + 1

     3  print "foobarbaz" + str(a);
1
  • 3
    wtf. this seems so esoteric that I can't imagine why cat would bother implementing such a feature (especially since nl and cat are both part of coreutils). And it makes me think the authors of that question deliberately went looking for something nobody would know.
    – phemmer
    Apr 18, 2018 at 1:56
0

'nl' offers more formatting options than 'cat -b.'

Punctuation such as periods "." or any other character can follow the line number, and varying amounts of white space may also be added with the '-s' option.

Number only non-empty lines:

$ echo -e "one\ntwo\nthree\n\n\nfour\n\nfive\nsix\nseven\n\neight\n\n\n\nnine\nten\n"|nl -bt -s\.\  -
 1. one
 2. two
 3. three


 4. four

 5. five
 6. six
 7. seven

 8. eight



 9. nine
10. ten

More whitespace between the line number and the line:

$ echo -e "one\ntwo\nthree\n\n\nfour\n\nfive\nsix\nseven\n\neight\n\n\n\nnine\nten\n"|nl -bt -s\.\ \ \ \ \ \  -
 1.      one
 2.      two
 3.      three


 4.      four

 5.      five
 6.      six
 7.      seven

 8.      eight



 9.      nine
10.      ten

Two asterisks after the line number:

$ echo -e "one\ntwo\nthree\n\n\nfour\n\nfive\nsix\n\n"|nl -bt -s**\ \ \ \ \ \ \  -
 1**       one
 2**       two
 3**       three


 4**       four

 5**       five
 6**       six

Some characters like parentheses must be escaped from the shell with backslashes:

$ echo -e "one\ntwo\nthree\n\n\nfour\n\nfive\nsix\n\n"|nl -bt -s\(\(\ \ \ \ \ \ \  -
 1((       one
 2((       two
 3((       three


 4((       four

 5((       five
 6((       six

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.