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

Analysis of usage cases of awk and grep

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "awk and grep use case analysis", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "awk and grep use case analysis" bar!

Brief introduction to awk & & grep

In daily work, commands such as awk and grep are often used to do simple data statistics, which is simple and efficient. The most basic function of the awk command is to extract information from files or strings. Usually awk processes units as a behavior of a file. Awk receives each line of the file and then executes the appropriate command to process the text. The grep command is mainly used to find qualified strings in the file.

Awk sample file / / example.txt

A 10 200

A 30 100

B 20 300

C 10 100

C 20 800

C 30600 key parameter-F: specify delimiter FILENAME: file name NR: line number NF: number of columns per line $0: variable means that the entire record BEGIN and END:BEGIN are executed before any data is read, which is generally used for initialization operations, while END is placed to be executed after all data reads are completed, generally used to output processing results example # specifies the delimiter as a space Print the file name, line number, number of columns, and the contents of the current line each time

Awk-F''{print "filename:" FILENAME, "linenumber:" NR, "columns:" NF, "linecontent:" $0} 'example.txt

# print the first column

Awk-F'{print $1} 'example.txt

# print the penultimate column

Awk-F'{print $NF} 'example.txt

# print the penultimate column

Awk-F'{print $NF-1} 'example.txt

# print the second line and the whole line

Awk-F 'NR==2 {print "filename:" FILENAME, "linecontent:" $0}' example.txt

# summation of the second column

Awk'{sum+=$2} END {print "sum:" sum} 'example.txt

# the first column is key, and the second column is value. Find the sum of all value corresponding to each key

Awk'{sum [$1] + = $2} END {for (idx in sum) print idx, sum [I]} 'example.txt

# Statistics the elements in the first column appear several times respectively

Awk'{sum [$1] + = 1} END {for (i in sum) print I example.txt

# count the number of occurrences of the first two elements

Awk'{sum [$1 "" $2] + +} END {for (i in sum) print I [I]}'

# count the number of rows with values of ≤ 500and > 500in the third column

Awk-F'BEGIN ($3) {if ($3)

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

Internet Technology

Wechat

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

12
Report