欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

gawk linux,linux中gawk命令

发布时间:2023/12/20 49 豆豆
生活随笔 收集整理的这篇文章主要介绍了 gawk linux,linux中gawk命令 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

终止gawk程序,使用Ctrl+D组合键产生一个EOF终止符。

[root@localhost ~]# gawk '{print "Hello World"}'

this is a test

Hello World

you need input ctrl+D to end the shell

Hello World

数据字段变量,默认分隔符是任意的空白字符(例如空格或制表符)

$0代表整个文本。

$1代表文本中的第1个数据字段

$2代表文本中的第2个数据字段

$n代表文本中的第n个数据字段

字段提取

[root@localhost shell]# cat helloworld.txt

one line of this file

two line of this file

three line of this file

four line of this file

five line of this file

[root@localhost shell]# gawk '{print $1}' helloworld.txt

one

two

three

four

five

-F分隔符

[root@localhost shell]# gawk -F: '{print $1}' /etc/passwd

root

bin

daemon

adm

lp

sync

shutdown

halt

mail

operator

games

BEGIN打印之前执行脚本

[root@localhost shell]# cat helloworld.txt

Line 1

Line 2

Line 3

[root@localhost shell]# gawk 'BEGIN {print "The helloworld.txt File Contents:"};{print $0}' helloworld.txt

The helloworld.txt File Contents:

Line 1

Line 2

Line 3

在处理数据后运行脚本

[root@localhost shell]# gawk 'BEGIN {print "The helloworld.txt File Contents:"};{print $0}; END {print "The end."}' helloworld.txt

The helloworld.txt File Contents:

Line 1

Line 2

Line 3

The end.

总结

以上是生活随笔为你收集整理的gawk linux,linux中gawk命令的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。