> For the complete documentation index, see [llms.txt](https://olb.gitbook.io/linux-essentials-certification/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://olb.gitbook.io/linux-essentials-certification/3.the-power-of-command-line/3.2-searching-extracting-data.md).

# 3.2 Searching extracting

**Weight 3**\
Key Knowledge Areas

* Command line pipes
* I/O redirection
* Basic Regular Expressions using `.` `[ ]` `*` and `?`

Partial list of the used files, terms and utilities

* `grep`
* `less`
* `cat`, `head`, `tail`
* `sort`
* `cut`
* `wc`

***

## I/O Redirection

### Standard Output (stdout)

* `echo "Hello World!" > text` : operator `>` redirect standard normal output (`stdout`) - create a new file or overwrite if exist
* `echo "Hello World!" >> text` : add new information to text file

### Standard Error (stderr)

* `find /usr games 2> text-error` : operator `2>` redirect standard error output (`stderr`)
* `/dev/null` : bit bucket (a file that accepts input and doesn’t do anything with it)

### Standard intput (stdin)

* `cat < text` or `tr -d "l" < text`
* `<<` operator acts in a different way. This input stream is also called *here document*.

### Combination

* `find /usr games &>> text-error` : `&` is combination of channel 1 (stdout) and channel 2 (stderr)

### CLI Pipes

* `ls -l | head | wc -w`

### Useful Commands

* `cut` Removes sections from each line of a file.
* `cat` Displays or concatenates files.
* `find` Searches for files in a directory hierarchy.
* `less` Displays a file, allowing the user to scroll one line at the time.
* `more` Displays a file, a page at the time.
* `head` Displays the first 10 lines of a file.
* `tail` Displays the last 10 lines of a file.
* `sort` Sorts files.
* `wc` Counts by default the lines, words or bytes of a file.
* `grep` : *global regular expression print* - search within files
  * `-i` the search is case insensitive
  * `-r` the search is recursive (it searches into all files within the given directory and its subdirectories)
  * `-c` the search counts the number of matches
  * `-v` invert the match, to print lines that do not match the search term
  * `-E` turns on extended regular expressions (needed by some of the more advanced meta-characters like | , + and ?)

### Regular expressions

* `.` Match any single character (except newline)
* `[abcABC]` Match any one character within the brackets
* `[^abcABC]` Match any one character except the ones in the brackets
* `[a-z]` Match any character in the range
* `[^a-z]` Match any character except the ones in the range
* `sun|moon` Find either of the listed strings
* `^` Start of a line
* `$` End of a line
* `*` Zero or more of the preceding pattern
* `+` One or more of the preceding pattern
* `?` Zero or one of the preceding pattern
