LinuxShell三剑客深度解析

admin 2026-01-15 14:50:20 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 文档深入解析了LinuxShell三剑客grep、awk和sed,涵盖基础语法、正则表达式及实战案例。通过大量可执行脚本演示了日志分析、进程监控、数据清洗等场景。虽然sed部分略有截断,但整体结构清晰,代码丰富,对提升运维与安全人员的文本处理及自动化能力极具参考价值。 综合评分: 80 文章分类: 安全工具


三、awk – 文本处理神器

3.1 基础awk语法

#!/bin/bash
# awk-basic-syntax.sh

echo"=== awk 基础语法 ==="

# 创建测试数据
cat > employee_data.txt <<&nbsp;'EOF'
张三 25 男 开发部 8000
李四 30 女 测试部 7000
王五 28 男 运维部 9000
赵六 35 女 开发部 8500
钱七 22 男 测试部 6500
EOF

echo"员工数据:"
cat employee_data.txt
echo"----------------------------------------"

# 1. 基本打印
echo"1. 打印所有行:"
awk&nbsp;'{print}'&nbsp;employee_data.txt

# 2. 打印特定字段
echo&nbsp;-e&nbsp;"\n2. 打印姓名和工资:"
awk&nbsp;'{print $1, $5}'&nbsp;employee_data.txt

# 3. 使用内置变量
echo&nbsp;-e&nbsp;"\n3. 显示行号和内容:"
awk&nbsp;'{print NR, $0}'&nbsp;employee_data.txt

# 4. 条件过滤
echo&nbsp;-e&nbsp;"\n4. 工资大于8000的员工:"
awk&nbsp;'$5 > 8000 {print $1, $5}'&nbsp;employee_data.txt

# 5. 模式匹配
echo&nbsp;-e&nbsp;"\n5. 开发部员工:"
awk&nbsp;'/开发部/ {print $0}'&nbsp;employee_data.txt

# 6. BEGIN和END块
echo&nbsp;-e&nbsp;"\n6. 统计信息:"
awk&nbsp;'BEGIN {print "开始处理数据..."}
&nbsp; &nbsp; &nbsp;{total += $5; count++}
&nbsp; &nbsp; &nbsp;END {print "处理完成"; print "平均工资:", total/count}'&nbsp;employee_data.txt

# 清理
rm employee_data.txt

3.2 高级awk功能

#!/bin/bash
# awk-advanced-features.sh

echo"=== awk 高级功能 ==="

# 创建复杂测试数据
cat > sales_data.txt <<&nbsp;'EOF'
2024-01-01 产品A 北京 1000
2024-01-01 产品B 上海 1500
2024-01-02 产品A 广州 800
2024-01-02 产品C 深圳 2000
2024-01-03 产品B 北京 1200
2024-01-03 产品A 上海 900
2024-01-04 产品C 广州 1800
EOF

echo"销售数据:"
cat sales_data.txt
echo"----------------------------------------"

# 1. 数组使用
echo"1. 按产品统计销售额:"
awk&nbsp;'{sales[$2] += $4} END {for(product in sales) print product, sales[product]}'&nbsp;sales_data.txt

# 2. 内置函数
echo&nbsp;-e&nbsp;"\n2. 使用内置函数:"
awk&nbsp;'{print "日期:", $1, "长度:", length($1)}'&nbsp;sales_data.txt | head -3

# 3. 字段分隔符控制
echo&nbsp;-e&nbsp;"\n3. 自定义字段分隔符:"
echo"2024-01-01,产品A,北京,1000"&nbsp;| awk -F,&nbsp;'{print $2, $4}'

# 4. 多条件处理
echo&nbsp;-e&nbsp;"\n4. 北京地区的产品A销售:"
awk&nbsp;'$3 == "北京" && $2 == "产品A" {print "北京产品A销售:", $4}'&nbsp;sales_data.txt

# 5. 格式化输出
echo&nbsp;-e&nbsp;"\n5. 格式化报表:"
awk&nbsp;'BEGIN {printf "%-12s %-8s %-6s %-8s\n", "日期", "产品", "地区", "销售额"}
&nbsp; &nbsp; &nbsp;{printf "%-12s %-8s %-6s %-8d\n", $1, $2, $3, $4}'&nbsp;sales_data.txt

# 6. 字符串操作
echo&nbsp;-e&nbsp;"\n6. 字符串处理:"
awk&nbsp;'{print "产品:", toupper($2), "地区:", substr($3,1,2)}'&nbsp;sales_data.txt

# 7. 数学运算
echo&nbsp;-e&nbsp;"\n7. 数学计算:"
awk&nbsp;'{total += $4; max = $4 > max ? $4 : max}
&nbsp; &nbsp; &nbsp;END {print "总销售额:", total, "最高销售额:", max}'&nbsp;sales_data.txt

# 清理
rm sales_data.txt

3.3 awk实战案例

#!/bin/bash
# awk-practical-examples.sh

echo"=== awk 实战应用案例 ==="

# 案例1: 系统监控
echo&nbsp;"1. 📊 系统进程内存使用统计:"
ps aux | awk&nbsp;'
NR>1 {
&nbsp; &nbsp; mem[$11] +=&nbsp;$4
&nbsp; &nbsp; count[$11]++
}
END {
&nbsp; &nbsp; print "进程内存使用统计:"
&nbsp; &nbsp; for (proc in mem) {
&nbsp; &nbsp; &nbsp; &nbsp; avg = mem[proc] / count[proc]
&nbsp; &nbsp; &nbsp; &nbsp; printf "%-20s %6.1f%% (平均)\n", proc, avg
&nbsp; &nbsp; }
}'&nbsp;| head -10

# 案例2: 日志分析
echo -e&nbsp;"\n2. 🔍 Nginx访问日志分析:"
cat > nginx_access.log <<&nbsp;'EOF'
192.168.1.1&nbsp;- - [10/Jan/2024:10:00:00]&nbsp;"GET /index.html HTTP/1.1"2001234
192.168.1.2&nbsp;- - [10/Jan/2024:10:01:00]&nbsp;"GET /about.html HTTP/1.1"2005678
192.168.1.1&nbsp;- - [10/Jan/2024:10:02:00]&nbsp;"GET /index.html HTTP/1.1"2001234
192.168.1.3&nbsp;- - [10/Jan/2024:10:03:00]&nbsp;"GET /contact.html HTTP/1.1"404234
192.168.1.2&nbsp;- - [10/Jan/2024:10:04:00]&nbsp;"GET /index.html HTTP/1.1"2001234
EOF

echo&nbsp;"IP访问统计:"
awk&nbsp;'{ip_count[$1]++} END {for(ip in ip_count) print ip, ip_count[ip]}'&nbsp;nginx_access.log

# 案例3: 数据清洗和转换
echo -e&nbsp;"\n3. 🧹 CSV转JSON:"
cat > data.csv <<&nbsp;'EOF'
name,age,department,salary
张三,25,开发部,8000
李四,30,测试部,7000
王五,28,运维部,9000
EOF

echo&nbsp;"CSV转JSON:"
awk -F,&nbsp;'
NR==1 {
&nbsp; &nbsp; for(i=1;i<=NF;i++) headers[i]=$i
}
NR>1 {
&nbsp; &nbsp; printf "{\n"
&nbsp; &nbsp; for(i=1;i<=NF;i++) {
&nbsp; &nbsp; &nbsp; &nbsp; printf " &nbsp;\"%s\": \"%s\"", headers[i],&nbsp;$i
&nbsp; &nbsp; &nbsp; &nbsp; if(i<NF) printf ",\n"
&nbsp; &nbsp; &nbsp; &nbsp; else printf "\n"
&nbsp; &nbsp; }
&nbsp; &nbsp; printf "}"
&nbsp; &nbsp; if(NR<FNR) printf ",\n"
}'&nbsp;data.csv

# 案例4: 系统性能报告
echo -e&nbsp;"\n\n4. 📈 系统性能报告:"
echo&nbsp;"内存使用情况:"
free -m | awk&nbsp;'
NR==2 {total=$2; used=$3; free=$4}
NR==3 {cache=$3}
END {
&nbsp; &nbsp; printf "总内存: %dMB\n", total
&nbsp; &nbsp; printf "已使用: %dMB (%.1f%%)\n", used, (used/total)*100
&nbsp; &nbsp; printf "空闲: %dMB\n", free
&nbsp; &nbsp; printf "缓存: %dMB\n", cache
}'

# 案例5: 文本处理
echo -e&nbsp;"\n5. 📝 文本格式处理:"
cat > text_data.txt <<&nbsp;'EOF'
John Doe:25:New York:Engineer
Jane Smith:30:London:Designer
Bob Johnson:35:Tokyo:Manager
EOF

echo&nbsp;"格式化输出:"
awk -F:&nbsp;'{
&nbsp; &nbsp; printf "%-12s: %s\n", "姓名",&nbsp;$1
&nbsp; &nbsp; printf "%-12s: %s\n", "年龄",&nbsp;$2
&nbsp; &nbsp; printf "%-12s: %s\n", "城市",&nbsp;$3
&nbsp; &nbsp; printf "%-12s: %s\n", "职业",&nbsp;$4
&nbsp; &nbsp; print "----------------"
}'&nbsp;text_data.txt

# 清理
rm -f nginx_access.log data.csv text_data.txt

四、sed – 流编辑器

4.1 基础sed操作

#!/bin/bash
# sed-basic-operations.sh

echo"=== sed 基础操作 ==="

# 创建测试文件
cat > sed_test.txt <<&nbsp;'EOF'
Hello World
Linux is great
Sed is powerful
Awk is amazing
Find is useful
Welcome to Linux
EOF

echo"原始文件内容:"
cat sed_test.txt
echo"----------------------------------------"

# 1. 替换操作
echo"1. 替换 'Linux' 为 'Unix':"
sed&nbsp;'s/Linux/Unix/g'&nbsp;sed_test.txt

# 2. 删除操作
echo&nbsp;-e&nbsp;"\n2. 删除包含 'is' 的行:"
sed&nbsp;'/is/d'&nbsp;sed_test.txt

# 3. 插入和追加
echo&nbsp;-e&nbsp;"\n3. 在第一行前插入标题:"
sed&nbsp;'1i\=== 标题 ==='&nbsp;sed_test.txt

echo&nbsp;-e&nbsp;"\n4. 在最后一行后追加结束标记:"
sed&nbsp;'$a\=== 结束 ==='&nbsp;sed_test.txt

# 4. 行范围操作
echo&nbsp;-e&nbsp;"\n5. 替换第2-4行的 'is' 为 'IS':"
sed&nbsp;'2,4s/is/IS/g'&nbsp;sed_test.txt

# 5. 保存修改到文件
echo&nbsp;-e&nbsp;"\n6. 保存修改到新文件:"
sed&nbsp;'s/Linux/UNIX/g'&nbsp;sed_test.txt > sed_modified.txt
echo"修改后的文件:"
cat sed_modified.txt

# 6. 使用正则表达式
echo&nbsp;-e&nbsp;"\n7. 使用正则表达式替换:"
sed&nbsp;'s/^[A-Z]/【&】/g'&nbsp;sed_test.txt

# 清理
rm sed_test.txt sed_modified.txt

4.2 高级sed技巧

#!/bin/bash
# sed-advanced-techniques.sh

echo"=== sed 高级技巧 ==="

# 创建复杂测试文件
cat > config_file.txt <<&nbsp;'EOF'
# Server configuration
ServerName example.com
Port 8080
Timeout 300
MaxClients 100

# Database settings
DB_Host localhost
DB_Port 3306
DB_User root
DB_Password secret

# Log settings
LogLevel info
LogFile /var/log/app.log
EOF

echo"配置文件内容:"
cat config_file.txt
echo"----------------------------------------"

# 1. 模式空间和保持空间
echo"1. 反转文件行顺序:"
sed&nbsp;'1!G;h;$!d'&nbsp;config_file.txt

# 2. 分支和标签
echo&nbsp;-e&nbsp;"\n2. 条件处理:"
sed&nbsp;'/DB_/{
&nbsp; &nbsp; s/3306/5432/
&nbsp; &nbsp; s/secret/NEW_PASSWORD/
}'&nbsp;config_file.txt

# 3. 多命令执行
echo&nbsp;-

免责声明:

本文所载程序、技术方法仅面向合法合规的安全研究与教学场景,旨在提升网络安全防护能力,具有明确的技术研究属性。

任何单位或个人未经授权,将本文内容用于攻击、破坏等非法用途的,由此引发的全部法律责任、民事赔偿及连带责任,均由行为人独立承担,本站不承担任何连带责任。

本站内容均为技术交流与知识分享目的发布,若存在版权侵权或其他异议,请通过邮件联系处理,具体联系方式可点击页面上方的联系我

本文转载自:运维星火燎原 刘军军《Linux Shell三剑客深度解析》

评论:0   参与:  0