Linux下的awk用法详解
生活随笔
收集整理的這篇文章主要介紹了
Linux下的awk用法详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Linux下的awk用法詳解
- 一、awk介紹
- 二、awk的語法
- 三、awk常見用法
- 四、awk其他用法
- 五、awk語言特性
一、awk介紹
1.AWK 是一種處理文本文件的語言,是一個強大的文本分析工具。之所以叫 AWK 是因為其取了三位創始人 Alfred Aho,Peter Weinberger, 和 Brian Kernighan 的 Family Name 的首字符。
2.AWK擁有自己的語言: AWK 程序設計語言,它允許您創建簡短的程序,這些程序讀取輸入文件、為數據排序、處理數據、對輸入執行計算以及生成報表,還有無數其他的功能。
3.
二、awk的語法
1.RHEL8.0系統上的awk幫助
[root@control scripts]# awk --help Usage: awk [POSIX or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or GNU style options] [--] 'program' file ... POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-D[file] --debug[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-i includefile --include=includefile-l library --load=library-L[fatal|invalid] --lint[=fatal|invalid]-M --bignum-N --use-lc-numeric-n --non-decimal-data-o[file] --pretty-print[=file]-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-s --no-optimize-S --sandbox-t --lint-old-V --version2.awk選項解釋
-F fs or --field-separator fs 指定輸入文件折分隔符,fs是一個字符串或者是一個正則表達式,如-F:。 -v var=value or --asign var=value 賦值一個用戶定義變量。 -f scripfile or --file scriptfile 從腳本文件中讀取awk命令。 -mf nnn and -mr nnn 對nnn值設置內在限制,-mf選項限制分配給nnn的最大塊數目;-mr選項限制記錄的最大數目。這兩個功能是Bell實驗室版awk的擴展功能,在標準awk中不適用。 -W compact or --compat, -W traditional or --traditional 在兼容模式下運行awk。所以gawk的行為和標準的awk完全一樣,所有的awk擴展都被忽略。 -W copyleft or --copyleft, -W copyright or --copyright 打印簡短的版權信息。 -W help or --help, -W usage or --usage 打印全部awk選項和每個選項的簡短說明。 -W lint or --lint 打印不能向傳統unix平臺移植的結構的警告。 -W lint-old or --lint-old 打印關于不能向傳統unix平臺移植的結構的警告。 -W posix 打開兼容模式。但有以下限制,不識別:/x、函數關鍵字、func、換碼序列以及當fs是一個空格時,將新行作為一個域分隔符;操作符**和**=不能代替^和^=;fflush無效。 -W re-interval or --re-inerval 允許間隔正則表達式的使用,參考(grep中的Posix字符類),如括號表達式[[:alpha:]]。 -W source program-text or --source program-text 使用program-text作為源代碼,可與-f命令混用。 -W version or --version 打印bug報告信息的版本。三、awk常見用法
1.文本字段處理
[root@control scripts]# awk '{print $0}' /etc/passwd |head root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin daemon:x:2:2:daemon:/sbin:/sbin/nologin adm:x:3:4:adm:/var/adm:/sbin/nologin lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt mail:x:8:12:mail:/var/spool/mail:/sbin/nologin operator:x:11:0:operator:/root:/sbin/nologin2.自定義字符段替換原文本的行輸出
[root@control scripts]# awk '{print "this is a test line"}' /etc/fstab this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line this is a test line3.指定文本的列輸出
[root@control scripts]# awk '{print $1}' /etc/fstab # # # # # # # # # # UUID=11568b58-6451-40da-a59f-7da0c2536cd3 UUID=59347c6a-20b5-4bc7-8b74-caa2980c6832 UUID=a94ebb71-f259-4589-9ad9-6ae3f495bc63 UUID=2f261207-03e5-47d7-ac83-9a4424fb6f744.指定文本的列輸出,且列之間添加空格顯示
[root@control scripts]# awk '{print $1 "\t " $3}' /etc/fstab UUID=11568b58-6451-40da-a59f-7da0c2536cd3 xfs UUID=59347c6a-20b5-4bc7-8b74-caa2980c6832 xfs UUID=a94ebb71-f259-4589-9ad9-6ae3f495bc63 xfs UUID=2f261207-03e5-47d7-ac83-9a4424fb6f74 swap四、awk其他用法
1.指定格式輸出文本
[root@control scripts]# [root@control scripts]# awk -F":" '{print "USER: " $1 "\tSHELL " $7}' /etc/passwd |head USER: root SHELL /bin/bash USER: bin SHELL /sbin/nologin USER: daemon SHELL /sbin/nologin USER: adm SHELL /sbin/nologin USER: lp SHELL /sbin/nologin USER: sync SHELL /bin/sync USER: shutdown SHELL /sbin/shutdown USER: halt SHELL /sbin/halt USER: mail SHELL /sbin/nologin USER: operator SHELL /sbin/nologin2.設置多個字符為分隔符
[root@control scripts]# awk -F "[\t ]+" '{print "DEVICE: " $1 "\tFSTYPE: " $3}' /etc/fstab DEVICE: UUID=11568b58-6451-40da-a59f-7da0c2536cd3 FSTYPE: xfs DEVICE: UUID=59347c6a-20b5-4bc7-8b74-caa2980c6832 FSTYPE: xfs DEVICE: UUID=a94ebb71-f259-4589-9ad9-6ae3f495bc63 FSTYPE: xfs DEVICE: UUID=2f261207-03e5-47d7-ac83-9a4424fb6f74 FSTYPE: swap DEVICE: FSTYPE:五、awk語言特性
awk中的if語句
[root@control ~]# cat numtest.txt 20 low 109 high 29 low 290 high 111 high [root@control ~]# awk '{if ($1>100) print $1 " bad!"; else print "OK!"}' /root/numtest.txt OK! 109 bad! OK! 290 bad! 111 bad! OK!備注:awk作為一門語言,支持語言的三種基本元素:順序、判斷、循環。所以awk可以支持if語句while/for循環,break和continue。
總結
以上是生活随笔為你收集整理的Linux下的awk用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux脚本之定时清空文件内容
- 下一篇: Zabbix的客户端安装教程