由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
awk 语言基础
awk '/^the/' filename # the every line starting with the 'the'awk '/the$/' filename # the every line ending with the 'the'awk '/[0-9]/' filename # the every line contain numbersawk '/[a-z]/' filename # the every line contain the lower capital lettersawk '/hel+0/' filename # helllllo helloawk '/abc|123/' filename # return for 'abc' or '123'. | --> orFS = Input field separator valueOFS = Output field separator valueNF = Number of fields on the current lineNR = Number of records in the current fileRS = Record separator valueORS = Output record separatorFILENAME = Current file name being processed and probably a few moreawk '{print NR}' filename # would print the line number for every line processed## = grep -cawk 'END{print NR}' filename # Counts the lines in a file. similar to 'wc -l'
pipline on awk
awk 'BEGIN{print "the start"};{print}; END{print "the end"}' filename
Simple Logic
awk '{if(NR~/^2#/)print}' filename # would print line 2 from filenameawk '{if(NR~2)print}' filename # would print any line numbers contain 2 from filename### 2, 12, 22, 32...awk '{if(NR!~2)print}' filename # negated matchawk '{if(NR==2)print}' filenameawk '{if(NR!=2)print}' filename
FS
awk '{FS="\t";print $6}' filenameorawk -F"\t" '{print $6}' filenameawk -F"\t" 'NR==1,NR==10{print $6}' filename #print the cloum 6 from line 1 to line 10;awk -F"\t" '{print length($5)}' filename # length() function to count the
Delete columns
awk '$1="";{print;OFS=\t}' FILENAME
Enjoy~
由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址
GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗
