sprintfを使おう
使用するテキスト
[root@centos8_1 ~]# cat in.txt
ososo 100
mitomito 89
motomoto 84
subesube 78
baibai 82
awkのsprintfを使うとこんな感じになる
[root@centos8_1 ~]# cat in.txt | awk '{str = sprintf("NAME:%s GRANT:%d", $1, $2); print str }'
NAME:ososo GRANT:100
NAME:mitomito GRANT:89
NAME:motomoto GRANT:84
NAME:subesube GRANT:78
NAME:baibai GRANT:82
配列を使おう
awkの配列は[1]
※[0]ではありません。プログラマの皆様
cat /etc/passwd | awk '{ z = split($1,fullname,":"); print fullname[1]}'
NRを使おう
該当行を取得することもできる
最終行を取得することもできる
cat /etc/passwd | awk '{ maxRow = NR } END {print maxRow}'
こっちのほうがいいけど
cat /etc/passwd | wc -l