search
HomeBackend DevelopmentPHP TutorialSummary of AWK syntax under shell programming_PHP tutorial
Summary of AWK syntax under shell programming_PHP tutorialJul 13, 2016 pm 05:26 PM
awkshellunixDownIncludeUtilitiesbringofprogrammingmy owngrammarlanguage

The AWK utility comes with its own self-contained language and is one of the most powerful data processing engines available in Unix/Linux or any environment. The greatest power of this programming and data manipulation language (which takes its name from the first letters of the surnames of its founders, Alfred Aho, Peter Weinberger, and Brian Kernighan) depends on the knowledge one possesses. It allows you to create short programs that read input files, sort the data, process the data, perform calculations on the input, and generate reports, among countless other functions.
What is AWK? At its simplest, AWK is a programming language tool for working with text. The language of the AWK utility is similar in many ways to the shell programming language, although AWK has a syntax that is entirely its own. When AWK was originally created, it was intended for text processing, and the basis of the language was to execute a sequence of instructions whenever there was a pattern match in the input data. This utility scans each line in a file for patterns that match what is given on the command line. If a match is found, proceed to the next programming step. If no match is found, continue processing the next line. Although the operation can be complex, the syntax of the command is always: awk {pattern + action} where pattern represents what AWK looks for in the data, and action is a sequence of commands that are executed when a match is found. Curly braces ({}) do not need to appear all the time in a program, but they are used to group a sequence of instructions according to a specific pattern. Understanding Fields The utility divides each input row into records and fields. A record is a single line of input, and each record contains several fields. The default field delimiter is space or tab, and the record delimiter is newline. Although both tabs and spaces are treated as field separators by default (multiple spaces still act as one separator), you can change the separator from a space to any other character. For demonstration, view the following employee list file saved as emp_names: 46012 DULANEY EVAN MOBILE AL46013 DURHAM JEFF MOBILE AL46015 STEEN BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL46019 BOGUE ROBERT PHOENIX AZ46021 JUNE MICAH P HOENIX AZ46022 KANE SHERYL UNKNOWN AR46024 WOOD WILLIAM MUNCIE IN46026 FERGUS SARAH MUNCIE IN46027 BUCK SARAH MUNCIE IN46029 TUTTLE BOB MUNCIE IN When AWK reads the input content, the entire record is assigned to the variable. Each field is separated by a field delimiter and assigned to variables , , etc. A row can contain essentially an infinite number of fields, each of which is accessed by its field number. Therefore, the print output that the command awk {print ,,,,} names will produce is 46012 DULANEY EVAN MOBILE AL46013 DURHAM JEFF MOBILE AL46015 STEEN BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL46019 BOGUE ROBERT PHOENIX AZ46021 JUNE MICAH PHOENIX AZ46022 KANE SHERYL UNKNOWN AR46024 WOOD WILLIAM MUNCIE IN46026 FERGUS SARAH MUNCIE IN46027 BUCK SARAH MUNCIE IN46029 TUTTLE BOB MUNCIE IN One important thing to note is that AWK interprets five fields separated by spaces, but when it prints the display, between each field There is only one space. With the ability to assign a unique number to each field, you can choose to print only specific fields. For example, to print only the names of each record, just select the second and third fields to print: $ awk {print ,} emp_namesDULANEY EVANDURHAM JEFFSTEEN BILLFELDMAN EVANSWIM STEVEBOGUE ROBERTJUNE MICAHKANE SHERYLWOOD WILLIAMFERGUS SARAHBUCK SARAHTUTTLE BOB$ You can also specify the by Print fields in any order, regardless of how they exist in the record. So, just display the name field, and reverse the order, displaying first name first and then last name: $ awk {print ,} emp_namesEVAN DULANEYJEFF DURHAMBILL STEENEVAN FELDMANSTEVE SWIMROBERT BOGUEMICAH JUNESHERYL KANEWILLIAM WOODSARAH FERGUSSARAH BUCKBOB TUTTLE$ Use patterns by including a pattern that must match , you can choose to operate only on specific records instead of all records. The simplest form of pattern matching is a search, where the item to match is enclosed in slashes (/pattern/). For example, to perform the previous operation only for those employees who live in Alabama: $ awk /AL/ {print ,} emp_namesEVAN DULANEYJEFF DURHAMBILL STEENEVAN FELDMANSTEVE SWIM$ If you do not specify the fields to print, the entire matching entry will be printed : $ awk /AL/ emp_names46012 DULANEY EVAN MOBILE AL46013 DURHAM JEFF MOBILE AL46015 STEEN BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL$ Multiple commands for the same data set can be separated by semicolons (;). For example, to print the name on one line and the city and state on another: $ awk /AL/ {print , ; print ,} emp_namesEVAN DULANEYMOBILE ALJEFF DURHAMMOBILE ALBILL STEENMOBILE ALEVAN FELDMANMOBILE ALSTEVE SWIMUNKNOWN AL$ If no semicolon is used (print ,,,) will display everything on the same line.On the other hand, if the two print statements are given separately, it will produce completely different results: $ awk /AL/ {print ,} {print ,} emp_namesEVAN DULANEYMOBILE ALJEFF DURHAMMOBILE ALBILL STEENMOBILE ALEVAN FELDMANMOBILE ALSTEVE SWIMUNKNOWN ALPHOENIX AZPHOENIX AZUNKNOWN ARMUNCIE INMUNCIE INMUNCIE INMUNCIE IN$ will only give fields three and two if AL is found in the list. However, fields four and five are unconditional and they are always printed. Only the command in the first set of curly braces has an effect on the command immediately preceding it (/AL/). The result is very unreadable and could be made slightly clearer. First, insert a space and comma between the city and state. Then, place a blank line after every two lines displayed: $ awk /AL/ {print , ; print ", ""n"} emp_namesEVAN DULANEYMOBILE, ALJEFF DURHAMMOBILE, ALBILL STEENMOBILE, ALEVAN FELDMANMOBILE, ALSTEVE SWIMUNKNOWN, AL$ on the fourth and the fifth field, add a comma and a space (between the quotes), and after the fifth field, print a newline character (n). All the special characters that can be used in the echo command can also be used in the AWK print statement, including: n (newline) t (tab) b (backspace) f (feed) r (carriage return) Therefore, to read To take all five fields initially separated by tabs and print them using tabs too, you can program as follows $ awk {print "t""t""t""t"} emp_names46012 DULANEY EVAN MOBILE AL46013 DURHAM JEFF MOBILE AL46015 STEEN BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL46019 BOGUE ROBERT PHOENIX AZ46021 JUNE MICAH PHOENIX AZ46022 KANE SHERYL UNKNOWN AR46024 WOOD WILLIAM MUNCIE IN460 26 FERGUS SARAH MUNCIE IN46027 BUCK SARAH MUNCIE IN46029 TUTTLE BOB MUNCIE IN$ by setting multiple items consecutively Standard and separated by pipe (|) symbols, you can search for multiple pattern matches at once: $ awk /AL|IN/ emp_names46012 DULANEY EVAN MOBILE AL46013 DURHAM JEFF MOBILE AL46015 STEEN BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL46024 WOOD WILLIAM MUNCIE IN46026 FERGUS SARAH MUNCIE IN46027 BUCK SARAH MUNCIE IN46029 TUTTLE BOB MUNCIE IN$ This will find matching records for every resident of Alabama and Indiana. But while trying to find out who lives in Arizona, a problem arises: $ awk /AR/ emp_names46019 BOGUE ROBERT PHOENIX AZ46021 JUNE MICAH PHOENIX AZ46022 KANE SHERYL UNKNOWN AZ46026 FERGUS SARAH MUNCIE IN46027 BUCK SARAH MUNCIE IN$Employees 46026 and 4 6027 No Live in Arizona; but their names contain the sequence of characters being searched for. Keep in mind that when doing pattern matching in AWK, such as grep, sed, or most other Linux/Unix commands, a match will be found anywhere in the record (line) unless otherwise specified. To solve this problem, the search must be tied to a specific field. This is accomplished by utilizing a tilde (?) along with a description of a specific field, as shown in the following example: $ awk ? /AR/ emp_names46019 BOGUE ROBERT PHOENIX AZ46021 JUNE MICAH PHOENIX AZ46022 KANE SHERYL UNKNOWN AZ$ tilde (indicates a match ) is a tilde (!?) preceded by an exclamation point. These characters tell the program to find all rows that match the search sequence if it does not appear in the specified field: $ awk !? /AR/ names46012 DULANEY EVAN MOBILE AL46013 DURHAM JEFF MOBILE AL46015 STEEN BILL MOBILE AL46017 FELDMAN EVAN MOBILE AL46018 SWIM STEVE UNKNOWN AL46024 WOOD WILLIAM MUNCIE IN46026 FERGUS SARAH MUNCIE IN46027 BUCK SARAH MUNCIE IN46029 TUTTLE BOB MUNCIE IN$ In this case, all rows without an AR in the fifth field will be displayed—including the two Sarah entries, both of which The entry does contain an AR, but in the third field instead of the fifth. Braces and field delimiters The brace characters play an important role in AWK commands. Actions that appear between parentheses indicate what is going to happen and when. When using only one pair of brackets: {print,} all operations between the brackets occur simultaneously. When using more than one pair of parentheses: {print }{print } executes the first set of commands, and after that command completes, executes the second set of commands. Note the difference between the following two lists: $ awk {print ,} namesEVAN DULANEYJEFF DURHAMBILL STEENEVAN FELDMANSTEVE SWIMROBERT BOGUEMICAH JUNESHERYL KANEWILLIAM WOODSARAH FERGUSSARAH BUCKBOB TUTTLE$$ awk {print }{print } namesEVANDULANEYJEFFDURHAMBILLSTEENEVANFELDMANSTEVESWIMROBER TBOGUEMICAHJUNESHERYLKANEWILLIAMWOODSARAHFERGUSSARAHBUCKBOBTUTTLE$ To use multiple sets of brackets to perform repeated searches, execute the first The commands in the group are processed until completion; then the second group of commands is processed. If there is a third set of commands, it is executed after the second set of commands completes, and so on. In the resulting printout, there are two separate print commands, so the first command is executed first, followed by the second command, causing each entry to appear on two lines instead of one. The field separator that distinguishes two fields does not always have to be a space; it can be any recognized character.For demonstration purposes, assume that the emp_names file uses colons instead of tabs to separate fields: $ cat emp_names46012:DULANEY:EVAN:MOBILE:AL46013:DURHAM:JEFF:MOBILE:AL46015:STEEN:BILL:MOBILE:AL46017:FELDMAN:EVAN: MOBILE:AL46018:SWIM:STEEVE:UNKNOWN:AL46019:BOGUE:ROBERT:PHOENIX:AZ46021:JUNE:MICAH:PHOENIX:AZ46022:KANE:SHERYL:UNKNOWN:AR46024:WOOD:WILLIAM:MUNCIE:IN4602

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531983.htmlTechArticleAWK utility comes with its own self-contained language, which is available in Unix/Linux and in any environment One of the most powerful data processing engines. This programming and data manipulation language (...
Statement
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
如何在 Windows 11 上安装经典 Shell?如何在 Windows 11 上安装经典 Shell?Apr 21, 2023 pm 09:13 PM

<p>定制您的操作系统是让您的日常生活更加愉快的绝佳方式。您可以更改用户界面、应用自定义主题、添加小部件等等。因此,我们今天将向您展示如何在Windows11上安装ClassicShell。</p><p>该程序已经存在了很长时间,并允许您修改操作系统。志愿者现在已经开始运营该组织,该组织于2017年解散。新项目名为OpenShell,目前在Github上可供感兴趣的人使用。</p>&a

Explorer.exe 在系统启动时不启动 [修复]Explorer.exe 在系统启动时不启动 [修复]Jun 03, 2023 am 08:31 AM

如今,许多Windows用户开始遇到严重的Windows系统问题。问题是系统加载后Explorer.exe无法启动,用户无法打开文件或文件夹。虽然,Windows用户在某些情况下可以使用命令提示符手动打开Windows资源管理器,并且每次系统重新启动或系统启动后都必须这样做。这可能是有问题的,并且是由于下面提到的以下因素造成的。损坏的系统文件。启用快速启动设置。过时或有问题的显示驱动程序。对系统中的某些服务进行了更改。修改后的注册表文件。请记住以上所有因素,我们提出了一些肯定会对用户有所帮助

PowerShell 部署失败并出现 HRESULT 0x80073D02 问题修复PowerShell 部署失败并出现 HRESULT 0x80073D02 问题修复May 10, 2023 am 11:02 AM

您在运行脚本时是否看到此错误消息“Add-AppxPackage:部署失败,HRESULT:0x80073D02,无法安装该包,因为它修改的资源当前正在使用中。PowerShell中出现错误0x80073D02…”?如错误消息所述,当用户在前一个进程运行时尝试重新注册一个或所有WindowsShellExperienceHost应用程序时,确实会发生这种情况。我们已经获得了一些简单的解决方案来快速解决这个问题。修复1–终止体验主机进程您必须在执行powershell命令之前结束

Linux快速删除文件末尾行的操作步骤Linux快速删除文件末尾行的操作步骤Mar 01, 2024 pm 09:36 PM

Linux系统下在处理文件时,有时候需要删除文件末尾的行。这种操作在实际应用中很常见,可以通过一些简单的命令来实现。本文将介绍在Linux系统中快速删除文件末尾行的操作步骤,同时提供具体的代码示例。步骤一:查看文件末尾行在进行删除操作之前,首先需要确认文件的末尾行是哪一行。可以使用tail命令来查看文件的末尾行,具体命令如下:tail-n1filena

在 Windows 上运行 shell 脚本文件的不同方法在 Windows 上运行 shell 脚本文件的不同方法Apr 13, 2023 am 11:58 AM

适用于 Linux 的 Windows 子系统第一种选择是使用适用于 Linux 或 WSL 的 Windows 子系统,这是一个兼容层,用于在 Windows 系统上本地运行 Linux 二进制可执行文件。它适用于大多数场景,允许您在 Windows 11/10 中运行 shell 脚本。WSL 不会自动可用,因此您必须通过 Windows 设备的开发人员设置启用它。您可以通过转到设置 > 更新和安全 > 对于开发人员来完成。切换到开发人员模式并通过选择是确认提示。接下来,查找 W

超硬核!11个非常实用的 Python 和 Shell 拿来就用脚本实例!超硬核!11个非常实用的 Python 和 Shell 拿来就用脚本实例!Apr 12, 2023 pm 01:52 PM

Python 脚本部分实例:企业微信告警、FTP 客户端、SSH 客户端、Saltstack 客户端、vCenter 客户端、获取域名 ssl 证书过期时间、发送今天的天气预报以及未来的天气趋势图;Shell 脚本部分实例:SVN 完整备份、Zabbix 监控用户密码过期、构建本地 YUM 以及上篇文章中有读者的需求(负载高时,查出占用比较高的进程脚本并存储或推送通知);篇幅有些长,还请大家耐心翻到文末,毕竟有彩蛋。Python 脚本部分企业微信告警此脚本通过企业微信应用,进行微信告警,可用于

以下是 Open Shell Windows 11 无法正常工作问题的修复以下是 Open Shell Windows 11 无法正常工作问题的修复Apr 14, 2023 pm 02:07 PM

无法在Windows 11上运行的 Open shell 并不是一个新问题,并且自从这个新操作系统问世以来一直困扰着用户。Open-Shell Windows 11 不工作问题的原因并不具体。它可能是由程序中的意外错误、病毒或恶意软件的存在或损坏的系统文件引起的。对于那些不知道的人,Open-Shell 是 2017 年停产的 Classic Shell 的替代品。您可以查看我们的教程,了解如何在 Windows 11 上安装 Classic Shell。如何替换 Windows 11 的开始菜

如何安装 Open Shell 以恢复 Windows 11 上的经典开始菜单如何安装 Open Shell 以恢复 Windows 11 上的经典开始菜单Apr 18, 2023 pm 10:10 PM

OpenShell是一个免费的软件实用程序,可用于自定义Windows11开始菜单,使其类似于经典风格的菜单或Windows7样式的菜单。以前版本的Windows上的开始菜单为用户提供了一种浏览其系统内容的简单方法。基本上,OpenShell是ClassicShell的替代品,它提供了不同的用户界面元素,有助于从以前的Windows版本获取后一个版本的功能。一旦ClassicShell的开发在2017年停止,它就由GitHub志愿者以OpenShell的名义维护和开发。它与Win

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft