LoginSignup
12
14

More than 5 years have passed since last update.

awkについて

Posted at

正規表現

#awkの文法
awk 'condition {actions}' file.txt
↓
# $0は全体を表す。
# ~ /pattern/が正規表現
awk '$0 ~ /pattern/ {print $0}' file.txt
↓
# $0は省略可能
awk '/pattern/{print}' file.txt
↓
# printもデフォルトのメソッド
awk '/pattern/' file.txt

(※以下、コマンドの最後に表示したいものを渡す)

偶数行だけ表示

awk 'NR%2'

2列目にsudoが含まれている行だけ取得

awk '$2 == sudo' 

6単語以上含まれているものだけ取得

awk 'NF > 5' 

10行目以降から表示

awk 'NR > 9'

1000と1009が書かれている行まで表示

awk '/1000/,/1009/'

アクセスの多い順に並べ替える

# uniq -cで出現回数をカウントする
# sort -rで降順並べ替え

awk '{print $7}' log | sort | uniq -c | sort -r

histroyコマンドの行数を削除して表示する方法

# historyでコマンド履歴を表示する。
# $1=""で行数を削除し、substrで空白を削除して表示する
history | awk '{$1=''; print substr($0, 2)}'
12
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
12
14