AWKツール学習(一)-awk紹介
2169 ワード
任意のawk文はモードと動作からなり、モード駆動動作はイベントをトリガし、動作は入力行に対する処理を実行する.
awkは'?'、'+'をサポート2つの拡張メタ文字ですがgrepとsedはサポートされていません
A.1小例を挙げてawkの使用を示す(以下、すべての操作手順)
以上は入力ファイルが空行であるか否かを判断する一例であり、1箇所入力が空行であるため、2を実行した後に'this a blank line'を出力する
3箇所に空以外のデータが入力されているため、4を実行しても出力されない
また、コマンドラインモードでは、入力ファイルが指定されていない場合、コマンドラインから関連テキストが読み出されます
A.2 awkプログラミングモデル
awkプログラムは1つの主入力サイクルによって維持され、主サイクルは順次ファイル行を読み取り、プログラマは行を操作するだけで、ファイルを開く、ファイルを閉じる操作はすべて主サイクルによって管理される.
awkはまた、BEGINとENDの2つの特殊なフィールドを定義します.BEGINは、ファイル行が読み込まれない前に実行するために使用されます.前の例:
A.3呼び出し方法
簡単に言えば、
1.コマンドライン実行
awk[-Fドメイン区切り記号]'awkプログラムセグメント'入力ファイル
hdfs cat/user/ddclick/visitpath-v2_2013-02-18/step_dict/part-* | awk -F~==~ '{print $1}' > urls
2.-f呼び出しを使用
awk-f awkスクリプトファイル入力ファイル
3.スクリプトを実行可能に設定し、直接実行する
./awkスクリプトファイル入力ファイル
awkは'?'、'+'をサポート2つの拡張メタ文字ですがgrepとsedはサポートされていません
A.1小例を挙げてawkの使用を示す(以下、すべての操作手順)
[lidc@hd66 awk-experiment]$ touch input
[lidc@hd66 awk-experiment]$ awk '/^$/{print "this is a blank line"}' input
[lidc@hd66 awk-experiment]$ cat input
1.[lidc@hd66 awk-experiment]$ echo '' > input
[lidc@hd66 awk-experiment]$ cat input
2.[lidc@hd66 awk-experiment]$ awk '/^$/{print "this is a blank line"}' input
this is a blank line
3.[lidc@hd66 awk-experiment]$ echo 'test by tony' >input
[lidc@hd66 awk-experiment]$ cat input
test by tony
4.[lidc@hd66 awk-experiment]$ awk '/^$/{print "this is a blank line"}' input
[lidc@hd66 awk-experiment]$
以上は入力ファイルが空行であるか否かを判断する一例であり、1箇所入力が空行であるため、2を実行した後に'this a blank line'を出力する
3箇所に空以外のデータが入力されているため、4を実行しても出力されない
また、コマンドラインモードでは、入力ファイルが指定されていない場合、コマンドラインから関連テキストが読み出されます
[lidc@hd66 awk-experiment]$ awk '/^$/{print "this is a blank line"}'
this is a blank line
this is a blank line
this is a blank line
A.2 awkプログラミングモデル
awkプログラムは1つの主入力サイクルによって維持され、主サイクルは順次ファイル行を読み取り、プログラマは行を操作するだけで、ファイルを開く、ファイルを閉じる操作はすべて主サイクルによって管理される.
awkはまた、BEGINとENDの2つの特殊なフィールドを定義します.BEGINは、ファイル行が読み込まれない前に実行するために使用されます.前の例:
[lidc@hd66 awk-experiment]$ awk 'BEGIN{print "joke"} /^$/{print "this is a blank line"}END{print "the end"}' input
joke
this is a blank line
the end
[lidc@hd66 awk-experiment]$ cat input
test by tony
[lidc@hd66 awk-experiment]$
A.3呼び出し方法
簡単に言えば、
1.コマンドライン実行
awk[-Fドメイン区切り記号]'awkプログラムセグメント'入力ファイル
hdfs cat/user/ddclick/visitpath-v2_2013-02-18/step_dict/part-* | awk -F~==~ '{print $1}' > urls
2.-f呼び出しを使用
awk-f awkスクリプトファイル入力ファイル
3.スクリプトを実行可能に設定し、直接実行する
./awkスクリプトファイル入力ファイル