Linkage effects modify the field values or NF
Note that the following is calculated and divided two words: segmentation indication FS (field Separator), calculates a variable using the predefined OFS (Output Field Separator).
- Modification
$0
, will be usedFS
to re-split field, it will affect$1、$2...
- Modification
$1、$2
, according$1
to the$NF
re-calculation of the fields$0
- Even
$1 = $1
so constant modification of the original value, the same will be recalculated$0
- Even
- Assignment for the field does not exist, add fields and used as needed to fill the empty string in the middle of the field, and used
OFS
to recalculate$0
awk 'BEGIN{OFS="-"}{$(NF+2)=5;print $0}' a.txt
- Increase the value of NF, will add an empty string field and use
OFS
recalculate$0
awk 'BEGIN{OFS="-"}{NF+=3;print $0}' a.txt
- NF reduced value, a certain number of discarded tail field, and used
OFS
to recalculate$0
awk 'BEGIN{OFS="-"}{NF-=3;print $0}' a.txt
About $ 0
When reading a record, to be stored exactly to $0
them.
awk '{print $0}' a.txt
But as long as there has been any kind of operation leads to the above mentioned $ 0 recalculated, OFS will be used immediately to rebuild $ 0
In other words, it did not lead to $0
reconstruction, $0
it has been exactly the data, specify the OFS also invalid.
awk ' the BEGIN {the OFS = "-"} {0} Print $ ' a.txt # invalid here the OFS
When $0
the reconstruction, the reconstruction will automatically use the OFS, OFS even if not specified, it will default value (space) for reconstruction.
[@ docker- the root 01 ~] # awk ' {+ = $ 10. 4; the OFS = "-"; Print $ 0} ' a.txt # inactive for the first row the OFS [@ Docker the root - 01 ~] # awk ' {$. 4 = 10 +; the OFS = "-"; $ = $. 1. 1; Print $ 0} ' a.txt # is valid for all rows
Focus on $0
reconstruction is a very useful skill.
For example, by the following reconstruction $0
to achieve the removal of line trailing spaces and compression techniques middle space:
[root@docker-01 ~]# echo " a b c d " | awk '{$1=$1;print}' a b c d [root@docker-01 ~]# echo " a b c d " | awk '{$1=$1;print}' OFS="-" a-b-c-d