Home > System Tutorial > LINUX > body text

Learn to use AWK built-in variables in Linux

PHPz
Release: 2024-01-06 16:22:06
forward
487 people have browsed it
Introduction We will gradually unveil the mystery of awk's functionality. In this section, we will introduce the concept of awk's built-in variables. There are two types of variables you can use in awk: user-defined variables and built-in variables.

linux下 awk内置变量使用介绍

我们将逐渐揭开 awk 功能的神秘面纱,在本节中,我们将介绍 awk 内置built-in变量的概念。你可以在 awk 中使用两种类型的变量,它们是:用户自定义user-defined变量和内置变量。awk 内置变量已经有预先定义的值了,但我们也可以谨慎地修改这些值.

awk 内置变量包括:
  • FILENAME : 当前输入文件名称
  • NR : 当前输入行编号(是指输入行 1,2,3……等)
  • NF : 当前输入行的字段编号
  • OFS : 输出字段分隔符
  • FS : 输入字段分隔符
  • ORS : 输出记录分隔符
  • RS : 输入记录分隔符
FILENAME :

让我们继续演示一些使用上述 awk 内置变量的方法,想要读取当前输入文件的名称,你可以使用 FILENAME 内置变量,如下:$ awk ' { print FILENAME } ' ~/domains.txt

linux下 awk内置变量使用介绍

你会看到,每一行都会对应输出一次文件名,那是你使用 FILENAME 内置变量时 awk 默认的行为。我们可以使用 NR 来统计一个输入文件的行数(记录),谨记:它也会计算空行,正如我们将要在下面的例子中看到的那样。 输出文件内容 当我们使用 cat 命令查看文件 domains.txt 时,会发现它有 14 行文本和 2 个空行:$ cat ~/domains.txt

linux下 awk内置变量使用介绍

awk 统计行数:
$ awk ' END { print "Number of records in file is: ", NR } ' ~/domains.txt
Copy after login

linux下 awk内置变量使用介绍

awk 统计文件中的字段数:
$ awk '{ "Record:",NR,"has",NF,"fields" ; }' ~/names.txt
Copy after login

 linux下 awk内置变量使用介绍

FS 内置变量:

你也可以使用 FS 内置变量指定一个输入文件分隔符,它会定义 awk 如何将输入行划分成字段。FS 默认值为“空格”和“制表符”,但我们也能将 FS 值修改为任何字符来让 awk 根据情况切分输入行。有两种方法可以达到目的:第一种方法是使用 FS 内置变量;第二种方法是使用 awk 的 -F 选项。来看 Linux 系统上的 /etc/passwd 文件,该文件中的各字段是使用 冒号(:) 分隔的,因此,当我们想要过滤出某些字段时,可以将冒号(:) 指定为新的输入字段分隔符, awk 过滤密码文件中的各字段 . 我们可以使用 -F 选项,如下:$ awk -F':' '{ print $1, $4 ;}' /etc/passwd

linux下 awk内置变量使用介绍

此外,我们也可以利用 FS 内置变量,如下:$ awk ' BEGIN { FS=“:” ; } { print $1, $4 ; } ' /etc/passwd

linux下 awk内置变量使用介绍

Use OFS built-in variables:

Use the OFS built-in variable to specify a field delimiter for output, which will define how to use the specified character to separate the output fields. Use the delimiter for awk output: $ awk -F':' ' BEGIN { OFS="==>" ;} { print $1, $4 ;}' /etc/passwd

linux下 awk内置变量使用介绍

In this section, we have learned the idea of ​​using awk built-in variables with predefined values. But we can also modify these values, although this is not recommended unless you know what you are doing and fully understand (these variable values).

After this, we will continue to learn how to use shell variables in awk command operations, so please continue to follow us.

The above is the detailed content of Learn to use AWK built-in variables in Linux. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!