Text Processing Tools
===
###### tags: `IBM` `Linux Tools`
## grep
- [grep 指令用法教學與範例](https://blog.gtwang.org/linux/linux-grep-command-tutorial-examples/)
## awk
- [awk的基礎用法和高級用法](https://www.itread01.com/content/1524235443.html)
- [gawk-Specific Regexp Operators](https://www.gnu.org/software/gawk/manual/html_node/GNU-Regexp-Operators.html)
```
netstat -nalt | awk '/^tcp\>/{state[$NF]++}END{for(stat in state){printf "%15s: %-10d\n",stat,state[stat]}}'
```
:::info
* ```\<```
Matches the empty string at the beginning of a word. For example, ``/\<away/`` matches ‘away’ but not ‘stowaway’.
* ```\>```
Matches the empty string at the end of a word. For example, ``/stow\>/`` matches ‘stow’ but not ‘stowaway’.
:::
- gsub(r, s, t)
```
awk '{i=1;while(i<=NF){gsub(/[[:digit:]]/,"o",$i);print $i; i++;}}' testfile
```
- split(t, a[, r[, seps]])
```
echo "12a31b22c45" | awk '{split($0, arr, /[[:alpha:]]/);for(ele in arr){print ele, arr[ele]}}'
```
- Self Defined Function
```
echo "12a31b22c45" | awk 'function say(s){print s" hello!"}{say($0)}'
```
- Switch Case
```
awk -f prog testfile
```
```
vi prog
{
switch (NR) {
case 3:
break
case "11":
print NR, "string"
break
case /2[[:digit:]]+/:
print NR, "regex"
break
default:
print NR
break
case -1:
print NR, "negative"
break
}
}
```
## sed
- [sed 命令常見用法](https://www.cnblogs.com/xiaoxie2014/p/12911241.html)