gawk程序基础

2023-12-27 05:08
文章标签 基础 程序 gawk

本文主要是介绍gawk程序基础,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

虽然sed已经很牛逼了,但是再牛逼也有自身的限制。gawk就是用来搞定sed不能搞定的问题。

gawk可以做以下几件事情:

  • 定义变量来保存数据;
  • 使用算术和字符串操作符来处理数据;
  • 使用结构化编程概念,为数据处理增加逻辑;
  • 提取数据文件中的数据元素进行格式化。

gawk命令格式:


gawk options program file

还是直接看例子吧。

列操作


首先假设我们有这样一个文本数据data:

No.1 Google Gmail
No.2 Microsoft Windows
No.3 SAP ERP
No.4 Intel Core
No.5 Cisco Rout

输入命令并得出输出结果:

$ gawk '{print $1}' data
No.1
No.2
No.3
No.4
No.5

看到了吧,啥情况,这是输出文本所有以空格为分隔符的第一列。输出第二列当然就是这样写了。

$ gawk '{print $2}' data
Google
Microsoft
SAP
Intel
Cisco

如果命令是‘$0’,会出现啥?

$ gawk '{print $0}' data
No.1 Google Gmail
No.2 Microsoft Windows
No.3 SAP ERP
No.4 Intel Core
No.5 Cisco Router

很显然,如果输入‘$0’,那整个文本就输出了。

好,有了这个功能,那我们分析一些系统文件是不是就省事多了,比如passwd文件里面的内容打开以后,如果直接看,那貌似比较困难,现在有了gwak,来先输出一下第一列吧。

$ gawk -F: '{print $1}' /etc/passwd 
root
daemon
bin
sys
sync
games
man
lp
mail
news
uucp
proxy
www-data
backup
list
irc
gnats
nobody
systemd-timesync
systemd-network
systemd-resolve
systemd-bus-proxy
syslog
......

看到这个结果就爽,不过要注意:这里用了一个参数F,这个参数的意思就是指定行中分隔数据字段的字段分隔符。F后面紧跟了个‘:’,意思就是以‘:’为分隔符。

gawk当然也可以用于管道数据处理:

$ echo "I have a pen" | gawk '{$4="apple"; print $0}'
I have a apple

这个命令的意思就是将第四个词改为“apple”,然后全部输出。注意:$4=”apple”这个地方必须是双引号,如果是单引号就只会输出“I have a”。

与sed一样,gawk也可以一行一行地输入脚本命令:

$ gawk '{
> $4="apple"
> print $0}'appleapple

因为我们之前啥也每输入,也就是说没有原始字符串,所以每次回车都替换第四个单词为apple,然后通过ctrl+D就可以结束运行了。

从文件中读取命令


新建脚本script

{print $1 "'s home direcotry is " $6}

运行并得出结果:

$ gawk -F: -f script /etc/passwd
root's home direcotry is /root
daemon's home direcotry is /usr/sbin
bin's home direcotry is /bin
sys's home direcotry is /dev
sync's home direcotry is /bin
games's home direcotry is /usr/games
man's home direcotry is /var/cache/man
lp's home direcotry is /var/spool/lpd
mail's home direcotry is /var/mail
news's home direcotry is /var/spool/news
uucp's home direcotry is /var/spool/uucp
proxy's home direcotry is /bin
www-data's home direcotry is /var/www
backup's home direcotry is /var/backups
list's home direcotry is /var/list
irc's home direcotry is /var/run/ircd
gnats's home direcotry is /var/lib/gnats
nobody's home direcotry is /nonexistent
systemd-timesync's home direcotry is /run/systemd
systemd-network's home direcotry is /run/systemd/netif
systemd-resolve's home direcotry is /run/systemd/resolve
systemd-bus-proxy's home direcotry is /run/systemd
syslog's home direcotry is /home/syslog
_apt's home direcotry is /nonexistent
messagebus's home direcotry is /var/run/dbus
uuidd's home direcotry is /run/uuidd
lightdm's home direcotry is /var/lib/lightdm
whoopsie's home direcotry is /nonexistent
avahi-autoipd's home direcotry is /var/lib/avahi-autoipd
avahi's home direcotry is /var/run/avahi-daemon
dnsmasq's home direcotry is /var/lib/misc
colord's home direcotry is /var/lib/colord
speech-dispatcher's home direcotry is /var/run/speech-dispatcher
hplip's home direcotry is /var/run/hplip
kernoops's home direcotry is /
pulse's home direcotry is /var/run/pulse
rtkit's home direcotry is /proc
saned's home direcotry is /var/lib/saned
usbmux's home direcotry is /var/lib/usbmux
comac's home direcotry is /home/comac
sshd's home direcotry is /var/run/sshd

当然这个脚本也可以这么写:

{ 
text="'s home directory is " 
print $1 text $6
}

注意:每个命令分别放到新的一行,有没有分号无所谓。

BEGIN,END关键字的使用


这两个关键字理解也简单,BEGIN就是在执行脚本前运行这个,END就是在执行脚本后运行这个。

还是看例子:

$ gawk 'BEGIN { print "This is the key word BEGIN" } { print $0}' data
This is the key word BEGIN
No.1 Google Gmail
No.2 Microsoft Windows
No.3 SAP ERP
No.4 Intel Core
No.5 Cisco Router

在打印data中的文本前先输出了BEGIN中的内容。下面再看看END是啥效果:

$ gawk 'BEGIN { print "This is the key word BEGIN" } { print $0} END { print "This is the key word END"}' data
This is the key word BEGIN
No.1 Google Gmail
No.2 Microsoft Windows
No.3 SAP ERP
No.4 Intel Core
No.5 Cisco Router
This is the key word END

很简单是吧,最后呢,再体验一下gawk的威力,你就回觉得这家伙真心牛逼。

新建脚本文件script

BEGIN {
print "The latest list of users and shells"
print "Userid                  shell "
print "---------------------------------------------"
FS=":"
}{
print $1 "\t\t\t" $7
}END {
print "---------------------------------------------"
print "This is the end of the list"
}

输出结果:

$ gawk -f script /etc/passwd
The latest list of users and shells
Userid                  shell 
---------------------------------------------
root            /bin/bash
daemon          /usr/sbin/nologin
bin         /usr/sbin/nologin
sys         /usr/sbin/nologin
sync            /bin/sync
games           /usr/sbin/nologin
man         /usr/sbin/nologin
lp          /usr/sbin/nologin
mail            /usr/sbin/nologin
news            /usr/sbin/nologin
uucp            /usr/sbin/nologin
proxy           /usr/sbin/nologin
www-data            /usr/sbin/nologin
backup          /usr/sbin/nologin
list            /usr/sbin/nologin
irc         /usr/sbin/nologin
gnats           /usr/sbin/nologin
nobody          /usr/sbin/nologin
systemd-timesync            /bin/false
systemd-network         /bin/false
systemd-resolve         /bin/false
systemd-bus-proxy           /bin/false
syslog          /bin/false
_apt            /bin/false
messagebus          /bin/false
uuidd           /bin/false
lightdm         /bin/false
whoopsie            /bin/false
avahi-autoipd           /bin/false
avahi           /bin/false
dnsmasq         /bin/false
colord          /bin/false
speech-dispatcher           /bin/false
hplip           /bin/false
kernoops            /bin/false
pulse           /bin/false
rtkit           /bin/false
saned           /bin/false
usbmux          /bin/false
comac           /bin/bash
sshd            /usr/sbin/nologin
---------------------------------------------
This is the end of the list

生成这么清晰明了的报表,就写到这里吧,稍稍休息一下。

参考文献

[1] Linux Command Line and Shell Scripting Bible 2nd Edition. Richard Blum, Christine Bresnahan. WILEY Press.

这篇关于gawk程序基础的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/541962

相关文章

从基础到进阶详解Pandas时间数据处理指南

《从基础到进阶详解Pandas时间数据处理指南》Pandas构建了完整的时间数据处理生态,核心由四个基础类构成,Timestamp,DatetimeIndex,Period和Timedelta,下面我... 目录1. 时间数据类型与基础操作1.1 核心时间对象体系1.2 时间数据生成技巧2. 时间索引与数据

安装centos8设置基础软件仓库时出错的解决方案

《安装centos8设置基础软件仓库时出错的解决方案》:本文主要介绍安装centos8设置基础软件仓库时出错的解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录安装Centos8设置基础软件仓库时出错版本 8版本 8.2.200android4版本 javas

python编写朋克风格的天气查询程序

《python编写朋克风格的天气查询程序》这篇文章主要为大家详细介绍了一个基于Python的桌面应用程序,使用了tkinter库来创建图形用户界面并通过requests库调用Open-MeteoAPI... 目录工具介绍工具使用说明python脚本内容如何运行脚本工具介绍这个天气查询工具是一个基于 Pyt

Ubuntu设置程序开机自启动的操作步骤

《Ubuntu设置程序开机自启动的操作步骤》在部署程序到边缘端时,我们总希望可以通电即启动我们写好的程序,本篇博客用以记录如何在ubuntu开机执行某条命令或者某个可执行程序,需要的朋友可以参考下... 目录1、概述2、图形界面设置3、设置为Systemd服务1、概述测试环境:Ubuntu22.04 带图

Python程序打包exe,单文件和多文件方式

《Python程序打包exe,单文件和多文件方式》:本文主要介绍Python程序打包exe,单文件和多文件方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python 脚本打成exe文件安装Pyinstaller准备一个ico图标打包方式一(适用于文件较少的程

Linux基础命令@grep、wc、管道符的使用详解

《Linux基础命令@grep、wc、管道符的使用详解》:本文主要介绍Linux基础命令@grep、wc、管道符的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录grep概念语法作用演示一演示二演示三,带选项 -nwc概念语法作用wc,不带选项-c,统计字节数-

Python程序的文件头部声明小结

《Python程序的文件头部声明小结》在Python文件的顶部声明编码通常是必须的,尤其是在处理非ASCII字符时,下面就来介绍一下两种头部文件声明,具有一定的参考价值,感兴趣的可以了解一下... 目录一、# coding=utf-8二、#!/usr/bin/env python三、运行Python程序四、

python操作redis基础

《python操作redis基础》Redis(RemoteDictionaryServer)是一个开源的、基于内存的键值对(Key-Value)存储系统,它通常用作数据库、缓存和消息代理,这篇文章... 目录1. Redis 简介2. 前提条件3. 安装 python Redis 客户端库4. 连接到 Re

无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案

《无法启动此程序因为计算机丢失api-ms-win-core-path-l1-1-0.dll修复方案》:本文主要介绍了无法启动此程序,详细内容请阅读本文,希望能对你有所帮助... 在计算机使用过程中,我们经常会遇到一些错误提示,其中之一就是"api-ms-win-core-path-l1-1-0.dll丢失

SpringBoot基础框架详解

《SpringBoot基础框架详解》SpringBoot开发目的是为了简化Spring应用的创建、运行、调试和部署等,使用SpringBoot可以不用或者只需要很少的Spring配置就可以让企业项目快... 目录SpringBoot基础 – 框架介绍1.SpringBoot介绍1.1 概述1.2 核心功能2