...
Notice that when you call more, it outputs some text then stops and indicates that there is additional text available (--More-- text available).
- Just press the spacebar to see the next page.
- If there is additional output, you'll see the --More-- indicator again; if not, the command prompt appears again.
- To end the more display, just type q (quit) or Ctrl-c.
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).
...
The word grep stands for general regular expression parser, and nearly .
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 note that , and use options:
- grep -i will perform a case-insensitive search
...
- grep -n will display line numbers where the pattern was matched
...
| Tip |
|---|
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. |
...
| Expand | ||
|---|---|---|
| ||
grep -n -i Jabberwock jabberwocky.txt |
Standard streams and piping
...
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:
...
- 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 on on standard input it reads more input from jabberwocky.txt
- then writes the output to standard output, which is displayed on your Terminal
- 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 standard input instead of the file
...
- In #2, the cat command "blocks" writing to its standard output until more says it's ready for more input
- This "write until block" / "read when input available" behavior makes streams a very efficient means of inter-process communication.
- In #1, more can report how much of the file has been read, e.g. --More-- (24%) because it has access to the size information for the file it is reading.
- But in #2, the text is "anonymous input" – from standard input – so more doesn't know how much of the total has been provided.
...
| Expand | |||||
|---|---|---|---|---|---|
| |||||
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 their there either, so it just sits and waits for some to appear. |
...