awk usage (ii)---program language features

Source: Internet
Author: User

1 built-in variables (built-in) commonly used

FS input field delimiter default whitespace changed by option-F or re-assigned FS

OFS output field delimiter default space, change can be re-assigned value

The filewidths is separated by spaces Iade defines the width of each field, that is, replacing FS with field lengths to determine how the fields are split

FNR the number of data rows in the current data file

Total number of fields in the NR data file

Number of input data rows that have been processed by NF

Example one: The use of FS and OFS

[Email protected] ~]# echo "Data1,date2,date3" | awk ' begin{fs= ', '; ofs= '--'} {print $1,$2,$3} '

Data1--date2--date3

Contrast

[Email protected] ~]# echo "Data1,date2,date3" | awk ' begin{fs= ', '} {print $1,$2,$3} '

Data1 Date2 Date3

Example two: the use of filedwidths

Separate phone numbers

[Email protected] ~]# echo "02164300980" | Gawk ' begin{fieldwidths= ' 3 8 "}{print $, $"

021 64300980

[Email protected] ~]# echo "02164300980" | Gawk ' begin{fieldwidths= ' 3 8 "; ofs="-"}{print $, $ $} '

021-64300980

Example three: The difference between NF NR RNR

[Email protected] ~]# echo "data1 date2 date3" | awk ' {print NF} '

3

NF good understanding is processed by how many fields of this record

[Email protected] ~]# echo-e "data1 date2 date3\ndate4 date5" | awk ' {print NF} '

3

2

The difference between NR and FNR is that if multiple input files are being processed, the two meanings are equal if you are processing one file

If the data file processed is two or more, FNR indicates the number of records processed in the file currently being processed

[[email protected] ~]# awk ' {print Nr,fnr} '/etc/issue/etc/issue

1 1

2 2

3 3

4 1

5 2

6 3

Explanation NR will accumulate the already recorded lines, regardless of which file the record line is from, and FNR the records processed in the file currently being processed

2 awk working with arrays and traversing arrays

The array variable is assigned the var[index]= element, which, if undefined, is null by default, and the index value can be any value such as a character

[[email protected] ~]# awk ' begin{var[1] = 3;var[2] = 4;total = var[1] + var[2];p rint Total} '

7

Iterating over array usages

for (var in array)

{

Statement

}

Note that the FOR statement assigns the next index value of each array that is associated with the arrays to the variable var instead of the data element value

[[email protected] ~]# awk ' begin{var[1] = 3;var[2] = 4; for (I in Var) {print "Index:", I, "Value:", Var[i]}} '

Index:1 Value:3

Index:2 Value:4

[[email protected] ~]# awk ' begin{var["v1"] = 3;var["v2"] = 4; for (I in Var) {print "Index:", I, "Value:", Var[i]}} '

Index:v1 Value:3

Index:v2 Value:4

The index value here is the character

awk usage (ii)---program language features

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.