Linux command: awk字串處理

来源:百度文库 编辑:神马文学网 时间:2024/05/03 04:58:58

Linux command: awk字串處理

用法: awk '{ print}' 檔案

EX:
1.將/etc/passwd內容逐行輸出

Shell$ awk '{print}' /etc/passwd

執行結果

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
...略...


2.將/etc/passwd每行以":"隔開的第一個字串輸出

Shell$ awk -F":" '{print $1}' /etc/passwd

執行結果

root
daemon
...略...

若-F沒有指定,預設為空白或Tab

3.將/etc目錄ls -l的結果,每行以空白或Tab隔開的第一和第二個字串輸出,並將兩者以":"隔開

Shell$ ls -l /etc awk '{print $1 ":" $2}'

執行結果

drwxr-xr-x:3
drwxr-xr-x:4
-rw-r-----:1
...略...
張貼者: Cloud 位於 3/05/2009 10:38:00 上午 0 意見  from site :  http://nabeko-notebook.blogspot.com/search/label/Linux_Command