Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. In the 1st, the more command reads some input from the jabberwocky.txt file
    • then writes the output to standard output, which is displayed on your Terminal
    • it pauses at page boundaries (--More--) waiting for input on standard input
    • when it receives a space character on on standard input it reads more input from jabberwocky.txt
    • then writes the output to standard output, which is displayed on your Terminal
  2. In the 2nd, the cat command reads its input from the jabberwocky.txt file
    • then writes its output to standard output
    • the pipe operator ( | ) then connects the standard output from cat to standard input of the more command
    • the more command then reads its input from standard input, instead of from a file
      • then writes its output to standard output, which is displayed on your Terminal
      • more continues its processing similar to #1, except reading from its standard input instead of the file

...

Code Block
languagebash
cat -n haiku.txt | tail -n 5   # display the last 5 lines of haiku.txt

cat -n haiku.txt | tail -n +5  # display text in haiku.txt starting at 
                               #  line 5
cat -n haiku.txt | tail +6     # display text in haiku.txt starting at 
                                # line 6

When you use the tail -n +<integer> syntax it will display all output input starting from that line to until the end of its input. So to view only a few lines starting at a specified line number, pipe the output to head:

...

Note that most Terminals let you increase/decrease the width/height of the Terminal window. But there will always be single lines too long for your Terminal width and or too many lines of text for its height.

...

Code Block
languagebash
wc -l mobydick.txt            # Reports the number of lines in the
                              # mobydick.txt file
cat mobydick.txt | wc -l      # Reports the number of lines of its input

tail -1 mobydick.txt | wc -c  # Reports the number of characters of the last mobydick.txt line

...

 in 
                              # the last mobydick.txt line
head -5 mobydick.txt | wc -c  # Reports the total number of characters in
                              # the first 5 mobydick.txt lines

When you give wc -l multiple files, it reports the line count of each, then a total.

...

Tip

Note the slight difference when you give wc -l a file name versus when you pipe input to it.

  • wc -l <filename> displays the number of lines then and the file name.
  • cat <filename> | wc -lonly displays the number of lines in its anonymous input from standard input.

...

We've talked about viewing text using various Unix commands – but what exactly is text? That is, what is stored in files that the shell interprets as text?

Inside of files, text isn't characters at all – it is all numbers, because that's all computers know.

On standard Unix systems, each text character is stored as one byteeight binary bits – in a format called ASCII (American Standard Code for Information Interchange). Eight bits can store 2^8 = 256 values, numbered 0 - 255.

...

However not all ASCII "characters" are printable -- in fact the "printable" characters start at ASCII 32 (space).

ASCII values 0 - 31 have special meanings. Many were designed for use in early modem protocols, such as EOT (end of transmission) and ACK (acknowledge), or for printers, such as VT (vertical tab) and FF (form feed).

The non-printable ASCII characters we care most about are:

  • Tab (decimal 9, hexadecimal 0x9, octal 0o011)
    • backslash escape: \t
  • Linefeed/Newline (decimal 10, hexadecimal 0xA, octal 0o012)
    • backslash escape: \n
  • Carriage Return (decimal 13, hexadecimal 0xD, octal 0o015)
    • backslash escape: \r

...

  • The numeric offset of the 16-character line, in hexadecimal (base 16)
    • 16 decimal is 0x10 hex
  • The numeric value (ASCII code) for each character, again in hexadecimal
    • each 2-digit hex number represents one 8-bit byte/character
  • The translated text
    • The display character associated with each ASCII code, or a period ( . ) for non-printable charactersthe , written between a greater than ( > ) and less than ( < ) sign

Notice that spaces are ASCII 0x20 (decimal 32), and the newline characters appear as 0x0a (decimal 10).

Why hexadecimal? Programmers like hexadecimal (base 16) because it is easy to translate hex digits to binary, which is how everything is represented in computers. And it can sometimes be important to know which binary bits are 1s and which are 0s. See Decimal and Hexadecimal for more information.

...