Bash awk 基本入门

Bash awk 基本入门

Awk patten {action} file

Akw command is used to handle fields in one line.

Analize every line in the file, and if the line match the patten, then do the action.

pattern field is optional.

$0 indicates the whole line.

The first field is $1.

[braveyly@m-net ~]$ cat awk.txt

liwei  27  male     hust

lijing 24  femaile  cnnu

hujuan 22  female   cnnu

hurui  18  male     hust

[braveyly@m-net ~]$ awk '$2 > 23 {print $1, $3, $4}' ./awk.txt

liwei male hust

lijing femaile cnnu

[braveyly@m-net ~]$ awk '$4 == "hust" {print $0}' ./awk.txt

liwei  27  male     hust

hurui  18  male     hust

[braveyly@m-net ~]$ awk '$4 ="hust" {print $0}' ./awk.txt

liwei 27 male hust

lijing 24 femaile hust

hujuan 22 female hust

hurui 18 male hust


awk -Fchar '{action}'

Seperate the line into different fileds by single char.

No blank is between -F and char.

[nick@d01 bash]$ cat sort4.txt
daemon:x:2:2:daemon:/sbin:/sbin/nologin
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin

 

[nick@d01 bash]$ awk -F: '{print $7}' ./sort4.txt
/sbin/nologin
/bin/bash
/sbin/nologin

 

-F argument indicates the separator of fields.

 

你可能感兴趣的:(Bash awk 基本入门)