awk, I think it's the most subtle command in Linux that handles text, and it's a line-processing command that uses the initial level: given some simple pattern, and then follow this pattern to search for matching rows. Its advanced usage is to program with AWK, in addition to the original matching string this function, but also can do some statistics, replacement, mathematical calculation functions, and even can write custom functions, it is very magical.
1. AWK Primary usage
awk ' pattern ' filename
awk ' {action} ' filename
awk ' Pattern {action} ' filename//finds the line with the pattern and performs some actions. There is no "," between pattern and {, which can be extended to awk ' pattern {action}; pattern2 {action2} ' filename
Nr:number of records, the first few lines
Nf:number of field, row with several fields
Ofs:output field Seperator, which represents the delimiter between output fields, the default is a space. If you do not add this parameter, for example, in the printing of $ and $, separated by a space, if this parameter is added, the parameter is separated, see example
Say a.txt.
Duplex_mode, duplexing mode, 0
Cfi_power_offset, CFI power offset, 5000
P_b, refers to downlink power allocation, 0
Dl_cp_type, Downlik cyclic prefix type, 0
Ul_cp_type, uplink cyclic prefix type, 0
DL_CH_BW, downlink channel bandwidth in resource blocks, 50
UL_CH_BW, uplink channel bandwidth in resource blocks, 50
Rs_power, Reference signal power, 0
>>awk-f ', ' Print $ ' a.txt//With ', ' delimiter, print first field, note ', ' and ', ' not the same
>>awk-f ', '/cfi/' a.txt//Find the line with CFI and print it out
>> awk-f ', ' $3>10 {print $ ' a.txt//Find a third domain greater than 10, and then print the first field
>> awk-f ', ' $ ~/^d._. _/' a.txt//"~" is used to match a regular expression within a record or field, beginning with D, and the third character is the line of _
>> awk-f ', ' {OFS = '? '}; /cfi/{print $1,$2} ' a.txt//result is Cfi_power_offset? CFI Power Offset
A little bit more advanced:
A. Multiple execution actions: awk ' pattern {action statement; Action statement; etc} '
>>awk-f ', '/cfi/{$3+=8;print $ ' a.txt//Add 8 First, then print out
B. The scope template matches the first occurrence from the first template to the second one, the next occurrence of the first template to the next occurrence of the first template, and so on. If the first template matches and the second template does not appear, awk displays all rows at the end of the file.
>> awk-f ', '/^dl_cp_type/,/^ul_ch_bw/{print $ ' a.txt//find rows between Dl_cp_type and UL_CH_BW and print the first domain
Reference: http://www.cnblogs.com/mchina/archive/2012/06/30/2571308.html
2. AWK Advanced Usage
I use a practical example to analyze the advanced usage of awk
Awk-f "," '/@ULS | @UCI | @DC | @DLS |@r|@s/{X=$1}; /^rnti, | preamble_index/{x=x "," $3;arr[x]++};/ack_nack_mode/{cnt=arr[x];d elete arr[x];x=x "," $3;arr[x]=cnt}; END {for (i in arr) print I "," arr[i]} ' a.txt > B.txt
A.txt:
@ULSCH_PDU
Handle, handle returned in RX indication, 1
Pdu_size, size of Ulsch PDU in bytes, 4
Rnti, Rnti for current PDU, 80
Rb_start, the starting resource block, 0
Ack_nack_mode, ACK non ack mode, 3
@ULSCH_PDU
Handle, handle returned in RX indication, 1
Pdu_size, size of Ulsch PDU in bytes, 4
Rnti, Rnti for current PDU, 80
Rb_start, the starting resource block, 0
Ack_nack_mode, ACK non ack mode, 1
@DCI_DL_PDU_Fmt1
Dci_format, DCI Format (0=1/1=1A/2=1B/3=1C/4=1D/5=2/6=2A), 0
Cce_idx, CCE index, 0
Aggreg_level, Aggregation level, 2
Rnti, Rnti, 60
@DLSCH_PDU
Pdu_length, MAC PDU length in bytes, 109
Rnti, Rnti for current PDU, 60
Resource_alloc_type, Resource allocation type, 0
Virtual_rb_flag, Type of virtual resource block, 0
Rb_coding, Resource block coding, 130560
MCS, modulation and coding scheme, 2
@DCI_DL_PDU_Fmt1
Dci_format, DCI Format (0=1/1=1A/2=1B/3=1C/4=1D/5=2/6=2A), 0
Cce_idx, CCE index, 0
Aggreg_level, Aggregation level, 2
Rnti, Rnti, 60
@DLSCH_PDU
Pdu_length, MAC PDU length in bytes, 109
Rnti, Rnti for current PDU, 60
Resource_alloc_type, Resource allocation type, 0
Virtual_rb_flag, Type of virtual resource block, 0
Rb_coding, Resource block coding, 130560
MCS, modulation and coding scheme, 2
The result B.txt is
@DLSCH_PDU, 60, 2
@DCI_DL_PDU_Fmt1, 60, 2
@ULSCH_PDU, 80, 3, 1
@ULSCH_PDU, 80, 1, 1
Reference: http://www.cnblogs.com/mchina/archive/2012/06/30/2571317.html
Some uses of awk