> For the complete documentation index, see [llms.txt](https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-er-zhang-linux-gong-fang-kuai-su-ru-men-shang/wen-ben-san-jian-ke.md).

# 文本三剑客

为什么云安全课程要学习Bash？

当你在Docker环境，容器中的系统被阉割过，只有系统内核，连个ifconfig都没有的环境，并且不能出网，也不能装工具，这时候如何去探测内网？ 有些条件环境苛刻无法上传文件，该如何探测？这块知识点对后面有什么帮助？

&#x20;

awk、grep、sed是linux操作文本的三大利器，合称文本三剑客。

#### 文本处理三剑客之grep&#x20;

* &#x20;grep 文本搜索工具

-v #显示不被匹配的行

-i #忽略字符大小写

-n #显示匹配的行号

-c #统计匹配的行数

-o #仅显示匹配的字符串

-q #静默模式，不输出任何信息

{% code overflow="wrap" %}

```bash
#查看当前主机连接数最多的前三位
ss -nt|grep "^ESTAB"|tr -s ' ' :|cut -d: -f6|sort|uniq -c|sort -nr|head -n3

#排队掉空行和#开头的行
grep -v '^#' /etc/profile|
 
#匹配IP地址
ifconfig ens33|grep netmask|grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'|head -n1
```

{% endcode %}

&#x20;

#### 文本处理三剑客之Sed

sed编辑器的工作原理是读取一行，处理一行，输出一行然后循环这样。

-n #输出模式空间内容到屏幕，即不自动打印

-e #多点编辑

-f  FILE #从指定文件中读取编辑脚本

-r, -E #使用扩展正则表达式

-i.bak #备份文件并原处编辑

-s #将多个文件视为独立文件，而不是单个连续的长文件流

```bash
a ：新增
c ：取代
d ：删除
i ：插入
p ：打印
s ：取代
 
#说明:
-ir 不支持
-i -r 支持
-ri 支持
-ni 会清空文件
 
#地址格式
##单地址
#：指定行
$：最后一行
 
##地址范围
#,# 3,6行
应用场景
#打印1行
sed -n '1p' /etc/issue.bak
 
#打印最后一行
sed -n  '$p' /etc/passwd
 
#倒数第二行
sed -n "$(echo $[`cat /etc/passwd|wc -l`-1])p" /etc/passwd
 
#删除所有以#开头的行
sed -i '/^#/d' fstab
sed -ri.bak  '/^#/s/^#//'  /etc/fstab
 
#获取IP
ifconfig ens33 |sed -nr  "2s/[^0-9]+([0-9.]+).*/\1/p"
 
#取基名和目录名
echo "/etc/sysconfig/network-scripts/" |sed -r 's#(^/.*/)([^/]+/?)#\2#' 取基名
echo "/etc/sysconfig/network-scripts/" |sed -r 's#(^/.*/)([^/]+/?)#\1#' 取目录
 
#取文件的前缀和后缀
echo a.b.c.gz  |sed -En 's/(.*)\.([^.]+)$/\1/p'
echo a.b.c.gz  |sed -En 's/(.*)\.([^.]+)$/\2/p'
 
#过滤掉空行和#开头的行
sed -r '/^(#|$)/d' /etc/httpd/conf/httpd.conf
sed -r '/^#|^$/d' /etc/httpd/conf/httpd.conf
 
#伪造Apache日志中的指定IP
sed –i 's/192.168.1.3/192.168.1.4/g' /var/log/apache/access.log
sed –i 's/192.168.1.3/192.168.1.4/g' /var/log/apache/error_log
```

#### 文本处理三剑客之AWK

-F 指定分隔符写法可以是-F:  -F ':'

-v var=value   赋值一个用户定义变量，将外部变量传递给awk

-f scripfile  从脚本文件中读取awk命令

&#x20;

awk内置变量

NF 段数

NR 行数

```bash
#连接最多的前3个IP
awk -F" +|:" '{print $(NF-2)}' ss.log |sort |uniq -c|sort -nr|head -n3
 
#关于POST请求
grep 'POST' /var/log/httpd/access_log | awk '{print $1}' | sort | uniq -c | sort -nr
 
#Content-Length过大的请求，例如过滤Content-Length大于5M的日志
awk '{if($10>5000000){print $0}}' /var/log/httpd/access_log
 
#/etc/passwd 中用户uid 大于500 的行给打印出来
awk -F ':' '$3 >= 500' 1.txt
 
#列出所有进程交换空间使用情况
for file in /proc/*/status ; do awk '/VmSwap|Name/{printf $2 " " $3}END{ print ""}' $file; done
 
#查看root用户
awk -F: '$3==0{print $1}' /etc/passwd
 
#查看SSH爆破记录
cat /var/log/secure | awk '/Failed/{print $(NF-3)}' | sort | uniq -c | awk '{print $2" = "$1;}'
 
#两个文件去重
awk '{print $0}' file1 file2 |sort|uniq
 
#查看当前系统IP连接数
netstat -n | awk '/^tcp/ {print $5}'| awk -F: '{print $1}' | sort | uniq -c | sort -rn
```

&#x20;

&#x20;

&#x20;

&#x20;


---

# 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://lzcloudsecurity.gitbook.io/yun-an-quan-gong-fang-ru-men/di-er-zhang-linux-gong-fang-kuai-su-ru-men-shang/wen-ben-san-jian-ke.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.
