Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Awk command usage and programming

2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

Awk (gawk): report generator, formatted text output: awk,gawk

Basic usage: gawk [options] 'program' FILE.

Program: PATTERN {ACTION STATEMENT}; can have multiple statements separated by semicolons

Print,printf

Options:

-F: indicates the field delimiter used when entering data

-v var=value: custom variable

Variables:

1) built-in variables

FS:input field seperator (input field delimiter, default is white space character)

# awk-v FS=':''{print $1}'/ etc/passwd

# awk-F:'{print $1}'/ etc/passwd (this command has the same effect as the command above)

Note: using FS, you can use more than one character at a time as a delimiter; for example: awk-v FS= "[,:]"'{print $3recovery1 characters 2} 'd.txt

OFS:output field seperator (output field delimiter, default is white space character)

# awk-v FS=':'-v OFS=':''{print $1 OFS=':''/ etc/passwd

RS:input record seperator, the newline character when entering

ORS:output record seperator, the newline character on output

NF:number of field, number of fields per row

{print NF}, {print $NF} (there is no need to add a $symbol to reference variables in awk)

The last field in the line is represented by $NF

NR:number of record, number of rows processed

# awk'{print NR}'/ etc/fstab

FNR: the number of lines processed per file

# awk'{print FNR}'/ etc/fstab / etc/issue

FILENAME: the file name of the file currently being processed

# awk'{print FILENAME}'/ etc/fstab

ARGC: the number of command line arguments

# awk 'BEGIN {print ARGC}' / etc/fstab

ARGV: an array that holds the parameters given by the command line

# awk 'BEGIN {print ARGV [0]}' / etc/fstab

2) Custom variable

-v var=value

Variable names are case-sensitive

# awk-v test='Hello gawk' 'BEGIN {print test}'

Define directly in program

# awk 'BEGIN {test= "Hello gawk"; print test}'

Operator:

Arithmetic operator:

X magic, x ^ y, x% y.

-x

+ x: convert to numeric value

String operator: operator without sign by default: indicates string concatenation

Assignment operator:

=, +, -, *, /,%, ^ =

Comparison operator:

>, > =, read from file a. The less than sign indicates redirection and cannot be understood as the less than sign.

Assign the read to a variable: getline name < "a"

2) Custom function

Exercise:

1. Count the number of occurrences of each file system type in the / etc/fstab file

two。 Count the number of times each word appears in the specified file

3. Count the number of words and characters in the following sentence

The squid project provides a number of resources toassist users design

Implement and support squid installations. Please browsethe documentation

And support ections for more infomation

4. Count the average of scores in grade.txt

Mona 70 77 85 83 70 89

John 85 92 78 94 88 91

Andrea 89 90 85 94 90 95

Jasper 84 88 80 92 84 82

Dunce 64 80 60 60 61 62

Ellis 90 98 89 96 96 92

5. On the basis of question 4, the average score of the class in grade.txt is counted, and the average score of the classmate is classified and counted according to the average score of the classmate.

6. The file test contains the following:

Zhangsan 80

Lisi 81.5

Wangwu 93

Zhangsan 85

Lisi 88

Wangwu 97

Zhangsan 90

Lisi 92

Wangwu 88

Required output format: (average: grade point average, total: total grade point)

Name#average#total

Zhangsan xxx xxx

Lisi xxx xxx

Wangwu xxx xxx

Answer:

1.

# awk'! / ^ # / {fs [$3] + +} END {for (i in fs) {print iMagnefs [I]}'/ etc/fstab

two。

# awk'{for (iTun1teri)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report