Unix-commands-awk

提供:Dev Guides
移動先:案内検索

awk-Unix、Linuxコマンド

NAME

*awk* -テキスト、データベースのソート/検証/インデックスの検索と置換

概要

'プログラム' 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)を返します。
$ ls -l | awk '{print $2}'
13
3
17
7
  • sample.txtの各行から行番号(NR)、ダッシュとスペース( "-")、そして最初の項目($ 1)を印刷します。 最初にsample.txtファイルを作成します
Sample Line 1
Sample Line 2
Sample Line 3
$ awk '{print NR "- " $1 }' sample.txt
1 - Sample
2 - Sample
3 - Sample
  • sample.txtの各行から最初のアイテム($ 1)を印刷し、次に2番目の最後のアイテム$(NF-1)を印刷します。
$ awk '{print $1, $(NF-1) }' sample.txt
Sample Line
Sample Line
Sample Line
  • ファイルから空でない行を印刷します。
$ awk 'NF > 0' sample.txt
  • 最も長い入力行の長さを印刷します。
$ awk '{ if (length($0) > max) max = length($0) } END { print max }' sample.txt
13
*0から100までの7つの乱数を出力します。
$  awk 'BEGIN { for (i = 1; i <= 7; i++) print int(101* rand()) }'
24
29
85
15
59
19
81
  • ファイル内の行を数えるには
$ awk 'END { print NR }' sample.txt
3

link:/cgi-bin/printpage.cgi [__印刷]