Variables in awk

Source: Internet
Author: User

Variables in awk

Combined with sed, awk provides almost all-around horizontal and vertical file processing, which can be regarded as a big move in text processing. Of course, awk's powerful sub-processing tool is not a waste of names, features are rich, and the learning cycle is longer. It can be completed without a Help document. Learning awk can be considered re-learning a programming language, because there are too many things in it. Let's proceed step by step. let's first talk about the variables in awk.
Variables in awk include built-in variables and custom variables.
Built-in variables, such as data fields, data row variables, and data variables, may be hard to understand. One by one.
Data fields and data row variables mainly include
FIELDWIDTHS is divided according to the length of the field. For example, if FIELDWIDTHS is set to 3 5 3 4, the output value is 20150401.223300 201. 22 50401.
FS is the delimiter of the input field. For example, if the value of FS is set to 11, 12, and 13, 11, 12, and 13 are separated into 11, 12, and 13.
OFS is the delimiter of the output field, for example, 11,12, and 13. If we separate them by commas, the data will be 11 12 13. If we specify OFS as "--", the output will be 11--12--13.
RS is the delimiter of the input data row, which is used in special scenarios. The following is an example.
ORS is the delimiter used to output data rows. It is used in special scenarios.
We specify a file named a. lst or an example of pm2.5. The following figure shows the data of pm2.5 in recent days.
2015 03 30 100
2015 03 31 150
2015 04 01 70

? Awk 'in in {FS = ""} {print $1 $2 $3} 'a. lst
20150330
20150331
20150401

Note the differences between the following two methods.
? Awk 'in in {FS = ""; OFS = "--"} {print $1 $2 $3} 'a. lst
20150330
20150331
20150401
? Awk 'in in {FS = ""; OFS = "--"} {print $1, $2, $3} 'a. lst
Septem

September-01-01

 

The following example shows how to use RS and ORS.
Assume that the following file exists, and an empty row is generated every three rows. We can selectively intercept data field values.
? Cat a. lst
2015line1 03 30 100
2015 03 31 150
2015 04 01 70

2015line2 03 30 100
2015 03 31 150
2015 04 01 70

2015line3 03 30 100
2015 03 31 150
2015 04 01 70

? Awk 'in in {FS = "\ n"; RS = ""} {print $1, $3} 'a. lst -- in this place, we use RS to separate data rows and divide each column by carriage return based on Null behavior.
2015line1 03 30 100 2015 04 01 70
2015line2 03 30 100 2015 04 01 70
2015line3 03 30 100 2015 04 01 70

In this way, 1st rows and 3rd rows of data are integrated. The following data follows this rule.

If ORS is used, the results will be very different. We use "--" as the output separator.
? Awk 'in in {FS = "\ n"; ORS = "----"} {print $1, $3} 'a. lst
2015line1 03 30 100 ---- 2015 03 31 150 ---- 2015 04 01 70 ---- 2015line2 03 30 100 ---- 2015 03 31 150 ---- 2015 04 01 70 ---- 2015line3 03 30 100 ---- 2015 03 31 150 ---- 2015 04 01 70 ------------

 

 

Data variables may be used less often. Built-in variables include:
ARGC indicates the number of parameters in the current command line.
ARGV array containing command line parameters
ENVIRON indicates the associated array composed of the Current shell environment variables and values
NF indicates the total number of fields in the data file.
NR is the number of processed input data rows
For example.
? Awk 'in in {print ARGC, ARGV [0], ARGV [1]} 'a. lst
2 awk a. lst
Here, ARGC is the number of parameters in the command line. We can see that the values of the two parameters are awk and a. lst respectively, and the subscript starts from 0.

? Awk '{print ENVIRON ["HOME"], ENVIRON ["PATH"]} 'a

/Home/mobaxterm/bin:/drives/c/WINDOWS/system32

 

 

The User-Defined variables are frequently used, and the flexibility of these variables is quite high. There are basically the following scenarios.
Assign a value to the variable in the script and assign a value to the variable on the command line


Assign a value to the variable in the script. For example, if we specify a variable test and initialize it twice, the variable value will change dynamically.
? Awk'
> BEGIN {
> Test = "first_try"
> Print test
> Test = "second_try"
> Print test
> }'
First_try
Second_try


This is understandable when assigning values to variables in the command line.
? Cat a. lst
2015line1 03 30 100
2015 03 31 150
2015 04 01 70

? Awk 'in in {FS = ""} {print $ n} 'n = 3 a. lst
30
31
01

In this case, n = 3 is dynamically assigned based on the set variable value.

If you need to pass the shell variable value, you can use the-v option to implement
? Awk-v n = 3' {print "this is a test", n} 'a
This is a test 3

Test = aaaaa
Echo $ test
Aaaa
? Awk-v t = $ test' {print "testing value:" t} 'a. lst
Testing value: aaaa
Testing value: aaaa
Testing value: aaaa

-------------------------------------- Split line --------------------------------------

Introduction and use of AWK

AWK introduction and Examples

Shell script-AWK text editor syntax

Learning and using AWK in Regular Expressions

AWK diagram of Text Data Processing

How to Use the awk command in Linux

Text Analysis Tool-awk

-------------------------------------- Split line --------------------------------------

This article permanently updates the link address:

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.