Unix-commands-awk
提供:Dev Guides
awk-Unix、Linuxコマンド
NAME
概要
'プログラム' input-file1 input-file2 … awk -f PROGRAM-FILE input-file1 input-file2 …
説明
awkコマンドは、パターンを含むテキストのファイルを検索します。 行またはテキストが一致すると、awkはその行/テキストに対して特定のアクションを実行します。 プログラム文は、実行する操作をawkに指示します。プログラムステートメントは一連の「ルール」で構成され、各ルールは検索する1つのパターンと、特定のパターンが見つかったときに実行する1つのアクションを指定します。 スラッシュ(/)で囲まれた正規表現は、そのセットに属するテキストを持つすべての入力レコードに一致するawkパターンです。
オプション
Tag | Description |
---|---|
-F FS—field-separator FS | Use FS for the input field separator (the value of the 'FS' predefined variable). |
-f PROGRAM-FILE—file PROGRAM-FILE | Read the awk program source from the file PROGRAM-FILE, instead of from the first command line argument. |
-mf NNN-mr NNN | The 'f' flag sets the maximum number of fields, and the 'r' flag sets the maximum record size. These options are ignored by 'gawk', since 'gawk' has no predefined limits; they are only for compatibility with the Bell Labs research version of Unix awk. |
-v VAR=VAL—assign VAR=VAL | Assign the variable VAR the value VAL before program execution begins. |
-W traditional-W compat—traditional—compat | Use compatibility mode, in which 'gawk' extensions are turned off. |
-W lint—lint | Give warnings about dubious or non-portable awk constructs. |
-W lint-old—lint-old | Warn about constructs that are not available in the original Version 7 Unix version of awk. |
-W posix—posix | Use POSIX compatibility mode, in which 'gawk' extensions are turned off and additional restrictions apply. |
-W re-interval—re-interval | Allow interval expressions, in regexps. |
-W source=PROGRAM-TEXT—source PROGRAM-TEXT | Use PROGRAM-TEXT as awk program source code. This option allows mixing command line source code with source code from files, and is particularly useful for mixing command line programs with library functions. |
— | Signal the end of options. This is useful to allow further arguments to the awk program itself to start with a '-'. This is mainly for consistency with POSIX argument parsing conventions. |
'Program' | A series of patterns and actions |
Input-File | If no Input-File is specified then awk applies the Program to "standard input", (piped output of some other command or the terminal. Typed input will continue until end-of-file (typing 'Control-d') |
例
- ls-lリストの出力の各行から2番目のアイテム($ 2)を返します。
- sample.txtの各行から行番号(NR)、ダッシュとスペース( "-")、そして最初の項目($ 1)を印刷します。 最初にsample.txtファイルを作成します
- sample.txtの各行から最初のアイテム($ 1)を印刷し、次に2番目の最後のアイテム$(NF-1)を印刷します。
- ファイルから空でない行を印刷します。
- 最も長い入力行の長さを印刷します。
- ファイル内の行を数えるには
link:/cgi-bin/printpage.cgi [__印刷]