Linux-shell script command-awk

Source: Internet
Author: User

[Awk Introduction:]

Awk can obtain part of the content from a text, or typeset the text so that it can be output in a certain format.

[Awk workflow:]

Awk will upload a row of file content to the memory and segment the content in this line (separated by spaces or tabs by default, $1, $2, $3 ...). and then read the second line to the memory... format: awk '{/pattern/command1; command2 ;...} 'File # execute the command in the matching mode, for example, who | awk '{print $1}' # print the first part of the segment, $1 is the first part, $0 indicates all content in a row.

[Awk parameter description:]

-F re: Allows awk to change its field separator. -V defines variables and transmits variables from shell to awk, such as-vDATE = $ DATE, that is, the $ DATE variable value in shell is passed to the awk variable DATE.
-F progfile: allows the awk to call and execute the progfile program file. Of course, the progfile must be a program file that complies with the awk syntax.

[Awk built-in variables:]

Number of ARGC command line parameters
ARGV command line parameter Array
Argind argv flag of the currently processed file awk '{if (ARGIND = 1) {print $1} if (ARGIND = 2) {print $2} 'aaa.txt bbb.txt # first scans the aaa file and then the bbb File
Number of records read by NR awk 'nr = 1, NR = 5 {print} 'aaa.txt # displays 1 to 5 rows of aaa.txt files
FNR current file records awk 'nr = FNR {print "a"} NR> FNR {print "B"} 'a.txt B .txt # input file a.txt and B .txt, when scanning a.txt, there must be NR = FNR; # After scanning B .txt, FNR starts to count from 1, while NRIS connected to the number of a.txt rows, so NR> FNR
FS input field separator (default: space :), equivalent to-F option awk-F': ''{print $1} 'ccc.txt # input file: As the delimiter
OFS output field separator (default: space :) # when outputting; split ① cat ccc.txt as follows:

② Awk-F': ''BEGIN {OFS ="; "} {print $1, $2, $3} 'ccc.txt
③ The output result is as follows:
1; 2; 3
4; 5; 6
Number of parts in the current NF record awk-F': ''{print NF} 'ccc.txt
The RS input record delimiter. The default value is "\ n". By default, awk regards a row as a record. If RS is set, awk splits the record according to RS ① cat ccc.txt: hello world; I want to go mongoming tomorrow; hiahia
② Run awk 'in in {RS = ";"} {print} 'ccc.txt
③ The result is as follows: hello world
I want to go mongoming tomorrow
Hiahia
ORS output record delimiter, which is a line break by default and controls the output symbols after each print statement
Awk 'in in {FS = "\ n"; RS = ""; ORS = ";"} {print NF} 'ddd.txt

[Awk built-in functions:]

Blength [([s])] calculates the string length in bytes)
Length [([s])] calculates the string length (character)
Rand () to generate a random number
Srand ([expr]) set rand () seed
Converts an int (x) string to an integer.
Substr (s, m [, n ])
Index (s, t) locates the first occurrence position of the t string in string s.
Match (s, ere) matches regular ere in string s. match modifies the RSTART and RLENGTH variables.
Split (s, a [, fs]) Splits strings into arrays.
Sub (ere, repl [, in]) string replacement
Gsub is the same as above
Sprintf (fmt, expr,...) spell string
System (cmd): Execute cmd in shell.
Toupper (s) string converted to uppercase
Tolower (s) string to lowercase

[Awk usage example:]

1. -F indicates what is used as the separator awk-F: '{print $1}' ccc.txt # With: As the separator, print the first line of the string awk-F in the ccc.txt file: '{print $1, $2}' ccc.txt # print the first and second strings. If you write $1 $2 in this way, the printed content will be connected together.
2./pattern match awk-F: '/A/{print $1}' ccc.txt # Use: As the separator to print the first string containing A in the ccc.txt File
3. ^ indicates the start of awk-F: '/^ A/{print $1}' ccc.txt # Use: As the separator to print the first string starting with A in the ccc.txt File
4 .~ Awk-F: '$4 ~ /A/{print $1} 'ccc.txt # Use: As the separator to print the string containing A in the fourth section of the ccc.txt File
5. before reading the first line into the memory, awk can perform other operations in the BEGIN format: awk 'in in {command}/pattern/{command1; command2 ;...} 'File
Awk 'in in {FS = ":"} $1 ~ /A/{print $1} 'ccc.txt # before reading the ccc.txt file, first run the command in BEGIN and set: As the delimiter awk 'begin {FS = ":"; OFS = "-"} $1 ~ /A/{print $1, $2} 'ccc.txt # Separate the output Separators-
6. after all the rows are processed, you can perform some operations first, using the END format: awk 'in in {command}/pattern/{command1; command2 ;...} END {command} 'file
7. Use NF who | awk '{print NF}' to count the number of segments after each row }'
8. Get awk to process the file content. In the source file, use NR awk-F: '/^ A/{print NR}' ccc.txt # To obtain the number of lines starting with A in ccc.txt.
9. custom variable awk 'in in {a = 0}/tb/{a ++} END {print a} 'ccc.txt # define a variable before circulating the ccc.txt file. The initial value is 0; then, loop through each row, and Add 1 to a for each row that contains tb until the value of a is printed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.