In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly shows you "how to use awk to add text", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "how to use awk to add text" this article.
Add text
You can add text to the display in the same way that you add control sequences or other characters. For example, to change the delimiter from a space to a colon, the command is
Awk'{print $1 ":" $2 ":" $3 ":" $4 ":" $5} 'emp_names > new_emp_names
In this case, the character (:) is enclosed in quotation marks ("/"), which is added between each field. The value between quotation marks can be anything. For example, create a database-like display of employees living in Alabama:
$awk'$5 ~ / AL/ {print "NAME:" $2 "," $3 "nCITY-STATE:" $4 "," $5 "n"} 'emp_namesNAME: DULANEY, EVANCITY-STATE: MOBILE, ALNAME: DURHAM, JEFFCITY-STATE: MOBILE, ALNAME: STEEN, BILLCITY-STATE: MOBILE, ALNAME: FELDMAN, EVANCITY-STATE: MOBILE, ALNAME: SWIM, STEVECITY-STATE: UNKNOWN, AL$
Mathematical operation
In addition to text functionality, AWK provides a full range of arithmetic operators, including the following symbols:
+ add the numbers
-minus
* multiply
/ divide
^ perform exponential operation
% provide module
+ + add one to the value of the variable
+ = assign the results of other operations to variables
-reduce the variable by one
-= assign the result of the subtraction operation to the variable
* = assign the result of multiplication
/ = assign the result of the division operation
% = assign the result of the modeling operation
For example, suppose the following files exist on your machine, listing the items in the hardware store in detail:
$cat inventoryhammers 5 7.99drills 2 29.99punches 7 3.59drifts 2 4.09bits 55 1.19saws 123 14.99nails 800.19 screws 80.29brads 100.24 $
The first business order calculates the inventory value of each item by multiplying the value of the second field (quantity) by the value of the third field (price):
$awk'{print $1, "QTY:" $2, "PRICE:" $3 "TOTAL:" $2 million 3} 'inventoryhammers QTY: 5 PRICE: 7.99 TOTAL: 39.95drills QTY: 2 PRICE: 29.99 TOTAL: 59.98punches QTY: 7 PRICE: 3.59 TOTAL: 25.13drifts QTY: 2 PRICE: 4.09 TOTAL: 8.18bits QTY: 55 PRICE: 1.19 TOTAL: 65.45saws QTY: 123 PRICE: 14.99 TOTAL: 1843.77nails QTY: 800 PRICE: .19 TOTAL: 152screws QTY: 80 PRICE: .29 TOTAL: 23.2brads QTY: 100 PRICE: .24 TOTAL: 24 $
If the lines themselves are not important, and you just want to determine how many items are in the store, you can assign a normal variable to increase the number of items in each record:
$awk'{x=x+$2} {print x} 'inventory5714167119499410741174 $
According to this data, there are 1174 items in the store. When executed for the first time, the variable x has no value, so it takes the value of the second field in the first row. On the second execution, it retains the value of the first row and adds the value of the second row, and so on, until the cumulative sum is reached.
The same process can be applied to determine the total value of existing inventory:
$awk'{xxx + ($2cm 3)} {print x} 'inventory39.9599.93125.06133.24198.692042.462194.462217.662241.66 $
Therefore, the value of 1174 items is $2241.66. Although this process can get a total value, it has a poor appearance and needs to be processed into an actual report. With some additions, it is easy to make the display cleaner:
{print $1, "QTY:" $2, "PRICE:" $3, "TOTAL:" $2, $2, $2, $2, $3, $2, $3, $2, $3, $2, $2, $3, $2, $ "BAL:" x} 'inventoryhammers QTY: 5 PRICE: 7.99 TOTAL: 39.95 BAL: 39.95drills QTY: 2 PRICE: 29.99 TOTAL: 59.98 BAL: 99.93punches QTY: 7 PRICE: 3.59 TOTAL: 25.13 BAL: 125.06drifts QTY: 2 PRICE: 4.09 TOTAL: 8.18 BAL: 133.24bits QTY: 55 PRICE: 1.19 TOTAL: 65.45 BAL: 198.69saws QTY: 123 PRICE: 14.99 TOTAL: 1843.77 BAL: 2042.46nails QTY: 800 PRICE: .19 TOTAL: 152 BAL: 2194.46screws QTY: 80 PRICE: .29 TOTAL: 23.2 BAL: 2217.66brads QTY: 100 PRICE: .24 TOTAL: 24 BAL: 2241.66 $
The process provides a list of each record, assigns the total value to the inventory value, and maintains a balanced operation of the store assets.
BEGIN and END
Using the BEGIN and END statements, you can specify that the operation takes place before or after the actual start of the process, respectively. BEGIN statements are most commonly used to create variables or display headings. The END statement, on the other hand, can be used to continue processing after the program ends.
In the previous example, the total value of the item is generated using the following routine:
Awk'{xxx + ($2 print x} 3)} {xx} 'inventory
This routine displays each line in the file when running the total cumulative time. There is no other way to specify it, and not to print on each line also causes it to never print. However, this problem can be avoided by using END statements:
END {print "Total Value of Inventory:" x} 'inventoryTotal Value of Inventory: 2241.66 $
The variable x is defined, which processes each row; however, the display is not generated until all processing is complete. Although it can be used as a stand-alone routine, it can also be placed in the previous code list, add more information, and generate a more complete report:
{print $1, "QTY:" $2, "PRICE:" $3. $awk'{xxx + ($2x3)} {xxx + ($2xx3)} {xxx + ($2xx3)} { "TOTAL:" $2 million 3} END {print "Total Value of Inventory:" x} 'inventoryhammers QTY: 5 PRICE: 7.99 TOTAL: 39.95drills QTY: 2 PRICE: 29.99 TOTAL: 59.98punches QTY: 7 PRICE: 3.59 TOTAL: 25.13drifts QTY: 2 PRICE: 4.09 TOTAL: 8.18bits QTY: 55 PRICE: 1.19 TOTAL: 65.45saws QTY: 123 PRICE: 14.99 TOTAL: 1843.77nails QTY: 800 PRICE: .19 TOTAL: 152screws QTY: 80 PRICE:. 29 TOTAL: 23.2brads QTY: 100PRICE: .24 TOTAL: 24Total Value of Inventory: 2241.66 $
The BEGIN command works the same way as END, but it establishes projects that need to be done before other work is done. The most common purpose of this process is to create the title of the report. The syntax of this routine is similar to
$awk 'BEGIN {print "ITEM QUANTITY PRICE TOTAL"}'
Input, output, and source files
The AWK tool can read its input from a file, and as all previous examples have done, it can also get input from the output of other commands. For example:
$sort emp_names | awk'{print $3J "2}'
The input to the awk command is the output of the sort operation. In addition to sort, you can use any other Linux command-such as grep. This procedure allows you to perform other actions on the file before leaving the selected field.
Similar to the interpreter, AWK uses the output redirection operators > and > > to put its output in a file instead of a standard output device. These symbols act like their counterparts in the interpreter, so > create a file when no file exists, and append it to the end of the existing file. Look at the following example:
$awk'{print NR, $1) > "/ tmp/filez"} 'emp_names$ cat / tmp/filez1 460122 460133 460154 460175 460186 460197 460218 460229 4602410 4602611 4602712 46029 $
Check the syntax of the statement, and you will see that the output redirection occurs after the print statement is completed. The file name must be enclosed in quotation marks, otherwise it is just an uninitialized AWK variable, and concatenating instructions will result in an error in AWK. (if the redirection symbol is used incorrectly, AWK cannot understand whether the symbol means "redirection" or a relational operator. )
Output to pipes in AWK is similar to the same operation implemented in the interpreter. To send the output of a print command to a pipe, you can append the pipe symbol and the name of the command after the print command, as follows:
$awk'{print $2 | "sort"} 'emp_namesBOGUEBUCKDULANEYDURHAMFELDMANFERGUSJUNEKANESTEENSWIMTUTTLEWOOD$
This is the case where the output is redirected, the command must be enclosed in quotation marks, and the name of the pipe is the name of the command being executed.
The commands used by AWK can come from two places. First, you can specify them on the command line, as shown in the example. Second, they can be provided by the source file. If this is the case, warn AWK of the situation with the-f option. The demonstration is as follows:
$cat awklist {print $3 awklist emp_namesEVAN DULANEYMOBILE ALJEFF DURHAMMOBILE ALBILL STEENMOBILE ALEVAN FELDMANMOBILE ALSTEVE SWIMUNKNOWN ALROBERT BOGUEPHOENIX AZMICAH JUNEPHOENIX AZSHERYL KANEUNKNOWN ARWILLIAM WOODMUNCIE INSARAH FERGUSMUNCIE INSARAH BUCKMUNCIE INBOB TUTTLEMUNCIE IN$ 2} {print $4 camera 5, "n"} $awk-f awklist emp_namesEVAN DULANEYMOBILE ALJEFF DURHAMMOBILE ALBILL STEENMOBILE ALEVAN FELDMANMOBILE ALSTEVE SWIMUNKNOWN ALROBERT BOGUEPHOENIX AZMICAH JUNEPHOENIX AZSHERYL KANEUNKNOWN ARWILLIAM WOODMUNCIE INSARAH FERGUSMUNCIE INSARAH BUCKMUNCIE INBOB TUTTLEMUNCIE IN$
Note that single quotes are not used anywhere in the source file or when it is called on the command line. Single quotation marks are only used to distinguish commands from file names on the command line.
If the simple output cannot handle the complex information needed in your program, you can try the more complex output obtained by the printf command, whose syntax is
Printf (format, value, value...)
The syntax is similar to the printf command in C, but the format is the same. You can define this format by inserting a specification that defines how numeric values are printed. The format specification contains a% followed by a letter. Similar to the print command, printf does not have to be enclosed in parentheses, but it is considered a good habit to use parentheses.
The following table lists the specifications provided by the printf command.
Specification
% c print a single ASCII character
D print decimal numbers
% e Scientific count representation of printed numbers
% f print floating point representation
% g prints% e or% f; both ways are shorter
% o print unsigned octal numbers
S print ASCII string
% x prints unsigned hexadecimal numbers
%% print% sign; no conversion is performed
Some additional formatting parameters can be provided between% and characters. These parameters further improve the way values are printed:
Parameter description
-aligns expressions in the field to the left
, width fills the field to the specified width as needed (leading zero fills the field with zero)
The maximum string width or maximum number of digits to the right of .prec decimal point.
The printf command can control and convert numeric values from one format to another. When you need to print the value of a variable, you only need to provide a specification that instructs printf to print information (usually enclosed in double quotes). You must include a specification parameter for each variable passed to printf; if it contains too few parameters, printf will not print all the values.
Handling error
The way the AWK tool reports errors is exasperating. An error can hinder any operation, and the error message provided is very ambiguous:
Awk: syntax error near line 2awk: bailing out near line 2
You may spend hours looking at line 2 to try to figure out why it prevents the program from running; this is a strong argument for using source files.
Keep in mind that there are two rules that can help you avoid syntax errors:
1. Make sure the command is in parentheses and the parentheses are in single quotation marks. Failure to use one of these characters will inevitably cause the program to fail to run.
two。 The search command needs to be between slashes. To find employees who live in Indiana, you must use "/ IN/" instead of "IN".
The above is all the content of the article "how to add text using awk". 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: 296
*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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.