Linuxのテキスト処理--awk
2528 ワード
1.grep-->egrep-->テキストフィルタ
gwak(gun awk):数学演算、フロー制御(if-else、サイクル、正則)をサポートし、本質的には言語patten or scanning and processing languageである.
2.awk:テキストカット、モードスキャン、処理の言語.
3.sed:テキストの置換と変更
awkコマンド詳細
完全文法:awk'BEGIN{commands}pattern{commands}END{commanda}'file 1
BEGIN、END-->1回のみ実行
単語定義記号
単語はfengで始まる
feng>-->単語はfengで終わります
-->単語fengの検索
一、
(1)出力/etc/passwdで1,3,4番目のフィールド
(2)区切り記号「#」の指定
(3)指定ファイルの生成
(4)出力パッチ
(5)指定bashを含むフィールドを探し出す
(6)1,7番目のフィールドがhで始まる行でないことを探し出す.
練習:
gwak(gun awk):数学演算、フロー制御(if-else、サイクル、正則)をサポートし、本質的には言語patten or scanning and processing languageである.
2.awk:テキストカット、モードスキャン、処理の言語.
3.sed:テキストの置換と変更
awkコマンド詳細
完全文法:awk'BEGIN{commands}pattern{commands}END{commanda}'file 1
BEGIN、END-->1回のみ実行
awk -F ‘/ /{ }’
awk
awk
、 , / , ;
awk
bash
:+,-,*,/, %,++,- -
:&&, ||, !
:>,=,!=,<=,== ~ !~
awk '$'
FS : ,
OFS : ,
NR :
NF: number of filed $NF --> $NF-1
単語定義記号
単語はfengで始まる
feng>-->単語はfengで終わります
-->単語fengの検索
一、
(1)出力/etc/passwdで1,3,4番目のフィールド
[root@wang lianxi]# awk -F ':' '{print$1 $3 $4}' /etc/passwd
(2)区切り記号「#」の指定
[root@wang lianxi]# awk -F: 'OFS="#"{print $1,$3,$4}' /etc/passwd
# ;
(3)指定ファイルの生成
awk -F: 'OFS="#"{print $1,$3,$4}' /etc/passwd >passwd.csv
(4)出力パッチ
awk -F: 'OFS="#"{print "username:"$1 " uid: " $3 " gid: " $4}' /etc/passwd
username:root uid:0 gid:0
(5)指定bashを含むフィールドを探し出す
[root@wang lianxi]# awk '/bash/' /etc/passwd
root:x:0:0:root:/root:/bin/bash
webadmin:x:1000:1000::/home/webadmin:/bin/bash
(6)1,7番目のフィールドがhで始まる行でないことを探し出す.
[root@wang lianxi]# awk -F: '/^[^h]/{print $1,$7}' /etc/passwd
練習:
1. NF passwd
[root@localhost ~]# cat /etc/passwd|awk -F: '{print $(NF-1)}'
2. passwd 5 10
[root@localhost ~]# cat /etc/passwd|awk 'NR>=5&&NR<=10'
3. passwd 7 bash
cat /etc/passwd|awk -F:'$7 !~ /bash/{print NR,$1}'
4. passwd 5
cat /etc/passwd|awk -F: 'NR ~/5/ {print $0 NR}'
5. ip add ip( tr cut )
[root@wsy lianxi]# ip add|awk -F "[ /]+" '/inet /{print $3}'
6. awk ens33 ( ) --》ifconfig --》 net-tools
[root@localhost ~]# ifconfig| awk 'NR==5||NR==7{print $5}'
7. awk r
[root@wang lianxi]# cat /etc/passwd|awk -F: 'BEGIN{n=0}$1 ~/^r/{n++}END{print n}'