SlideShare une entreprise Scribd logo
1  sur  27
Télécharger pour lire hors ligne
テキストを切り刻め!!!
awkコマンドを紹介します

オイシックス・ラ・大地(株)@morihaya55
Photo by Alexandr Podvalny on Unsplash: https://unsplash.com/photos/WOxddhzhC1w
オイラ大地 社内LT大会
2019-08-20
take-home message
● awkを使うとテキストを自在に刻める
● awkはWindowsでも使える
● awkは軽量で大容量のテキスト処理も簡単
本日の流れ
● What is awk ?
● 実例awk
○ 横に刻む(選択)
○ 縦に刻む(射影)
● Why do you use awk ?
● まとめ
What is awk ?
AWK(オーク)は、プログラミング言語の一つ。 テキス
トファイル、特に空白類(スペースの他、タブなど)やカ
ンマなどで区切られたデータファイルの処理を念頭に
置いた仕様となっているが、 一般的なプログラミング
に用いることも可能である。 UNIX上で開発された。
https://ja.wikipedia.org/wiki/AWK
それではどんな感じに便利なの
かをご紹介
実例awk
awkは縦と横に刻めます
● 横に刻む(選択)
● 縦に刻む(射影)
実例awk
実例awk - 横に刻む
番号 氏名    所属

0001 オイラ太郎 大崎

0002 ORD二郎  大崎

0003 ORD三郎  海老名

0004 JohnSmith ボストン

こんなテキストファイルがあるとして (data.txt)
実例awk - 横に刻む
番号 氏名    所属

0001 オイラ太郎 大崎

0002 ORD二郎  大崎

0003 ORD三郎  海老名

0004 JohnSmith ボストン

“大崎” を含むレコードを選択したい場合
実例awk - 横に刻む
こんなコマンドを黒い画面で打ちます
awk /大崎/'{print $0}' data.txt
実例awk - 横に刻む
結果がこうなります
bash-3.2$ awk /大崎/'{print $0}' data.txt
0001 オイラ太郎 大崎
0002 ORD二郎  大崎
実例awk - 横に刻む
こんなコマンドを黒い画面で打ちます
awk /大崎/'{print $0}' data.txt
“awk” はコマンドなので固定
実例awk - 横に刻む
こんなコマンドを黒い画面で打ちます
awk /大崎/'{print $0}' data.txt
“大崎” は横に選択したい文字列
実例awk - 横に刻む
こんなコマンドを黒い画面で打ちます
awk /大崎/'{print $0}' data.txt
“print $0” はプログラムを記載
縦に射影する時にも使う
実例awk - 横に刻む
こんなコマンドを黒い画面で打ちます
awk /大崎/'{print $0}' data.txt
“data.txt” は処理を行うファイル
実例awk - 縦に刻む
番号 氏名    所属

0001 オイラ太郎 大崎

0002 ORD二郎  大崎

0003 ORD三郎  海老名

0004 JohnSmith ボストン

“氏名” だけのデータが欲しい場合
実例awk - 横に刻む
こんなコマンドを黒い画面で打ちます
awk '{print $2}' data.txt
実例awk - 横に刻む
結果がこうなります
bash-3.2$ awk '{print $2}' data.txt
氏名
オイラ太郎
ORD二郎
ORD三郎
JohnSmith
実例awk - 縦に刻む
番号 氏名    所属

0001 オイラ太郎 大崎

0002 ORD二郎  大崎

0003 ORD三郎  海老名

0004 JohnSmith ボストン

空白区切りで連番になってる (区切り文字は指定可能)
$2$1 $3
だから?
Excelでいいじゃん??(・
ε ・)フーン
Excelでサクッとできるなら
もちろんOK
しかしExcelより圧倒的に
awkが有利なシーンがあり
ます
Why do we use awk ?
● どんなに超弩級の大容量なファイルでも
○ 処理できる(1行ずつ処理するから)
○ 極めて高速
● プログラミングによる値変換&出力制御
● 別ファイルへ簡単に出力ができる
● 他のコマンドとの組み合わせが可能
Why do we use awk ?
2,049,062行で339MBの”big.csv”
Excelで開くと1分ほどかかるしなんか怪しい
Why do we use awk ?
awkで”09:20:13”のログを出してみる
time awk /09:20:13/'{print $0}' big.csv
-> real 0m1.056s 約1秒
Windowsでのawk
以下のような手段でawkはWindowsで使えます
● gawkをインストールする
● WSLを利用する(Windows 10以降) 欲しい
● Cygwinを利用する
という訳で
 まとめ
take-home message
● awkを使うとテキストを自在に刻める
● awkはWindowsでも使える
● awkは軽量で大容量のテキスト処理も簡単
使ってみたくなったらお声がけください!
ご静聴感謝!! m( _ _ )m

Contenu connexe

Tendances

Spark Streaming on AWS -S3からKinesisへ-
Spark Streaming on AWS -S3からKinesisへ-Spark Streaming on AWS -S3からKinesisへ-
Spark Streaming on AWS -S3からKinesisへ-chibochibo
 
Spark in small or middle scale data processing with Elasticsearch
Spark in small or middle scale data processing with ElasticsearchSpark in small or middle scale data processing with Elasticsearch
Spark in small or middle scale data processing with Elasticsearchchibochibo
 
XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法
XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法
XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法Kazuho Oku
 
Sql world を支える技術
Sql world を支える技術Sql world を支える技術
Sql world を支える技術Oda Shinsuke
 
Objective-Cのいろいろな反復処理
Objective-Cのいろいろな反復処理Objective-Cのいろいろな反復処理
Objective-Cのいろいろな反復処理Kosuke Ogawa
 
Amazon DynamoDBの紹介と東急ハンズでの活用について
Amazon DynamoDBの紹介と東急ハンズでの活用についてAmazon DynamoDBの紹介と東急ハンズでの活用について
Amazon DynamoDBの紹介と東急ハンズでの活用についてTaiji INOUE
 

Tendances (6)

Spark Streaming on AWS -S3からKinesisへ-
Spark Streaming on AWS -S3からKinesisへ-Spark Streaming on AWS -S3からKinesisへ-
Spark Streaming on AWS -S3からKinesisへ-
 
Spark in small or middle scale data processing with Elasticsearch
Spark in small or middle scale data processing with ElasticsearchSpark in small or middle scale data processing with Elasticsearch
Spark in small or middle scale data processing with Elasticsearch
 
XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法
XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法
XSSに強いウェブサイトを作る – テンプレートエンジンの選定基準とスニペットの生成手法
 
Sql world を支える技術
Sql world を支える技術Sql world を支える技術
Sql world を支える技術
 
Objective-Cのいろいろな反復処理
Objective-Cのいろいろな反復処理Objective-Cのいろいろな反復処理
Objective-Cのいろいろな反復処理
 
Amazon DynamoDBの紹介と東急ハンズでの活用について
Amazon DynamoDBの紹介と東急ハンズでの活用についてAmazon DynamoDBの紹介と東急ハンズでの活用について
Amazon DynamoDBの紹介と東急ハンズでの活用について
 

Plus de Yukiya Hayashi

I have a problem when operating AWS with multiple accounts
I have a problem when operating AWS with multiple accountsI have a problem when operating AWS with multiple accounts
I have a problem when operating AWS with multiple accountsYukiya Hayashi
 
My misstake on Ansible’s lineinfile module
My misstake on Ansible’s lineinfile moduleMy misstake on Ansible’s lineinfile module
My misstake on Ansible’s lineinfile moduleYukiya Hayashi
 
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021Yukiya Hayashi
 
AWS Cognito makes old web apps available from anywhere
AWS Cognito makes old web apps available from anywhereAWS Cognito makes old web apps available from anywhere
AWS Cognito makes old web apps available from anywhereYukiya Hayashi
 
アドベントカレンダー から学ぶOCIの空気感
アドベントカレンダー から学ぶOCIの空気感アドベントカレンダー から学ぶOCIの空気感
アドベントカレンダー から学ぶOCIの空気感Yukiya Hayashi
 
オンボーディングを楽しむ
オンボーディングを楽しむオンボーディングを楽しむ
オンボーディングを楽しむYukiya Hayashi
 
事前アンケート集計 Terraform meetup tokyo#2
事前アンケート集計 Terraform meetup tokyo#2事前アンケート集計 Terraform meetup tokyo#2
事前アンケート集計 Terraform meetup tokyo#2Yukiya Hayashi
 
I want the power of onboarding!
I want the power of  onboarding!I want the power of  onboarding!
I want the power of onboarding!Yukiya Hayashi
 
How did you start learning Azure
 How did you start learning Azure How did you start learning Azure
How did you start learning AzureYukiya Hayashi
 
My feelings of going to the first conference overseas
My feelings of going to the first conference overseasMy feelings of going to the first conference overseas
My feelings of going to the first conference overseasYukiya Hayashi
 
What i feel when began use AWS CodePipeline as GitLab Ci user
What i feel when began use AWS CodePipeline as GitLab Ci userWhat i feel when began use AWS CodePipeline as GitLab Ci user
What i feel when began use AWS CodePipeline as GitLab Ci userYukiya Hayashi
 
How to get rid of terraform plan diffs
How to get rid of terraform plan diffsHow to get rid of terraform plan diffs
How to get rid of terraform plan diffsYukiya Hayashi
 
Task and Time monitoring with Backlog and Toggl
Task and Time monitoring with Backlog and TogglTask and Time monitoring with Backlog and Toggl
Task and Time monitoring with Backlog and TogglYukiya Hayashi
 
Oiradaichi's Akamai Journey
Oiradaichi's Akamai Journey Oiradaichi's Akamai Journey
Oiradaichi's Akamai Journey Yukiya Hayashi
 
What does the monitoring tool use at oisix ra daichi?
What does the monitoring tool use at oisix ra daichi?What does the monitoring tool use at oisix ra daichi?
What does the monitoring tool use at oisix ra daichi?Yukiya Hayashi
 
We love backlog ! in reCap event.
We love backlog ! in reCap event.We love backlog ! in reCap event.
We love backlog ! in reCap event.Yukiya Hayashi
 
What we expect of neo4j
What we expect of neo4jWhat we expect of neo4j
What we expect of neo4jYukiya Hayashi
 
Backlog World 2019 LT - We love backlog !
Backlog World 2019 LT - We love backlog !Backlog World 2019 LT - We love backlog !
Backlog World 2019 LT - We love backlog !Yukiya Hayashi
 
Introduce that Best practices for writing Dockerfiles
Introduce that Best practices for writing DockerfilesIntroduce that Best practices for writing Dockerfiles
Introduce that Best practices for writing DockerfilesYukiya Hayashi
 

Plus de Yukiya Hayashi (20)

I have a problem when operating AWS with multiple accounts
I have a problem when operating AWS with multiple accountsI have a problem when operating AWS with multiple accounts
I have a problem when operating AWS with multiple accounts
 
My misstake on Ansible’s lineinfile module
My misstake on Ansible’s lineinfile moduleMy misstake on Ansible’s lineinfile module
My misstake on Ansible’s lineinfile module
 
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
AWS SSO x On-Prem AD Easy IAM user management on Jtf2021
 
AWS Cognito makes old web apps available from anywhere
AWS Cognito makes old web apps available from anywhereAWS Cognito makes old web apps available from anywhere
AWS Cognito makes old web apps available from anywhere
 
アドベントカレンダー から学ぶOCIの空気感
アドベントカレンダー から学ぶOCIの空気感アドベントカレンダー から学ぶOCIの空気感
アドベントカレンダー から学ぶOCIの空気感
 
オンボーディングを楽しむ
オンボーディングを楽しむオンボーディングを楽しむ
オンボーディングを楽しむ
 
事前アンケート集計 Terraform meetup tokyo#2
事前アンケート集計 Terraform meetup tokyo#2事前アンケート集計 Terraform meetup tokyo#2
事前アンケート集計 Terraform meetup tokyo#2
 
I want the power of onboarding!
I want the power of  onboarding!I want the power of  onboarding!
I want the power of onboarding!
 
How did you start learning Azure
 How did you start learning Azure How did you start learning Azure
How did you start learning Azure
 
My feelings of going to the first conference overseas
My feelings of going to the first conference overseasMy feelings of going to the first conference overseas
My feelings of going to the first conference overseas
 
What i feel when began use AWS CodePipeline as GitLab Ci user
What i feel when began use AWS CodePipeline as GitLab Ci userWhat i feel when began use AWS CodePipeline as GitLab Ci user
What i feel when began use AWS CodePipeline as GitLab Ci user
 
How to get rid of terraform plan diffs
How to get rid of terraform plan diffsHow to get rid of terraform plan diffs
How to get rid of terraform plan diffs
 
Task and Time monitoring with Backlog and Toggl
Task and Time monitoring with Backlog and TogglTask and Time monitoring with Backlog and Toggl
Task and Time monitoring with Backlog and Toggl
 
Oiradaichi's Akamai Journey
Oiradaichi's Akamai Journey Oiradaichi's Akamai Journey
Oiradaichi's Akamai Journey
 
What does the monitoring tool use at oisix ra daichi?
What does the monitoring tool use at oisix ra daichi?What does the monitoring tool use at oisix ra daichi?
What does the monitoring tool use at oisix ra daichi?
 
We love backlog ! in reCap event.
We love backlog ! in reCap event.We love backlog ! in reCap event.
We love backlog ! in reCap event.
 
What we expect of neo4j
What we expect of neo4jWhat we expect of neo4j
What we expect of neo4j
 
Backlog World 2019 LT - We love backlog !
Backlog World 2019 LT - We love backlog !Backlog World 2019 LT - We love backlog !
Backlog World 2019 LT - We love backlog !
 
20190116 neo4jug-lt
20190116 neo4jug-lt20190116 neo4jug-lt
20190116 neo4jug-lt
 
Introduce that Best practices for writing Dockerfiles
Introduce that Best practices for writing DockerfilesIntroduce that Best practices for writing Dockerfiles
Introduce that Best practices for writing Dockerfiles
 

Let's split text by awk command