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

How to use the awk command of Linux

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

Share

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

Today, the editor will share with you the relevant knowledge points about how to use the awk command of Linux. The content is detailed and the logic is clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article. Let's take a look.

Awk is a text analysis tool awk is particularly powerful compared to grep and sed. The most basic function of awk language is to browse and extract information from files or strings based on specified rules.

Awk built-in variables ARGC command line parameters ``ARGV command line parameter arrangement ``ENVIRON supports the use of system environment variables in queues using ``FILENAME ``awk`` browsed filename ``FNR browsing file number of records`` FS setting input domain separator Equivalent to the command line-F option ``NF the number of domains of browsing records ``NR the number of records read ``OFS output domain separator ``ORS output record separator ``RS control record separator`` $0 variable refers to the entire record. $1 represents the first field of the current row, and $2 represents the second field of the current row. and so on. $NF is number finally, which represents the information of the last column, which is different from the variable NF. The variable NF counts the total number of each row and column. Commonly used commands show that awk is good at column output.

Search / etc/passwd all lines with the root keyword

Awk``` / root/' ``/ etc/ passwd`` [this is the use of pattern. Only lines that match pattern (in this case, root) will execute action (no action specified, default output of each line)]

Search for all lines with the root keyword in / etc/passwd and display the corresponding shell

Awk``-F: ``/ root/ {print $7}'`` / etc/passwd

Statistics / etc/passwd: file name, line number of each line, number of columns per line, corresponding full line content:

Awk``-F ```:``` {print "filename:" FILENAME ", linenumber:" NR ", columns:" NF ", linecontent:" $0}'``/ etc/passwd

Using printf instead of print can make the code more concise and easy to read

Awk``-F: ``{printf ("filename:s, linenumber:%3s,column:%3s,content:%3f\ n", FILENAME,NR,NF,$0)}'`` / etc/passwd

Print the second line of information for / etc/passwd/

Awk``-F: ``'NR==2 {print "filename:" FILENAME, $0}'`` / etc/passwd

Filtering usage of awk

Ls``-lF | ``awk`` / ^ dbank 'specify a specific delimiter and query the first column

Awk``-F ``":" ``'{print $1}'`` / etc/passwd specify a specific delimiter and query the last column

Awk``-F ``":" ``'{print $NF}'`` / etc/passwd specifies a specific delimiter and queries the penultimate column

Awk``-F ``":" ``'{print $NF-1}'`` / etc/passwd gets the information of the first column of lines 12 to 31

Awk``-F ``":" ``'{if (NR12) print $1}'`` / etc/passwd

Use of multi-delimiters:

[root@localhost ftl] ``# awk-F "[/]"'NR = = 4 {print $0, "\ n", $1}'/ etc/ passwd`` where / is the delimiter, multiple delimiters are used by [] and then write the delimiter in it

Added BEGIN and END

[root@localhost ftl] ``# cat / etc/passwd | awk-F: 'BEGIN {print "name, shell"} {print $1 END {print "hello world"}'

View the IP information with the most recent logins

[root@localhost ftl] ``# last | awk'{S [$3] + +} END {for (an in S) {print S [a], a}}'| uniq | sort-rh

Use regularization to filter multiple spaces

[root@localhost ~] ``# ifconfig | grep eth* | awk-F'[] +'{print $1}'

Awk programming-variables and assignments in addition to the built-in variables in awk, awk can also customize variables. Circular statements in awk are also borrowed from the C language and support while, do/while, for, break, continue. The semantics of these keywords are exactly the same as those in the C language.

Count the number and sum of files greater than 100k in a folder

Ls``-l | ``awk`` {if ($5 > 100) {count++; sum+=$5}} {print "Count:" count, "Sum:" sum} ``[because ``awk`` polls statistics, so the whole process is displayed] `ls``-l | `awk`` {if ($5 > 100) {count++; sum+=$5} END {print "Count:" count, "Sum:" sum}`` [only the final result is displayed after Tianjie END] Note: count is a custom variable. There was only one print in the previous action {}. In fact, print is just a statement, while action {} can have multiple statements separated by the; sign.

Statistics show that / etc/passwd 's account

Awk``-F: ```{count++;} END {print count}'``/ etc/ passwd``cat`` / etc/ passwd`` | ``wc``-l``awk``-F```: ````BEGIN {name [count] = $1countcounter;}; END {for (I = 0; I usually, awk is a processing unit of a file. Awk receives each line of the file and then executes the appropriate command to process the text. The above is the Linux system-related content shared by Liangxu tutorial Network for all friends. If you want to know more about Linux, remember to follow the official account "good Linux", or scan the QR code below to follow, more practical information is waiting for you! These are all the contents of this article entitled "how to use Linux's awk commands". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report