---
# System prepended metadata

title: Regular Expression

---

# Regular Expression

## Number and letter

| Expression  | match                   |
| ----------- | ----------------------- |
| [0-9]       | Any number              |
| [a-z]       | Any Lower case letters  |
| [A-Z]       | Any Bigger case letters |
| [a-zA-Z]    | Any letters             |
| [0-9a-zA-Z] | Any number and letters  |

## Character

| Expression  | match                                                        |
| ----------- | ------------------------------------------------------------ |
| **.**       | Match any Character<br/>e.g., search google, “g.e”           |
| **\***       | Match any Character include change line<br/>e.g., search google ccde, “g*e” |
| **.***      | Match none or any character                                  |
| **\[0-9\]\[0-9\]\*** | Match one or more number<br/>e.g., 0, 11, 2552, 8886695 |
| **\[A-Z\]\[A-Z\]\* or \[a-z\]\[a-z\] \*** | Match one or more letter<br/>e.g., Y、IJK、ZZZZZZ |
| **.\*k** | Match any character until meet ‘k’ |
| **'ca\(r\\ \|n\\)'** | Match both character<br/>e.g., car, can|



# Search command

 ## Grep

| Command | Note               |
| ------- | ------------------ |
| grep “search string” “File name” | Show the result of search string |
| ls /etc/ \| grep network | List file name which have “network” in /ect/  |
| grep -i “search string” | Not case sensitive |
| grep -n “search string” | Show line number |
| grep -v “search string” | Search inverse result |
| grep -r “search string” | Search in Directory and its subdirectories |
| grep -E “search string” | Search in Extended Regular Expression mode. <br> e.g., 'ca\(r\\ \|n\\)' to 'ca(r\|n)' |

## Sed

**Format**:

sed \[-OPTION\] \[ADD1\]\[,ADD2\] \[COMMAND\] \[/PATTERN\]\[/REPLACEMENT\]/\[FLAG\] \[FILE]

| option | Note                           |
| ------ | ------------------------------ |
| -e     | Just excute, not write back.   |
| -f     | Use parameter by external file |
| -r     | Use regular expression         |
| -i     | Write back result direct       |

**Example**

| Command | Note               |
| ------- | ------------------ |
| sed -e 's/cpu/memory/' test.txt  ||
| sed -i -e 's/cpu/memory/i' test.txt  ||
| sed 's/a/an/' \| sed 's/apple/APPLE/' | “this is a apple” to “this is an APPLE” |

## Awk

For awk variable:

| variable | Content               |
| ------- | ------------------ |
| $0 | drwxr-xy-x 2 aaa aaa 4096 2011-09-07 11:44 Desktop |
| $1 | drwxr-xy-x |
| $2 | 2 |
| $3 | aaa |
| $4 | aaa |
| $5 | 4096 |
| $6 | 2011-09-07 |
| $7 | 11:44 |
| $8 | Desktop |

**example**

| variable | Content               |
| ------- | ------------------ |
| ls -l \| awk '{print $5,$8}' | Print $5 and $8 |
| ls -l \| awk '{print "File",$8,"size =",$5,"Byte"}' | Combine another string |
| ls -l \| awk '{print "File",$8,"size =",$5/1024,"KB"}' | For calculate |

# Example

| Command                                                      | Note                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| (grep -n 192.168.56.255 <<< $(ifconfig)) \| awk -F' ' '{print $1}' | find IP in ifconfig, get row number by grep -n, print result by awk, select print result by \$1,\$2,\$n |
![grep example](https://i.imgur.com/VoObP5T.png)

| Command                                                      | Note                                                         |
| ------------------------------------------------------------ | ------------------------------------------------------------ |
| (ifconfig \| grep -n inet) \| awk -F' ' 'NR==1{print $3}' | find IP in ifconfig, get row number by grep -n, print result by awk, select print row by NR== _num_, select print result by \$1,\$2,\$n|
![](https://i.imgur.com/GlxVTxt.png)
