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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

awk,gawk调用shell,bash中的变量 笔记221106

發布時間:2023/12/14 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 awk,gawk调用shell,bash中的变量 笔记221106 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

awk,gawk調用shell,bash中的變量 筆記221106

  • "'${變量名}'" 雙 包 單 包 ${} 包 變量名
  • "'"${變量名}"'" 雙 包 單 包 雙 包 ${} 包 變量名

方法一: "雙引號"包裹’單引號’ 包裹${} 包裹變量名

"'${變量名}'" 雙單${量}單雙


例1

v001=HelloWorld echo aaa | awk '{print "'${v001}'"}'

前面的 echo aaa 只是滿足awk要有一個輸入,沒有別的作用


例2

echo a | awk '{print "'$PATH'"}'



方法二: 方法一再包一層雙引號 : 雙 包 單 包 雙 包 ${} 包 變量名

"'"${變量名}"'" 雙單雙 ${ 變量名 } 雙單雙


例1

v001=HelloWorld echo aaa | awk '{print "'"${v001}"'"}'

前面的 echo a 只是滿足awk要有一個輸入,沒有別的作用


例2

echo a | awk '{print "'"$PATH"'"}'



方法三 使用-v選項

-v選項 可以定義awk變量, 并將一個shell變量賦值給awk變量

例1

v001=HelloWorld awk -v v=$v001 'BEGIN { print v } '

例2

awk -v v=${PATH} 'BEGIN{print v}'

當 AWK 只使用 BEGIN{}語句塊 時, 可以不需要輸入流




AWK介紹

簡介:
AWK是一種解釋性編程語言,主要用于文本處理。之所以叫AWK是因為其取了三位創始人Alfred Aho,Peter Weinberger,和Brian Kernighan 的 Family Name 的首字符。

GNU/Linux發布的AWK目前由自由軟件基金會(FSF)進行開發和維護,通常也稱它為GNU AWK。

AWK的幾個實現:

  • AWK:原先來源于AT&T實驗室的的AWK;
    AT&T(American Telephone & Telegraph的縮寫 是一家美國電信公司)
  • NAWK:AT&T實驗室的AWK的升級版;
  • GAWK:這就是GNU AWK;
  • MAWK:一種輕巧快速的AWK實現。
  • AWK介紹1

    各種AWK版本

    當前使用的AWK有三個主要版本,并且它們都符合POSIX標準(至少對于絕大多數用例而言,足夠接近)。

    • 第一個是經典awk,它是 Aho,Weinberger , Kernighan (3個人) 在他們的《 AWK編程語言》一書中描述的AWK版本。有時稱為 “新AWK”(nawk)或 “一個真正的AWK”,現在托管在GitHub上。這是許多基于BSD的系統(包括macOS)上預先安裝的版本(盡管macOS隨附的版本已過時,需要升級)。

    • 第二個是GNU Awk(gawk),它是迄今為止功能最強大,維護最活躍的版本。Gawk通常預先安裝在Linux系統上,并且通常是默認的awk。使用Homebrew可以很容易地在macOS上安裝,Gawk還提供Windows二進制文件。自1994年以來,Arnold Robbins一直是gawk的主要維護者,并繼續推廣該語言(他還為經典的awk版本做出了許多修復)。Gawk具有awk或POSIX標準中未提供的許多功能,包括新函數,聯網功能,C擴展API,事件探查器和調試器以及最近的名稱空間。

    • 第三個通用版本是mawk,由Michael Brennan編寫。它是Ubuntu和Debian Linux上的默認awk,并且仍然是AWK的最快版本,具有字節碼編譯器和更節省內存的值表示形式。(從4.0開始,Gawk也使用了字節碼編譯器,因此它現在已經接近mawk的速度。)

    如果你想將AWK用于單行和基本文本處理,則上述任何方法都是不錯的選擇。如果你打算將其用于較大的腳本或程序,Gawk的功能使其成為明智的選擇。

    AWK的其他幾種實現也具有不同的成熟度和維護級別。值得注意的有這些:嵌入式Linux環境中使用的尺寸優化的BusyBox版本,支持運行時訪問Java語言的Java版以及我自己用Go編寫的GoAWK(與POSIX兼容的版本)。三個主要的AWK和BusyBox版本都用C編寫。


    AWK是否仍然有意義?

    AWK是否仍然有用這個問題,就像在問空氣是否有用一樣:你可能看不到它,但周圍無處不在。許多Linux管理員和DevOps工程師使用它來轉換數據或通過日志文件診斷問題。幾乎所有基于Unix的計算機上都安裝了AWK版本。除了臨時使用外,許多大型開源項目還在其構建或文檔工具中使用AWK。僅舉幾個例子:Linux內核在x86工具中使用它來檢查 和重新格式化 objdump文件,Neovim使用它來生成文檔,而FFmpeg使用它來進行構建和測試。

    即使人們不再想看到AWK,AWK構建腳本也很難被殺死:2018年,LWN 寫了一篇文章,是關于GCC貢獻者想要用Python替換AWK腳本來生成其選項解析代碼。這個建議在當時有一些支持,但顯然沒有人主動要求做實際的移植,現在AWK腳本依然存在。

    Robbins在他的2018年論文中主張將AWK(特別是gawk)用作“系統編程語言”,在這種情況下,其含義是用于編寫較大的工具和程序的語言。他概述了他認為尚未流行的原因,但Kernighan“ 并非100%確信 ”缺乏擴展機制是AWK不被大型程序廣泛使用的主要原因。他建議,這可能是由于缺乏對訪問系統調用等內容的內置支持。但是,這些都沒有阻止幾個人開發更大的工具:Robbins自己編寫的TexiWeb Jr.識字編程工具(1300行AWK),Werner Stoop的d.awk工具用于從源碼中備注的MarkDown生成文檔, Translate Shell是一個6000行AWK工具,可為基于云的翻譯API提供相當強大的命令行界面。

    過去幾年中,有幾位開發人員寫過關于在其“大數據”工具包中使用AWK的信息,它比重型分布式計算系統(如Spark和Hadoop)簡單得多(有時更快)。Nick Strayer 撰寫了 有關使用AWK和R解析多個內核中25 TB數據的文章。其他大數據示例包括Adam Drake 寫的標題頗為誘人的文章:“命令行工具可以比Hadoop集群快235倍”,以及Brendan O’Connor的“ 不要使用MAWK,AWK是最快,最優雅的大數據處理語言”。

    命令行工具可以比Hadoop集群快235倍 ???

    總而言之,在臨時文本修改,構建工具,“系統編程”和大數據處理之間(更不用說文本模式的第一人稱射擊游戲),AWK似乎在2020年還活得很好。



    AWK介紹2

    awk 是一種編程語言,用于在linux/unix下對文本和數據進行處理。數據可以來自標準輸入、一個或多個文件,或其它命令的輸出(即管道)。它支持用戶自定義函數和 動態正則表達式等先進功能,是linux/unix下的一個強大編程工具。它在命令行中使用,但更多是作為腳本來使用。

    awk的處理文本和數據的方式是這 樣的,它逐行掃描文件,從第一行到最后一行,尋找匹配的特定模式的行,并在這些行上進行你想要的操作。如果沒有指定處理動作,則把匹配的行顯示到標準輸出 (屏幕),即默認處理動作是print;如果沒有指定模式,則所有被操作所指定的行都被處理,即默認指定模式是全部。awk分別代表其作者姓氏的第一個字母。因為它的作者是三個人,分別是Alfred Aho、Brian Kernighan、Peter Weinberger。gawk是awk的GNU版本,它提供了Bell實驗室和GNU的一些擴展。

    像shell一樣,awk也有好幾種,常見的如awk、nawk、mawk、gawk,其中
    awk:最初在1 9 7 7年完成,1 9 8 5年發表了一個新版本的awk,它的功能比舊版本增強了不少,awk 能夠用很短的程序對文檔里的資料做修改、比較、提取、打印等處理,如果使用C 或P a s c a l 等語言編寫程序完成上述的任務會十分不方便而且很花費時間,所寫的程序也會很大;

    nawk: 在 20 世紀 80 年代中期,對 awk語言進行了更新,并不同程度地使用一種稱為 nawk(new awk) 的增強版本對其進行了替換。許多系統中仍然存在著舊的awk 解釋器,但通常將其安裝為 oawk (old awk) 命令,而 nawk 解釋器則安裝為主要的 awk 命令,也可以使用 nawk 命令。Dr. Kernighan 仍然在對 nawk 進行維護,與 gawk 一樣,它也是開放源代碼的,并且可以免費獲得;

    mawk:mawk 是 awk 編程語言的解釋器。awk語言在多媒體數據文件以及文本的檢索和處理,算法的原型設計和試驗都有廣泛的使用。mawk帶給awk新的概念,它實現了在《The AWK Programming Language》(Aho, Kernighan and Weinberger, The AWK Programming Language, Addison-Wesley Publishing, 1988.被認為是 AWK 手冊。)中定義的 awk語言。mawk遵循 POSIX 1003.2 (草案 11.3)定義的 AWK 語言,包含了一些沒有在AWK 手冊中提到的特色,同時 mawk 提供一小部分擴展,另外據說mawk是實現最快的awk;

    gawk: 是 GNU Project 的awk解釋器的開放源代碼實現。盡管早期的 GAWK 發行版是舊的 AWK 的替代程序,但不斷地對其進行了更新,以包含 NAWK 的特性;
    目前,大家都比較傾向于使用awk和gawk。Ubuntu系統中的各種awk的選項設置,可以通過sudo update-alternatives --config awk來完成,實際上你通過手動修改軟鏈接也能實現。Debian最小化安裝的時候awk的鏈接是指向mawk的。

    sudo update-alternatives --config awk

    GAWK gawk 5 新特性

    GNU Awk 5.0.0 發布了,Awk(Gawk)是一種編程語言,用于在 Linux/unix 下對文本和數據進行處理。數據可以來自標準輸入、文件或其它命令的輸出。它支持用戶自定義函數和動態正則表達式等功能。

    這是一個重要的新版本,帶來了一些新功能和功能改進,最大的亮點是實現了命名空間。相比 4.2.1,變化包括:

    • 添加了對 POSIX 標準 %a 和 %A 輸出格式的支持。
    • 測試基礎結構得到了極大的改進,簡化了 test/Makefile.am 的內容,并且可以從 test/Makefile.in 生成 pc/Makefile.tst。
    • 正則表達式例程已經被 GNULIB 中的例程取代。
    • 基礎設施升級:Bison 3.3、Automake 1.16.1、Gettext 0.19.8.1、makeinfo 6.5。
    • 刪除了未記錄的配置選項和允許在標識符中使用非英語“字母”的代碼。
    • 刪除了 `–with-whiny-user-strftime’ 配置選項現在已經消失。
    • 更好地適應 C99 環境。
    • PROCINFO[“platform”] 產生一個字符串,表示編譯 gawk 的平臺。
    • 寫入不是變量名的 SYMTAB 元素現在會導致致命錯誤。
    • pretty-printer 中的注釋處理幾乎完全從頭開始重新設計,以往無法支持的許多極端情況中的注釋現在可以格式化中輸出。
    • 已經實現命名空間。命令行程序段現在必須都是自包含的句法單元,比如現在不能這么寫:gawk -e ‘BEGIN {’ -e ‘print “hello” }’
    • 現在使用語言環境設置來忽略單字節語言環境中的大小寫,而不是使用 Latin-1 中的硬連線法。
    • 修復了一些 bug,包括特別重要的 bug。



    awk 執行流程圖

    Created with Rapha?l 2.3.0執行 AWK 的 BEGIN{}語句塊 的命令從 輸入流(開頭管道或末尾文件) 讀取一行用表達式篩選該行, 執行表達式語句塊的命令例1{print} , 例2/pattern/{print "hello"} , 例3 $n!~/pattern/{代碼}是否讀取完最后一行執行 AWK 的 END{}語句塊 的命令yesno

    #mermaid-svg-V3Kw2zD2x5vIZu5z {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .error-icon{fill:#552222;}#mermaid-svg-V3Kw2zD2x5vIZu5z .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-V3Kw2zD2x5vIZu5z .marker{fill:#333333;stroke:#333333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .marker.cross{stroke:#333333;}#mermaid-svg-V3Kw2zD2x5vIZu5z svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-V3Kw2zD2x5vIZu5z .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .cluster-label text{fill:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .cluster-label span{color:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .label text,#mermaid-svg-V3Kw2zD2x5vIZu5z span{fill:#333;color:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .node rect,#mermaid-svg-V3Kw2zD2x5vIZu5z .node circle,#mermaid-svg-V3Kw2zD2x5vIZu5z .node ellipse,#mermaid-svg-V3Kw2zD2x5vIZu5z .node polygon,#mermaid-svg-V3Kw2zD2x5vIZu5z .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-V3Kw2zD2x5vIZu5z .node .label{text-align:center;}#mermaid-svg-V3Kw2zD2x5vIZu5z .node.clickable{cursor:pointer;}#mermaid-svg-V3Kw2zD2x5vIZu5z .arrowheadPath{fill:#333333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-V3Kw2zD2x5vIZu5z .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-V3Kw2zD2x5vIZu5z .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-V3Kw2zD2x5vIZu5z .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-V3Kw2zD2x5vIZu5z .cluster text{fill:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z .cluster span{color:#333;}#mermaid-svg-V3Kw2zD2x5vIZu5z div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-V3Kw2zD2x5vIZu5z :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;}沒有讀取完執行AWK的 BEGIN語句塊 的命令從 輸入流 讀取一行用表達式篩選該行,
    執行表達式語句塊塊的命令是否讀取完最后一行執行AWK的 END語句塊 的命令

    awk的選項

    通用常用選項,

    • -F 指定分隔符
    • -f 指定腳本文件 (注意:是腳本文件, 不是輸入文件, 輸入文件寫在末尾)
    • -v 定義awk自己的變量, 例如 -v name=小明

    這三個選項符合 POSIX 規范

    來自fedora36👇

    Usage: awk [POSIX or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or GNU style options] [--] 'program' file ... POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-D[file] --debug[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-i includefile --include=includefile-I --trace-l library --load=library-L[fatal|invalid|no-ext] --lint[=fatal|invalid|no-ext]-M --bignum-N --use-lc-numeric-n --non-decimal-data-o[file] --pretty-print[=file]-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-s --no-optimize-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info' which is section `Reporting Problems and Bugs' in the printed version. This same information may be found at https://www.gnu.org/software/gawk/manual/html_node/Bugs.html. PLEASE do NOT try to report bugs by posting in comp.lang.awk, or by using a web forum such as Stack Overflow.gawk is a pattern scanning and processing language. By default it reads standard input and writes standard output.Examples:awk '{ sum += $1 }; END { print sum }' fileawk -F: '{ print $1 }' /etc/passwd

    gawk的選項

    來自Fedora36👇

    Usage: gawk [POSIX or GNU style options] -f progfile [--] file ... Usage: gawk [POSIX or GNU style options] [--] 'program' file ... POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-D[file] --debug[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-i includefile --include=includefile-I --trace-l library --load=library-L[fatal|invalid|no-ext] --lint[=fatal|invalid|no-ext]-M --bignum-N --use-lc-numeric-n --non-decimal-data-o[file] --pretty-print[=file]-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-s --no-optimize-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info' which is section `Reporting Problems and Bugs' in the printed version. This same information may be found at https://www.gnu.org/software/gawk/manual/html_node/Bugs.html. PLEASE do NOT try to report bugs by posting in comp.lang.awk, or by using a web forum such as Stack Overflow.gawk is a pattern scanning and processing language. By default it reads standard input and writes standard output.Examples:gawk '{ sum += $1 }; END { print sum }' filegawk -F: '{ print $1 }' /etc/passwd

    來自Ubuntu22.04Server版👇

    Usage: awk [POSIX or GNU style options] -f progfile [--] file ... Usage: awk [POSIX or GNU style options] [--] 'program' file ... POSIX options: GNU long options: (standard)-f progfile --file=progfile-F fs --field-separator=fs-v var=val --assign=var=val Short options: GNU long options: (extensions)-b --characters-as-bytes-c --traditional-C --copyright-d[file] --dump-variables[=file]-D[file] --debug[=file]-e 'program-text' --source='program-text'-E file --exec=file-g --gen-pot-h --help-i includefile --include=includefile-l library --load=library-L[fatal|invalid|no-ext] --lint[=fatal|invalid|no-ext]-M --bignum-N --use-lc-numeric-n --non-decimal-data-o[file] --pretty-print[=file]-O --optimize-p[file] --profile[=file]-P --posix-r --re-interval-s --no-optimize-S --sandbox-t --lint-old-V --versionTo report bugs, see node `Bugs' in `gawk.info' which is section `Reporting Problems and Bugs' in the printed version. This same information may be found at https://www.gnu.org/software/gawk/manual/html_node/Bugs.html. PLEASE do NOT try to report bugs by posting in comp.lang.awk, or by using a web forum such as Stack Overflow.gawk is a pattern scanning and processing language. By default it reads standard input and writes standard output.Examples:awk '{ sum += $1 }; END { print sum }' fileawk -F: '{ print $1 }' /etc/passwd

    mawk的選項

    來自Ubuntu22.04Desktop版👇

    Usage: mawk [Options] [Program] [file ...]Program:The -f option value is the name of a file containing program text.If no -f option is given, a "--" ends option processing; the followingparameters are the program text.Options:-f program-file Program text is read from file instead of from thecommand-line. Multiple -f options are accepted.-F value sets the field separator, FS, to value.-v var=value assigns value to program variable var.-- unambiguous end of options.Implementation-specific options are prefixed with "-W". They can beabbreviated:-W version show version information and exit.-W dump show assembler-like listing of program and exit.-W help show this message and exit.-W interactive set unbuffered output, line-buffered input.-W exec file use file as program as well as last option.-W random=number set initial random seed.-W sprintf=number adjust size of sprintf buffer.-W posix_space do not consider "\n" a space.-W usage show this message and exit.

    來自Ubuntu22.04Server版👇

    Usage: mawk [Options] [Program] [file ...]Program:The -f option value is the name of a file containing program text.If no -f option is given, a "--" ends option processing; the followingparameters are the program text.Options:-f program-file Program text is read from file instead of from thecommand-line. Multiple -f options are accepted.-F value sets the field separator, FS, to value.-v var=value assigns value to program variable var.-- unambiguous end of options.Implementation-specific options are prefixed with "-W". They can beabbreviated:-W version show version information and exit.-W dump show assembler-like listing of program and exit.-W help show this message and exit.-W interactive set unbuffered output, line-buffered input.-W exec file use file as program as well as last option.-W random=number set initial random seed.-W sprintf=number adjust size of sprintf buffer.-W posix_space do not consider "\n" a space.-W usage show this message and exit.

    總結

    以上是生活随笔為你收集整理的awk,gawk调用shell,bash中的变量 笔记221106的全部內容,希望文章能夠幫你解決所遇到的問題。

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