awk命令擴展使用操做

awk 中使用外部shell變量

示例1html

[root@centos01 t1022]# A=888
[root@centos01 t1022]# echo "" | awk -v GET_A=$A '{print GET_A}'
888
[root@centos01 t1022]# echo "aaaaaaaaaaaaa" |  awk -v GET_A=$A '{print GET_A}'
888

說明:-v選項用於定義參數,這裏表示將變量A的值賦予GET_A。有多少個變量須要賦值,就須要多少個-v選項。前面的echo "string"是awk運行須要的參數linux

示例2redis

[root@centos01 t1022]# cat test.txt
1111111:13443253456
2222222:13211222122
1111111:13643543544
3333333:12341243123
2222222:12123123123
[root@centos01 t1022]# cat 1.sh
#!/bin/bash
sort -n test.txt | awk -F ':' '{print $1}' | uniq > t.txt
for id in `cat t.txt`;do
    echo "[$id]"
    awk -v get_id=$id -F ':' '$1==get_id {print $2}' test.txt
    # 或者 awk -F ':' '$1=="'$id'" {print $2}' test.txt
done

[root@centos01 t1022]# bash 1.sh
[1111111]
13443253456
13643543544
[2222222]
13211222122
12123123123
[3333333]
12341243123

awk 合併文件

[root@centos01 t1022]# cat p1.txt
1 aa
2 bb
3 ee
4 ss
[root@centos01 t1022]# cat p2.txt
1 ab
2 cd
3 ad
4 bd
5 de
[root@centos01 t1022]# awk 'NR==FNR{a[$1]=$2}NR>FNR{print $0,a[$1]}' p1.txt p2.txt
1 ab aa
2 cd bb
3 ad ee
4 bd ss
5 de

說明: NR表示讀取的行數, FNR表示讀取的當前行數。 因此NR==FNR 就表示讀取p1.txt的時候。 同理NR>FNR表示讀取p2.txt的時候shell

把一個文件多行鏈接成一行

[root@centos01 t1022]# cat p1.txt
1
2
3
4
[root@centos01 t1022]# f=`cat p1.txt`;echo $f
1 2 3 4
[root@centos01 t1022]# awk '{printf("%s",$0)}' p1.txt 
1 2 3 4  [root@centos01 t1022]#   # 打印後沒有換行,交互不是特別好,加echo處理
[root@centos01 t1022]# awk '{printf("%s",$0)}' p1.txt;echo
1 2 3 4
[root@centos01 t1022]# paste -s -d '' p1.txt
1 2 3 4
[root@centos01 t1022]# cat p1.txt |xargs
1 2 3 4

[root@centos01 t1022]# cat p1.txt|xargs|sed 's/ /+/g'
1+2+3+4

擴展:centos

  • gdb安裝 yum install -y gdb
  • gdb當計算器使用
[root@centos01 t1022]# gdb
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-110.el7
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) p 3+2
$1 = 5
(gdb) p 128+12435
$2 = 12563

awk中gsub函數的使用

# 把test01.txt文件中的全部root替換爲ABC打印出來
[root@centos01 t1022]# cat test01.txt
0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin
[root@centos01 t1022]#
[root@centos01 t1022]# awk 'gsub(/root/, "ABC")' test01.txt
0:0:ABC:/ABC:/bin/bash
operator:x:11:0:operator:/ABC:/sbin/nologin

# 替換每行第一次出現的root爲ABC
[root@centos01 t1022]# awk 'sub(/root/, "ABC")' test01.txt
0:0:ABC:/root:/bin/bash
operator:x:11:0:operator:/ABC:/sbin/nologin

# 替換$3中的root爲ABC打印出來
[root@centos01 t1022]# awk -F ':' 'gsub(/root/, "ABC", $3) {print $0}' test01.txt
0 0 ABC /root /bin/bash

awk 截取指定多個域爲一行

[root@centos01 t1022]# cat test01.txt
0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@centos01 t1022]# cat 2.sh
for i in `seq 1 10`
do
    awk -F ':' -v a=$i '{$a;printf $a ""}' test01.txt
    echo
done

[root@centos01 t1022]#  bash 2.sh
0bindaemonadmlpsyncshutdownhaltmailoperator
0xxxxxxxxx
root1234567811
/root1247000120
/bin/bashbindaemonadmlpsyncshutdownhaltmailoperator
/bin/sbin/var/adm/var/spool/lpd/sbin/sbin/sbin/var/spool/mail/root
/sbin/nologin/sbin/nologin/sbin/nologin/sbin/nologin/bin/sync/sbin/shutdown/sbin/halt/sbin/nologin/sbin/nologin

grep 或 egrep 或awk 過濾兩個或多個關鍵詞

  • grep -E '123|abc' filename # 找出文件(filename)中包含123或者包含abc的行
  • egrep '123|abc' filename # 用egrep一樣能夠實現
  • awk '/123|abc/' filename # awk 的實現方式
[root@centos01 t1022]# cat test01.txt
0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@centos01 t1022]# grep -E 'x:|nologin' test01.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@centos01 t1022]# egrep 'x:|nologin' test01.txt 
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

[root@centos01 t1022]# awk '/x:|abc/' test01.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
operator:x:11:0:operator:/root:/sbin/nologin

awk編寫生成如下結構文件

用awk編寫生成如下結構文件的程序。( 最後列使用如今的時間,時間格式爲YYYYMMDDHHMISS) 各列的值應以下所示,每增長一行便加1,共500萬行。
1,1,0000000001,0000000001,0000000001,0000000001,0000000001,0000000001,2005100110101 2,2,0000000002,0000000002,0000000002,0000000002,0000000002,0000000002,2005100110101bash

[root@centos01 t1022]# awk 'BEGIN{for(i=1;i<=10;i++)printf("%d,%d,%010d,%010d,%010d,%010d,%010d,%010d,%d\n",i,i,i,i,i,i,i,i,strftime("%Y%m%d%H%M%S"))}'
1,1,0000000001,0000000001,0000000001,0000000001,0000000001,0000000001,20181023070344
2,2,0000000002,0000000002,0000000002,0000000002,0000000002,0000000002,20181023070344
3,3,0000000003,0000000003,0000000003,0000000003,0000000003,0000000003,20181023070344
4,4,0000000004,0000000004,0000000004,0000000004,0000000004,0000000004,20181023070344
5,5,0000000005,0000000005,0000000005,0000000005,0000000005,0000000005,20181023070344
6,6,0000000006,0000000006,0000000006,0000000006,0000000006,0000000006,20181023070344
7,7,0000000007,0000000007,0000000007,0000000007,0000000007,0000000007,20181023070344
8,8,0000000008,0000000008,0000000008,0000000008,0000000008,0000000008,20181023070344
9,9,0000000009,0000000009,0000000009,0000000009,0000000009,0000000009,20181023070344
10,10,0000000010,0000000010,0000000010,0000000010,0000000010,0000000010,20181023070344

awk 打印單引號

awk 'BEGIN{print "a'"'"'s"}' # 不用脫義,就多寫幾個單引號、雙引號函數

awk 'BEGIN{print "a'''s"}' # 用脫義,脫義的是單引號code

awk 'BEGIN{print "a"s"}' # 用脫義,脫義的是雙引號htm

[root@centos01 t1022]# awk 'BEGIN{print "a'"'"'s"}'
a's
相關文章
相關標籤/搜索