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

What are the common commands in Linux system?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly analyzes the relevant knowledge of the commonly used commands in the Linux system, the content is detailed and easy to understand, the operation details are reasonable, and has a certain reference value. If you are interested, you might as well follow the editor to have a look, and follow the editor to learn more about "what are the common commands of the Linux system".

Awk is a great language, it is suitable for text processing and report generation, its syntax is more common, drawing on some of the essence of some languages, such as C language. In the daily work of linux system, play a very important role, mastering awk will make your work higher and higher.

Usage

Awk'{pattern + action}'{filenames} although the operation can be complex, the syntax is always like this, where pattern represents what AWK looks for in the data, and action is a series of commands that are executed when a match is found. Curly braces ({}) do not need to appear all the time in the program, but they are used to group a series of instructions according to a specific pattern. Pattern is the regular expression that you want to represent, surrounded by diagonal bars.

The most basic function of awk language is to browse and extract information from files or strings based on specified rules. After awk extracts information, other text operations can be carried out. A complete awk script is usually used to format information in a text file.

Typically, 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.

There are three ways to call awk to call awk

1. Command line mode awk [- F field-separator] 'commands' input-file (s) where commands is the real awk command and [- F domain delimiter] is optional. Input-file (s) is the file to be processed. In awk, each item separated by a domain delimiter in each line of a file is called a field. In general, when the-F domain delimiter is not named, the default field delimiter is a space. 2.shell script inserts all awk commands into a file and makes the awk program executable, and then the awk command interpreter, as the first line of the script, is called by typing the name of the script. Equivalent to the first line of the shell script: #! / bin/sh can be replaced by: #! / bin/awk 3. Insert all the awk commands into a separate file, and then call: awk- f awk-script-file input-file (s) where the-f option loads the awk script in awk-script-file, and input-file (s) is the same as above.

A starter example assumes that the output of last-n 5 is as follows

[root@www ~] # last-n 5 if you only show the five recently logged-in accounts # last-n 5 | awk'{print $1} 'root root root dmtsai rootawk workflow is like this: read in a record separated by a'\ n' line break, and then divide the record into domains according to the specified domain delimiter, populating the domain, then all domains 1 represent the first domain and represent the first domain. The default domain delimiter is a blank key or key, so 1 represents the logged-in user, $3 represents the logged-in user ip, and so on. If only the account # cat / etc/passwd of / etc/passwd is displayed | awk-F':'{print $1} 'root daemon bin sys this is an example of awk+action, and each line executes action {print $1}. -F specifies that the domain delimiter is':'. If only the account of / etc/passwd and the corresponding shell of the account are displayed, and the account and shell are separated by the tab key # cat / etc/passwd | awk-F':'{print $1 "\ t" $7} 'root / bin/bash daemon / bin/sh bin/ bin/sh sys / bin/sh if only the account of / etc/passwd and the corresponding shell of the account are displayed, and the account and shell are separated by a comma, and the column name name,shell is added to all lines Add "blue,/bin/nosh" to the last line. Cat / etc/passwd | awk-F': 'BEGIN {print "name,shell"} {print $1 "," $7} END {print "blue,/bin/nosh"}' name,shell root,/bin/bash daemon,/bin/sh bin,/bin/sh sys,/bin/sh. The blue,/bin/noshawk workflow is like this: first execute BEGING, then read the file, read in a record separated by the / n newline character, and then divide the record into fields according to the specified domain delimiter, populating the domain, indicating that all fields 1 represent the first domain, $n represents the nth domain, and then start executing the action action corresponding to the mode. Then start reading the second record until all the records have been read, and finally perform the END operation. Search / etc/passwd for all lines with the root keyword # awk-F:'/ root/' / etc/passwd root:x:0:0:root:/root:/bin/bash this is an example of 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 supports regularities, for example: awk-F:'/ ^ root/' / etc/passwd at the beginning of root: search / etc/passwd all lines with the root keyword and display the corresponding shell # awk-F:'/ root/ {print $7}'/ etc/passwd / bin/bash where action {print $7} awk built-in variable awk has many built-in variables to set environment information, which can be changed Here are some of the most commonly used variables. ARGC command line parameters ARGV command line parameters arrangement ENVIRON support queue system environment variables using FILENAME awk browsed filename FNR browse file number of records FS setting input field delimiter Equivalent to the command line-F option NF the number of fields of browsing records NR the number of records read OFS output field delimiter ORS output record delimiter RS control record separator in addition, the variable refers to the entire record. 1 represents the first field of the current line, $2 represents the second field of the current row, …... and so on. Statistics / etc/passwd: file name, line number of each line, number of columns per line The corresponding full line content: # awk-F':'{print "filename:" FILENAME ", linenumber:" NR ", columns:" NF ", linecontent:" $0}'/ etc/passwd filename:/etc/passwd,linenumber:1,columns:7,linecontent:root:x:0:0:root:/root:/bin/bash filename:/etc/passwd,linenumber:2,columns:7,linecontent:daemon:x:1:1:daemon:/usr/sbin:/bin/sh filename:/etc/passwd,linenumber:3 Columns:7,linecontent:bin:x:2:2:bin:/bin:/bin/sh filename:/etc/passwd,linenumber:4,columns:7,linecontent:sys:x:3:3:sys:/dev:/bin/sh uses printf instead of print to make the code more concise Easy to read awk-F':'{printf ("filename:s,linenumber:%s,columns:%s,linecontent:%s\ n", FILENAME,NR,NF,$0)}'/ etc/passwdprint and printfawk provide both print and printf printout functions. Where the argument to the print function can be a variable, a numeric value, or a string. Strings must be referenced in double quotes and parameters separated by commas. If there is no comma, the parameters are concatenated and cannot be distinguished. Here, the comma serves the same purpose as the delimiter of the output file, except that the latter is a space. Printf function, whose usage is basically similar to printf in c language, can format strings. When the output is complex, printf is easier to use and the code is easier to understand. Awk programming variables and assignments in addition to awk's built-in variables, awk can also customize variables. The following counts the number of / etc/passwd accounts awk'{count++;print $0;} END {print "user count is", count}'/ etc/passwd root:x:0:0:root:/root:/bin/bash. User count is 40count 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. Count is not initialized here, although the default is 0, but it is appropriate to initialize it to 0: awk 'BEGIN {count=0;print "[start] user count is", count} {count=count+1;print $0;} END {print "[end] user count is", count}' / etc/passwd [start] user count is 0 root:x:0:0:root:/root:/bin/bash. [end] user count is 40 counts the number of bytes occupied by files in a folder ls-l | awk 'BEGIN {size=0;} {size=size+$5;} END {print "[end] size is", size}' [end] size is 8657198 if displayed in M: ls-l | awk 'BEGIN {size=0;} {size=size+$5 } END {print "[end] size is", size/1024/1024, "M"}'[end] size is 8.25889 M Note that the statistics do not include subdirectories of the folder. Conditional statements in awk are borrowed from C language. See the following declaration: if (expression) {statement; statement;...} if (expression) {statement;} else {statement2;} if (expression) {statement1;} else if (expression1) {statement2;} else {statement3 } count the number of bytes occupied by files in a folder and filter files of 4096 size (usually folders): ls-l | awk 'BEGIN {size=0;print "[start] size is", size} {if ($5 million) {size=size+$5 }} END {print "[end] size is", size/1024/1024, "M"}'[end] size is 8.22339 M Loop statement awk also borrows from C language and supports while, do/while, for, break, continue. The semantics of these keywords are exactly the same as those in C language. Array because the subscript of an array in awk can be numeric and alphabetic, the subscript of an array is often called a keyword (key). The values and keywords are stored in an internal table for the key/value application hash. Because hash is not stored sequentially, when you display the contents of the array, you will find that they are not displayed in the order you expected. Arrays, like variables, are created automatically when they are used, and awk also automatically determines whether they store numbers or strings. In general, arrays in awk are used to collect information from records and can be used to calculate sums, count words, track the number of times the template has been matched, and so on. Show / etc/passwd 's account awk-F': 'BEGIN {count=0;} {name [count] = $1 leading countdown;}; END {for (I = 0; I here use the for loop to traverse the array so that the sharing of common ask commands in the Linux system is over. If you have any questions, you can submit them to us through the comments section. 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! This is the end of the introduction on "what are the common commands in the Linux system?" more relevant content can be searched for previous articles, hoping to help you answer questions and questions, please support the website!

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