由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

awk 语言基础

  1. awk '/^the/' filename # the every line starting with the 'the'
  2. awk '/the$/' filename # the every line ending with the 'the'
  3. awk '/[0-9]/' filename # the every line contain numbers
  4. awk '/[a-z]/' filename # the every line contain the lower capital letters
  5. awk '/hel+0/' filename # helllllo hello
  6. awk '/abc|123/' filename # return for 'abc' or '123'. | --> or
  7. FS = Input field separator value
  8. OFS = Output field separator value
  9. NF = Number of fields on the current line
  10. NR = Number of records in the current file
  11. RS = Record separator value
  12. ORS = Output record separator
  13. FILENAME = Current file name being processed and probably a few more
  14. awk '{print NR}' filename # would print the line number for every line processed
  15. ## = grep -c
  16. awk 'END{print NR}' filename # Counts the lines in a file. similar to 'wc -l'

pipline on awk

  1. awk 'BEGIN{print "the start"};{print}; END{print "the end"}' filename

Simple Logic

  1. awk '{if(NR~/^2#/)print}' filename # would print line 2 from filename
  2. awk '{if(NR~2)print}' filename # would print any line numbers contain 2 from filename
  3. ### 2, 12, 22, 32...
  4. awk '{if(NR!~2)print}' filename # negated match
  5. awk '{if(NR==2)print}' filename
  6. awk '{if(NR!=2)print}' filename

FS

  1. awk '{FS="\t";print $6}' filename
  2. or
  3. awk -F"\t" '{print $6}' filename
  4. awk -F"\t" 'NR==1,NR==10{print $6}' filename #print the cloum 6 from line 1 to line 10;
  5. awk -F"\t" '{print length($5)}' filename # length() function to count the

Delete columns

  1. awk '$1="";{print;OFS=\t}' FILENAME

Enjoy~

本文由Python腳本GitHub/語雀自動更新

由於語法渲染問題而影響閱讀體驗, 請移步博客閱讀~
本文GitPage地址

GitHub: Karobben
Blog:Karobben
BiliBili:史上最不正經的生物狗