パソコン忘備録
awk
最終更新:
kplus
-
view
awk
基本構文 awk /pattern/ {action} awk /正規表現/ { print "テキスト" }
awk -option /pattern/ {action} $1 ~/[正規表現]/ { print "文章" } NR === 10 { print "文章" } count > 50 { print "文章" } $1 == "string" && $2 ~/[正規表現]/ { print "文章" } NR >= 10 || $NF ~/[正規表現]/ { print "文章" }
基本構文2 awk BIGIN { print "文章" }
基本構文3 awk END { print "文章" }
基本構文4 awk BIGIN { print "文章" } END { print "文章" }
基本構文3 awk END { print "文章" }
基本構文4 awk BIGIN { print "文章" } END { print "文章" }
awk -option /pattern/ {action}
基本構文5 awk 'match($1, "^" var) { print var "文章" }'
action変数
NF 列数 NR 行数
文例
% awk '$1 > 4.0{print}' datafile % awk '$1 <= 5.0 {print $3} $1 > 10.0 {print $2}' datafile % awk `$1 == "S"{print}` datafile % awk '$1 !~ "#"{print}' datafile % awk '{printf("%d %f %s\n",$1,$2,$3)}' datafile % awk 'NF >0 {print $1}' datafile % awk '{if ($1 == "B" || $1 == "N") print "B"; else print "C"}' datafile % awk '{if ($1 !~ "#") if ($1 == 0 && $2 == 0) print $0,0.5,"r"; else print $0,$1/($1+$2); else print "# is escaped."}' datafile % awk 'NR==1{s0=$1 } NR!=1{r=$1-s0; s0=$1; print r}' datafile
[参考]
AWK リファレンス - UNIX & Linux コマンド・シェルスクリプト リファレンス
< http://shellscript.sunone.me/awk.html >
データファイル処理に便利なUNIXコマンド
< http://www.e.ics.nara-wu.ac.jp/~nogu/tips/unix_command.html >
< http://shellscript.sunone.me/awk.html >
データファイル処理に便利なUNIXコマンド
< http://www.e.ics.nara-wu.ac.jp/~nogu/tips/unix_command.html >