Shell text filtering programming (2): the basis of awk

Source: Internet
Author: User
Tags processing text
[Copyright statement: reprinted. Please retain the Source: blog.csdn.net/gentleliu. Mail: shallnew at 163 dot com]
The grep command is used to obtain the line information that complies with the rules. This section describes how to use awk to obtain a specified text field from a file or string.
The basic function of language a w k is to browse and extract information in a file or string based on specified rules. A w k can be used to extract information before other text operations can be performed. The complete a w k script is usually used to format information in a text file.
The most common method to call the awk command in the command line is awk:
Awk [-F separator] 'cmd' filename
CMD is a real awk command, and [-F domain separator] is optional, because a w k uses space as the default domain separator, so if you want to browse text with spaces between domains, you do not need to specify this option, but if you want to browse a file such as p a s w d, each field of this file uses a colon as the separator, you must specify the-F option, such:
Awk-F: '{cmd}'/etc/passwd
When an awk call is made, it reads a record or a row each time, splits the specified domain with the specified split domain (the default value is space when the split domain is not set), and marks it as $1, $2... $ n. Using these domain IDs makes it easier to further process the domain. Then execute some operations of the awk command, and then awk starts to read the next line of content until the entire file is read.
The awk command action is specified in braces. Most of the actions are used for printing, but there are still some longer Code such as if and looping statements and loop exit structures.
In the preceding example, a file (from the/etc/group file) is constructed as follows:
# cat group_file1wireshark x 987usbmon x 986jackuser x 985vboxusers x 984 allenaln x 1001#
Example 1:
# awk '{print}' group_file1wireshark x 987usbmon x 986jackuser x 985vboxusers x 984 allenaln x 1001#
This command has the same effect as the cat command. It directly outputs the file content. The command part of the awk is print. when running the awk, the print command is executed for each line in sequence, and it prints the content of each line of the file. Other code can also be included here, or it can be irrelevant to the entered line. For example:
# awk '{print "hello awk!"}' group_file1hello awk!hello awk!hello awk!hello awk!hello awk!
The number of rows of files that will be promised Hello awk!
Awk is very good at processing text that is divided into multiple logical fields, and allows you to reference each independent field in the awk script effortlessly. For the above file, we can extract the content of the first and third columns of each row (when-F is not specified, the default is space), as shown below:
# awk '{print $1 $3}' group_file1wireshark987usbmon986jackuser985vboxusers984aln1001
The output results are stuck together. We can add some characters to separate them:
# awk '{print "group:"$1 "\tid:"$3}' group_file1    group:wireshark id:987group:usbmon    id:986group:jackuser  id:985group:vboxusers id:984group:aln     id:1001
In {}, there is a space between $3 and $1 in time, and no space will be printed. We need to add the string to output the split printing, or we can add a comma, in this way, there is a space to separate the output.
Whether it looks better or not. Is there a sense that awk is awesome? Don't worry, it's just getting started.
Like other shell commands, awk commands can also be placed in script files. Run the awk command by executing the script file. The script content and execution result are as follows:
# cat 1_awk.sh#!/bin/shawk '{print "name:"$1 "\tid:"$3}' group_file1# ./1_awk.shname:wireshark  id:987name:usbmon    id:986name:jackuser   id:985name:vboxusers  id:984name:aln      id:1001
Generally, for each input line, awk executes each script code block once. However, in many programming cases, initialization code may need to be executed before awk starts to process the text in the input file. In this case, awk allows you to define a begin block. Awk executes the begin block before processing the input file, so it initializes the FS (field separator) variables, print headers, or initialize excellent positions of other global variables that will be referenced in the program.
Awk also provides another special block called the end block. Awk executes this block after processing all rows in the input file. Generally, the end block is used to execute the final calculation or print the summary information that should appear at the end of the output stream.
Modify the above example and add the begin and end blocks:
#!/bin/shawk 'BEGIN {   print "    Name\tID\n==========================="}{   print "name:"$1 "\tid:"$3}END {   print "=============END============="}' group_file1
For rows that are not separated by spaces, you can use-F to set the separator, for example, the following file:
# cat group_file2wireshark:x:987:usbmon:x:986:jackuser:x:985:vboxusers:x:984:allenaln:x:1001:# awk -F: '{print $1, $3}' group_file2wireshark 987usbmon 986jackuser 985vboxusers 984aln 1001
Another way to set the Delimiter is to set the FS variable.

Shell text filtering programming (2): the basis of awk

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.