Documents are as follows
# cat Cesca,1a,2b,3b,4c,2d,5
You need to get the number of ABCD occurrences, followed by the numbers and the average.
With Shell:
# grep-e ^a Cesc |awk-f ', ' {sum+=$2} end {print ' A, Count: ' NR ' sum: ' Sum ' Average: ' Sum/nr} 'A, count:2 sum:3 average:1.5# grep-e ^b Cesc |awk-f ', ' {s
Simple examples of Text Analysis Tool awk and text awk
Create a file: vim hi
Take 2nd fields and 3rd fields:
Awk '{print $2, $3}' hi note {}, CommaWill be converted to a space in the output.
Character Description:
Display the entire line:
Field separator:-F
Built-in variable: NF: number of fields. $ NF indicates the last field.
for the demo, print without any parameters, is to print all the contents of the current line, and also represents all the contents of the current line, so, the above two statements are the same, if you want to see the name of the employee who is working 0, then:awk ' $ = = 0 {print $ emp.data} 'You can also print out the line number to make it easy to see how several employees are not working:awk ' $ = = 0 {print NR, ': \ t ', ' $ ' emp.dataIf, you
this a book4. Awk commonly used built-in variable tables:1 $ $ current record (as a single variable)2 $1~ $n the nth field of the current record, separated by FS between fields3 FS input field delimiter default is space4 NF The number of fields in the current record, that is, how many columns5 NR The number of records that have been read, is the line number, starting from 16 RS input Record he thinks of de
Argc, argv
# Awk
ProgramThe available command line parameters are stored in the argv array. Argc is the number of command line parameters.
Argind
# The index of the currently processed file in argv. Each time gawk opens a new data file, it sets argind to the index in argv of the file name. That is to say, when gawk is processing files, 'filename = argv [argind] 'is always true.
Environ
# A joint array used to store environment variables. The dir
"unit for processing the smallest field 』. Well, how does awk know how many rows of data I have? How many columns are there? This requires the help of the built-in variables of awk ~
Variable name meaning
Total number of fields owned by each line of NF ($0)
NR currently awk processes the "row number" Data
The delimite
environment variables in ENVIRON support queuesFileName awk Browse file nameNumber of records FNR browsing filesFS Set input field separator, equivalent to command line-f optionNF browsing the number of fields recordedThe number of records that NR has readOFS Output Field SeparatorORS Output Record SeparatorRS Control Record Separator
Here are some simple things to use:
1, output file second line
space with 15 characters is retained and left aligned.| Linux | /> Echo "Linux" | awk '{printf "| % 15s | \ n", $1 }'# %-15s indicates that the space of 15 characters is retained and the right alignment is also made.| Linux |# % 8d indicates that the number is right aligned and the space of 8 characters is retained. /> Awk '{printf "The name is %-15 s ID is % 8d \ n", $1, $3}' employeesThe name is Tom ID i
first parameter is Variable n assigned initial value 1, and the second parameter provides the file name. Therefore, for each row in test, the condition "n = 1" is true. After reading all the rows in test, calculate the third parameter and assign n to 2. Finally, the fourth parameter provides the second file name. In this case, the condition "n = 2" in the main process is true.
The consequence of using this method to evaluate parameters is that you cannot use the BEGIN process to test or test th
AWK and awk commands
VariableDescription
N the nth field of the current record. The fields are separated by FS.
0Complete input records.ARGCThe number of command line parameters.ARGINDLocation of the current file in the command line (starting from 0 ).ARGVArray containing command line parameters.CONVFMTNumber conversion format (default value: %. 6g)ENVIRONEnvironment Variable join array.ERRNODescript
text. You can also add modifiers and strings # awk 'in in {OFS = ":"} {print $1, "hello ", $2} 'test.txt this: hello: is output three rows, note that \ n # awk 'BEGIN {print "line one \ nline two \ nline three"}' line oneline twoline thr must be added for line feed. Ee 2. awk variable 2.1 awk built-in variable Record
specify an input file, awk accepts the standard input and displays the results on the standard output.
5. awk Script Writing
5.1 Awk's built-in variables
There are two types of built-in variables in awk, a class of users can be changed according to needs, mainly: FS: input data of the field separator, RS: Enter the data of the record delimiter, OFS: Output Data
Tag: the logic is useful for the first execution of 3.3 processing | WHILE LOOPAwk learning and usage
Awk is a text processing language with powerful functions and flexible usage. It can also process operations that cannot be completed by cut. The following is a summary of my usage by combining the information on the network and some examples of my own practices.I. Definitions of common parameters and options
$0 indicates the number of NF fields in th
. The default value is line feed.
Ignorecase Boolean variable. If it is true, case insensitive.
Number of Domain records of NF
Current Nr records
Example 1:
[[Email protected] TMP] # awk 'in in {FS = ","; OFS = ";"; ors = "|"} {print NR, NF, $0, "\ n"} 'tel
1; 3; CLC, 1, 234325;
| 2; 3; clc1, 8, 258353;
| 3; 3; clc3, 3, 234583;
| 4; 3; clc4, 2,345534;
Example 2:
command-line arguments argv command-line parameter arrangement environ support the use of system environment variables in queues filename awk browses the file name Fnr the number of records to browse files FS Set input field delimiter, equivalent to command line-F option NF Browse record number of fields nr
/passwd have root keyword of all lines, and show the corresponding shell
# awk-f: '/root/{print $} '/etc/passwd
/bin/bash
Action{print $} awk built-in variables are specified here
Awk has a number of built-in variables to set up environment information, which can be changed, and some of the most commonly used variables are given below.
ARGC
Number
being processedawk ' {if (argind==1) {print $} if (argind==2) {print $}} ' aaa.txt Bbb.txt # First scan the AAA file and then scan the BBB fileNR number of records that have been readawk ' Nr==1,nr==5{print} ' Aaa.txt # shows 1 to 5 lines of aaa.txt filesFNR the number of records in the current fileawk ' Nr==fnr{print ' a '}
recalculated using OFS.
(2) Data Variables of awk built-in Variables
NR: The number of input records
Number of records processed by the awk command; If there are multiple files, this number will count the rows of the processed files in a unified manner.
NF: Number of Field
Number of fields in the current record
FNR
Number of
arguments argv command-line parameter arrangement environ support the use of system environment variables in queues filename awk browses the file name Fnr the number of records to browse files FS Set input field delimiter, equivalent to command line-F option NF Browse record number of fields nr Read records of
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.