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 awk tools in Linux

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

Share

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

This article mainly shows you "how to use awk tools in Linux", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use awk tools in Linux" this article.

Awk is a powerful tool for working with text in linux, or a language that specializes in dealing with strings, and it has its own encoding format. The power of awk also lies in its ability to generate powerful formatted reports.

Awk built-in variables ARGC command line parameters ARGV command line parameters arrangement ENVIRON support queue system environment variables using FILENAME awk browsing filename FNR browse file number of records FS setting input domain delimiter Equivalent to the command line-F option NF the number of fields of browsing records NR the number of records read OFS output domain delimiter 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 in the last column, which is different from the variable NF. The variable NF counts the total number of each row and column, which is displayed by commonly used commands.

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, and only lines that match pattern (in this case root) will execute action (no action specified, default output of each line)] search / etc/passwd for all lines with the root keyword 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'/ ^ dplink'

Specify a specific delimiter to query the first column

Awk-F ":" {print $1}'/ etc/passwd is all the contents of this article entitled "how to use awk tools in Linux". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow 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