# 【Shell】awk 取出特定欄位

- AWK 要取得最後一個欄位的值是使用： **$NF**
- AWK 要取得倒數第二個欄位的值是使用： **$(NF-1)**
- 由後往前取，就繼續 -2、-3... 下去即可
- 範例：**awk '{print $(NF - 2)}'** # 從後面數來第二個

所以要抓 access.log 倒數第二欄 和 第二欄，範例如下：

- cat access.log | awk -F\\" '{print $(NF-2),$2}' # 使用 " 切割

想要過濾掉其他欄位，主要就是把不要的欄位設定為「空」，再來印出 $0 就可以了~

- $ cat access.log | awk '{$1=$2=$8=""; print $0}'

cut 也可以達成：

- $ cat access.log | cut --complement -d" " -f 1,2,8

- [使用 AWK 過濾不要的欄位 – Tsung's Blog (longwin.com.tw)](https://blog.longwin.com.tw/2019/04/linux-awk-filter-column-2019/)
- [AWK 取得欄位的「最後」與「倒數第二個值」的方法 – Tsung's Blog (longwin.com.tw)](https://blog.longwin.com.tw/2018/10/awk-get-nf-last-next-value-2018/)