If you're coming back to this tutorial after class, or just ran across it on the Internet, here are the main 3 files we use. You can download them and follow along if you have a Unix or Linux laptop or server. Not all examples shown will work, and what you see in your Terminal may not match the description, but you can still practice many operations using these files. |
First things first. Text you want to manipulated often resides in files, and one of the first things you want to do is look at it.
But you're on a command line, so you don't have a handy GUI program you can use, like Notepad on Windows or TextEdit on Macs. So you'll usually use a Linux command to view text.
The most basic way of view file data is the cat command. While the name comes from its ability to concatenate one or more files, it can be used to output the contents of a single file. For example:
cat haiku.txt |
Each line of the file is printed to the Terminal (standard output), and you see the command prompt again when it is done.
One of the most useful options is cat -n, which displays a line number with each line of output. We can use this option to see that the haiku.txt file has 11 lines:
student01@gsafcomp01:~$ cat -n haiku.txt
1 The Tao that is seen
2 Is not the true Tao, until
3 You bring fresh toner.
4
5 With searching comes loss
6 and the presence of absence:
7 "My Thesis" not found.
8
9 Yesterday it worked
10 Today it is not working
11 Software is like that. |
Using cat by itself is fine for small files, but it reads/writes everything in the file without stopping. So for larger files you use a pager such as more, or less.
A pager reads text and outputs only one "page" of text at a time, then waits for you to ask it to advance. A "page" of text is the number of lines that will fit on your visible Terminal. Compare these:
cat jabberwocky.txt more jabberwocky.txt # press the space bar to go to the next page |
Notice that when you call more, it outputs some text then stops and indicates that there is additional text available (--More--).
So more is really simple, and is great for scanning through a large file. But what if you're looking for something in particular? Or want to go back and forth? That's where the less pager comes in, providing navigation and search capabilities using its own special characters (metacharacters).
Basic navigation in less:
Searching in less:
The searched-for pattern will appear highlighted, at the top of the Terminal window. There may be other highlighted occurrences farther down, but the current one will always be at the top.
Everything in Linux is case-sensitive by default, so "foo" and "Foo" are different text strings. However if you use the less -I (Insensitive) flag, searching will be case-insensitive, and "foO", "Foo", "FOO" will all match the pattern "foo".
So how do you tell where you are in the file? If you use the less -N option, each line will be labeled with a Number.
On what lines of the jabberwocky.text file does the word Jabberwock appear (exact match)?
less -N jabberwocky.txt then to see the 1st occurrence /Jabberwock to see other occurrences, just type n |
Lines 12, 21 and 28 |
What lines is the word Jabberwock on if case is ignored?
less -N -I jabberwocky.txt |
Lines 1, 12, 21 and 28 |
Using the less program's search function is one way to find text in a file, especially when you want to see the context surrounding the searched-for text. But for general text searching, the grep program is used more frequently.
The word grep stands for general regular expression parser.
Nearly every programming language offers grep functionality, where a pattern you specify – a regular expression or regex – describes how the search is performed.
In Unix, the grep program performs regular-expression text searching, and displays lines where the pattern text is found.
There are many grep regular expression metacharacters that control how the search is performed. This is a complex topic and we defer it to the Intermediate Unix workshop (for a preview, see the grep command).
For now, we will just use grep <pattern> <file> where <pattern> just contains alphanumeric characters (A-Z, a-z, 0-9), and use options:
Note that less and grep both support case-insensitive matching, and displaying line numbers, but they use slightly different options:
It would be great if all Linux command options that mean the same thing used the same options – and some do, but some don't. |
Use grep to display (numbered) jabberwocky.txt lines containing the word Jabberwock ignoring case.
grep -n -i Jabberwock jabberwocky.txt |
A key to text manipulation is understanding Unix streams. Every command and Unix program has three "built-in" streams: standard input, standard output and standard error.

Most programs/commands read input data from some source, then write output to some destination. A data source can be a file, but can also be standard input. Similarly, a data destination can be a file but can also be a stream such as standard output.
The power of the Linux command line is due in no small part to the power of piping. The pipe operator ( | ) connects one program's standard output to the next program's standard input.
![]()
The key to the power of piping is that most Unix commands can accept input from standard input instead of from files. So, for example, these two expressions appear equivalent:
more jabberwocky.txt cat jabberwocky.txt | more |
Let's dissect the difference in detail:
Notes:
Use the pipe operator to provide jabberwocky.txt data to the less command so that line numbers are displayed.
|
What happens when you just enter the cat command with no arguments? Can you explain why?
man cat |
Just entering the cat command with no arguments appears to "hang" – that is, nothing happens and you don't see the command prompt (just Ctrl-c to get it back). Reading the man page for cat says this:
The SYNOPSYS says in addition to one of more options to cat ( Since there was no FILE provided, cat reads from standard input – but there's no data there either, so it just sits and waits for some to appear. |
Two other commands that are useful for viewing text are head and tail.
With no options, head shows the first 10 lines of its input and tail shows the last 10 lines. You can use the -n option to specify how many lines to view, or just put the number you want after a dash (e.g. -5 for 5 lines or -1 for 1 line).
head -n 3 haiku.txt head -3 haiku.txt cat haiku.txt | head -3 |
tail -n 1 haiku.txt tail -1 haiku.txt cat haiku.txt | tail -1 |
But what if you want to see lines in the middle of a file? Here's where a special feature of tail comes in handy. If you use tail and put a plus sign (+) in front of the number (with or without the -n option), tail will start its output at that line. Let's pipe line-numbered output from cat to tail to see how this works:
cat -n haiku.txt | tail -n 7 # display the last 7 lines of haiku.txt cat -n haiku.txt | tail -n +7 # display text in haiku.txt starting at line 7 cat -n haiku.txt | tail +10 # display text in haiku.txt starting at line 10 |
When you use the tail -n +<integer> syntax it will display all output from that line to the end of its input. So to view only a few lines starting at a specified line number, pipe the output to head:
# display 2 lines of haiku.txt starting at line 9 cat -n haiku.txt | tail -n +9 | head -2 cat -n haiku.txt | head -10 | tail -2 |
Use cat, head and tail to display the middle stanza of haiku.txt.
Use cat -n to see the numbering of haiku.txt lines, then a combination of head/tail or tail/head. |
There are three 3-line stanzas in haiku.txt. The middle stanza is lines 5-7.
|
Sometimes a line of text is longer than the width of your Terminal. In this case the text is wrapped. It can appear that the output is multiple lines, but it is not. We can see that by looking at lines of the mobydick.txt file, that has some very long lines:
tail -1 mobydick.txt cat -n mobydick.txt | more |
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 too many lines of text for its height.
So how long is a line? And how many lines are there in a file? The wc (word count) command can tell us this.
Examples:
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 |
When you give wc -l multiple files, it reports the line count of each, then a total.
student01@gsafcomp01:~$ wc -l haiku.txt jabberwocky.txt 11 haiku.txt 37 jabberwocky.txt 48 total |
How long is the 12th line of jabberwocky.txt?
Use tail and head to isolate the 12th line |
It is 32 characters long (including the linefeed):
|
Note the slight difference when you give wc -l a file name versus when you pipe input to it.
|
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?
On standard Unix systems, each text character is stored as one byte – eight binary bits – in a format called ASCII (American Standard Code for Information Interchange). Eight bits can store 2^8 = 256 values, numbered 0 - 255.
In its original form values 0 - 127 were used for standard ASCII characters. Now values 128 - 255 comprise an Extended set. See https://www.asciitable.com/
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:
Let's use the hexdump command (really an alias, see below) to look at the actual ASCII codes stored in a file:
head haiku.txt | hexdump |
This will produce output something like this:
![]()
Each line here describes 16 characters, in three display areas:
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.
The expression below defines a helpful hexdump alias for viewing file contents in hexadecimal (base 16). An alias is just shorthand for calling another command, usually with a specific set of options you like. Here, the hexdump alias calls the built-in od function with various arguments that we'd never remember. alias hexdump='od -A x -t x1z -v' We have defined the hexdump alias for you in your ~/.profile file, the dot file that is executed every time you login. But you can just enter that expression on the command line and the hexdump function will be defined in your current Terminal session. |