bash shell awk

awk 资料:

        wiki:https://en.wikipedia.org/wiki/AWK

        函数:Built-in (The GNU Awk User’s Guide)

awk 用法:

        查看ubuntu 20.04 ,实际使用为gawk 5.1.0

jagger@2004:~/working/bash$ which awk
/usr/bin/awk
jagger@2004:~/working/bash$ file /usr/bin/awk 
/usr/bin/awk: symbolic link to /etc/alternatives/awk
jagger@2004:~/working/bash$ file /etc/alternatives/awk
/etc/alternatives/awk: symbolic link to /usr/bin/gawk
jagger@2004:~/working/bash$ awk --version
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)
Copyright (C) 1989, 1991-2020 Free Software Foundation.

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

awk 格式:

        test.txt 取用/etc/passwd 前7行,另加一空行

jagger@2004:~/working/bash$ cat test.txt |awk -F ":" '{print $0}'
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin

jagger@2004:~/working/bash$ echo 'hello world !' |awk '{print $0} {print $1} {print $2} {print $3}'
hello world !
hello
world
!

jagger@2004:~/working/bash$ echo 'hello world !' |awk '{print $0"|"$1"|"$2"|"$3}'
hello world !|hello|world|!

        awk将文件以空格符、制表符分割为行,基本格'式为' '里一对{ },大括号内为每一行要处理的动作。print为打印,$num为要打印的行

        echo 'hello world !' 的结果作为 awk 的输入,也就是第一个参数$0

        $1 $2 $3 为 awk 按空格符依次分割的行

jagger@2004:~/working/bash$ cat test.txt |awk -F ":" '{print $1}'
root
daemon
bin
sys
sync
games
man

        上面的代码是,-F ":"以 :  作为分割符,打印$1行

awk 内置变量:

  • NR: Number of Records. Keeps a current count of the number of input records read so far from all data files. It starts at zero, but is never automatically reset to zero.[14]
  • FNR: File Number of Records. Keeps a current count of the number of input records read so far in the current file. This variable is automatically reset to zero each time a new file is started.Built-in (The GNU Awk User’s Guide)
  • NF: Number of Fields. Contains the number of fields in the current input record. The last field in the input record can be designated by $NF, the 2nd-to-last field by $(NF-1), the 3rd-to-last field by $(NF-2), etc.
  • FILENAME: Contains the name of the current input-file.
  • FS: Field Separator. Contains the "field separator" used to divide fields in the input record. The default, "white space", allows any sequence of space and tab characters. FS can be reassigned with another character or character sequence to change the field separator.
  • RS: Record Separator. Stores the current "record separator" character. Since, by default, an input line is the input record, the default record separator character is a "newline".
  • OFS: Output Field Separator. Stores the "output field separator", which separates the fields when Awk prints them. The default is a "space" character.
  • ORS: Output Record Separator. Stores the "output record separator", which separates the output records when Awk prints them. The default is a "newline" character.
  • OFMT: Output Format. Stores the format for numeric output. The default format is "%.6g".
jagger@2004:~/working/bash$ cat test.txt |awk -F ":" '{print $NF}'
/bin/bash
/usr/sbin/nologin
/usr/sbin/nologin
/usr/sbin/nologin
/bin/sync
/usr/sbin/nologin
/usr/sbin/nologin

jagger@2004:~/working/bash$ cat test.txt |awk -F ":" '{print NR") "$1}'
1) root
2) daemon
3) bin
4) sys
5) sync
6) games
7) man
8) 

        上面的代码,是加入了字符串的拼接

jagger@2004:~/working/bash$ awk -F ":" '{print "filename: "FILENAME " row: "NR " column: " NF" " $1}' test.txt 
filename: test.txt row: 1 column: 7 root
filename: test.txt row: 2 column: 7 daemon
filename: test.txt row: 3 column: 7 bin
filename: test.txt row: 4 column: 7 sys
filename: test.txt row: 5 column: 7 sync
filename: test.txt row: 6 column: 7 games
filename: test.txt row: 7 column: 7 man
filename: test.txt row: 8 column: 0 

awk 正则:

jagger@2004:~/working/bash$ cat test.txt |awk '/games/{print $0}'
games:x:5:60:games:/usr/games:/usr/sbin/nologin
jagger@2004:~/working/bash$ cat test.txt |awk '/^s/{print $0}'
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync
jagger@2004:~/working/bash$ cat test.txt |awk '/in$/{print $0}'
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
games:x:5:60:games:/usr/games:/usr/sbin/nologin
man:x:6:12:man:/var/cache/man:/usr/sbin/nologin
jagger@2004:~/working/bash$ cat test.txt |awk '/^$/{print $0}'

         / /:内为检索内容

        ^c :以c为开头的行

        c%:以c为结尾的行

        ^$:空行

awk 函数:Built-in (The GNU Awk User’s Guide)

jagger@2004:~/working/bash$ cat test.txt |awk '/in$/{print toupper($0)}'
DAEMON:X:1:1:DAEMON:/USR/SBIN:/USR/SBIN/NOLOGIN
BIN:X:2:2:BIN:/BIN:/USR/SBIN/NOLOGIN
SYS:X:3:3:SYS:/DEV:/USR/SBIN/NOLOGIN
GAMES:X:5:60:GAMES:/USR/GAMES:/USR/SBIN/NOLOGIN
MAN:X:6:12:MAN:/VAR/CACHE/MAN:/USR/SBIN/NOLOGIN

        tolower():字符转为小写

        length():返回字符串长度

        substr():返回子字符串

        sin():正弦

        cos():余弦

        sqrt():平方根

        rand():随机数

awk 条件:

jagger@2004:~/working/bash$ cat test.txt |awk -F ":" '$1 == "games" || $1 == "root" {print $1}'
root
games

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值