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

本文详细介绍了awk文本处理工具的常用选项,包括参数传递、指定脚本文件、分隔符设置及版本查看等,通过实例展示了如何使用这些选项进行高效的数据处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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/.

 

转载于:https://www.cnblogs.com/reblue520/p/11016891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值