shell编程系列20--文本处理三剑客之awk常用选项

复制代码
shell编程系列20--文本处理三剑客之awk常用选项


awk选项总结

选项        解释
-v        参数传递
-f        指定脚本文件
-F        指定分隔符
-V        查看awk的版本号

[root@localhost shell]# awk -v num2="$num1" -v var1="$var" 'BEGIN{print num2,var1}'
20 hello world


# -f 选项 文件中读取表达式
[root@localhost shell]# cat 1.awk 
BEGIN{
    str="I hava a tream"
    location=index(str,"ea")
    print location
}
[root@localhost shell]# awk -f 1.awk 
12

[root@localhost shell]# awk -f 2.awk 
Transaction $ Start,Event ID:9002
[root@localhost shell]# cat 2.awk 
BEGIN{
    str="Transaction 243 Start,Event ID:9002"
    count=sub(/[0-9]+/,"$",str)
    print str
}

# -F 指定分隔符
[root@localhost shell]# awk -F ":" '{print $7}' passwd 
/bin/bash
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/sync
/sbin/shutdown
/sbin/halt
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/sbin/nologin
/bin/bash
/sbin/nologin
/bin/bash
/sbin/nologin

# 显示版本号
[root@localhost shell]# awk -V
GNU Awk 4.0.2
Copyright (C) 1989, 1991-2012 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.
复制代码

 

posted @   reblue520  阅读(300)  评论(0编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示