awk's usage

awk's usage

awk -F "spearator" '[BEGIN {}] [{}] [END {}]' file1 file2 *.txt

 

example
#---- Sperator -----
00. awk '{print $1}' info.txt
01. awk -F ";" '{print $0}' info.txt
02. awk -F "[,/t]" '{print $2, $3}' info.txt
03. awk -F "[,/t]" '{if( $1 > 100) print $2, $3;}' info.txt

 

#---- BEGIN and END -----
04. awk -F ",/t" 'BEGIN {num = 0;} {num++;} END { printf "total=%s/n", $num }' info.txt
05. awk -F ",/t" 'BEGIN {num = 0;} {print num++;} ' info.txt

 

#---- Regular Expressions -----
05. awk '/abc/ { print $1, $3} *.txt
06. awk '/abc/ { printf "find %s/n", $0}
        /123/ { printf "find %s/n", $1}' *.txt
07. awk '/abc/ || /123/ { print $1, $3} *.txt
08. awk '/abc/ && /123/ { print $1, $3} *.txt
09. awk -F "[,;]" '/abc/ {print hello,$0} ; /123/ {print world,$0}' xxx.txt
10. awk '{ if ($1 ~ /Jack/) print $0 }'         # ~  -> $1 match "Jack"
11. awk '{ if ($1 !~ /Jack/) print $0 }'        # !~ -> $1 not match "Jack"

 

#---- Build Variable -----
NF: the last field in a record
RS: character can be used for the record separator
NR: the total number of input records read so far from all data files
FNR: the number of records that have been read so far from the current input file

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章