> For the complete documentation index, see [llms.txt](https://alomancy.gitbook.io/guides/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://alomancy.gitbook.io/guides/cheat-sheets/bash-cheat-sheets/grep.md).

# Grep

## Grep

### **Type of grep**

```
grep = grep -G # Basic Regular Expression (BRE)
fgrep = grep -F # fixed text, ignoring meta-charachetrs
egrep = grep -E # Extended Regular Expression (ERE)
pgrep = grep -P # Perl Compatible Regular Expressions (PCRE)
rgrep = grep -r # recursive
```

### **Grep and count number of empty lines**

```
grep -c "^$"
```

### **Grep and return only integer**

```
grep -o '[0-9]*'
#or
grep -oP '\d'
```

### **Grep integer with certain number of digits (e.g. 3)**

```
grep ‘[0-9]\{3\}’
# or
grep -E ‘[0-9]{3}’
# or
grep -P ‘\d{3}’
```

### **Grep only IP address**

```
grep -Eo '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}'
# or
grep -Po '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
```

### **Grep whole word (e.g. 'target')**

```
grep -w 'target'

#or using RE
grep '\btarget\b'
```

### **Grep returning lines before and after match (e.g. 'bbo')**

```
# return also 3 lines after match
grep -A 3 'bbo'

# return also 3 lines before match
grep -B 3 'bbo'

# return also 3 lines before and after match
grep -C 3 'bbo'
```

### **Grep string starting with (e.g. 'S')**

```
grep -o 'S.*'
```

### **Extract text between words (e.g. w1,w2)**

```
grep -o -P '(?<=w1).*(?=w2)'
```

### **Grep lines without word (e.g. 'bbo')**

```
grep -v bbo filename
```

### **Grep lines not begin with string (e.g. #)**

```
grep -v '^#' file.txt
```

### **Grep variables with space within it (e.g. myvar="some strings")**

```
grep "$myvar" filename
#remember to quote the variable!
```

### **Grep only one/first match (e.g. 'bbo')**

```
grep -m 1 bbo filename
```

### **Grep and return number of matching line(e.g. 'bbo')**

```
grep -c bbo filename
```

### **Count occurrence (e.g. three times a line count three times)**

```
grep -o bbo filename |wc -l
```

### **Case insensitive grep (e.g. 'bbo'/'BBO'/'Bbo')**

```
grep -i "bbo" filename
```

### **COLOR the match (e.g. 'bbo')!**

```
grep --color bbo filename
```

### **Grep search all files in a directory(e.g. 'bbo')**

```
grep -R bbo /path/to/directory
# or
grep -r bbo /path/to/directory
```

### **Search all files in directory, do not ouput the filenames (e.g. 'bbo')**

```
grep -rh bbo /path/to/directory
```

### **Search all files in directory, output ONLY the filenames with matches(e.g. 'bbo')**

```
grep -rl bbo /path/to/directory
```

### **Grep OR (e.g. A or B or C or D)**

```
grep 'A\|B\|C\|D'
```

### **Grep AND (e.g. A and B)**

```
grep 'A.*B'
```

### **Regex any single character (e.g. ACB or AEB)**

```
grep 'A.B'
```

### **Regex with or without a certain character (e.g. color or colour)**

```
grep ‘colou?r’
```

### **Grep all content of a fileA from fileB**

```
grep -f fileA fileB
```

### **Grep a tab**

```
grep $'\t'
```

### **Grep variable from variable**

```
$echo "$long_str"|grep -q "$short_str"
if [ $? -eq 0 ]; then echo 'found'; fi
#grep -q will output 0 if match found
#remember to add space between []!
```

### **Grep strings between a bracket()**

```
grep -oP '\(\K[^\)]+'
```

### **Grep number of characters with known strings in between(e.g. AAEL000001-RA)**

```
grep -o -w "\w\{10\}\-R\w\{1\}"
# \w word character [0-9a-zA-Z_] \W not word character
```

### **Skip directory (e.g. 'bbo')**

```
grep -d skip 'bbo' /path/to/files/*
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://alomancy.gitbook.io/guides/cheat-sheets/bash-cheat-sheets/grep.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
