Linux Shell Basics (vi) AWK commands (in edit)

Source: Internet
Author: User

First, the awk command
   awk是一种**编程语言**,用于在linux/unix下对文本和数据进行处理(gawk - pattern scanning and processing language)。centos7中的awk是gawk的链接。它支持用户自定义函数和动态正则表达式等先进功能,是linux/unix下的一个强大编程工具。前面的grep、sed只是单纯的工具,而awk已经是一门语言了,可见awk的功能将比较强大         用法:         awk [options] ‘script‘ var=value file(s)   awk [options] -f scriptfile var=value file(s)         选项:   -F fs   fs指定输入分隔符,fs可以是字符串或正则表达式,如-F:   -v var=value   赋值一个用户定义变量,将外部变量传递给awk   -f scripfile  从脚本文件中读取awk命令
Second, the operation of the text vertical column

The awk command makes it easy to divide the text into specific columns by specifying a delimiter, and then manipulate it against the split vertical column

1. Display the first column of the Test.txt document in the top 5 rows.

Awk-f ': ' {print $} ' test.txt | Head-n 5

Command interpretation:-F ': ' means this file is: As a delimiter, we do the experiment is the passwd file, which itself is: divided into 7 paragraphs, ' {} ' is the description of the operation, the command print $ is displayed in the 1th column.

2. Display the contents of all vertical columns of the Test.txt file

Awk-f ': ' {print $} ' test.txt
awk ' {print $} ' test.txt

Note the difference from the previous command, which represents the first column, which is all columns.

The effect is actually the same as cat.

3. Awk can also
The awk command defaults to a space or blank segment as a delimiter when no-f split symbol or split string is specified

Let's start by creating a test document with a space as a delimiter

After we enter the following command

awk ' {print '} '
awk ' {print $1,$3} '
awk ' {print $1,$2} '

You can see that awk automatically takes a space as a fragment mark, no matter how many spaces.

4. Specify # to display a specific column for display separators (note that the original file is not affected, only the display is affected)

Awk-f ': ' {print $ "#" $ "#" $7} ' test.txt

Iii. examples of awk matching functions

Linux Shell Basics (vi) AWK commands (in edit)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.