日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

awk命令扩展使用操作

發布時間:2024/4/14 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 awk命令扩展使用操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

awk 中使用外部shell變量

示例1

[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運行需要的參數

示例2

[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`;doecho "[$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的時候

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

[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

擴展:

  • 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` doawk -F ':' -v a=$i '{$a;printf $a ""}' test01.txtecho 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,2005100110101

[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"}' # 用脫義,脫義的是單引號

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

[root@centos01 t1022]# awk 'BEGIN{print "a'"'"'s"}' a's

轉載于:https://my.oschina.net/u/996931/blog/2250974

總結

以上是生活随笔為你收集整理的awk命令扩展使用操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。