awkの基礎・grepとの違いについて | 瀬戸内の雲のように

awkの基礎・grepとの違いについて

Posted: 2018-09-18


スポンサーリンク

目次

背景

サーバエンジニアをやっている限り、トラブルというのは付き物です。
そしてトラブルの原因を見つけたり修復したりするためにはログファイルを加工し効率よく確認することが重要です。

今回はそんな時にとても役に立つ awk の使い方の基本を記事にしたいと思います。

 

awkとは

awkとはスクリプト言語の一種で、テキスト処理に優れているという特徴を持っています。
用途として一番わかりやすいのは行の中の特定のカラムを抜き出す場面だと思います。

使い方はいたって簡単で、

  • ファイルから読み込む
  • 標準入力からパイプで渡す

ということをすればすぐに使えます。実際の例をあげてみると

$ echo 'hello world' | awk '{print $1}'
hello

 
というように使うことができます。(上記はパイプで渡しているという例です)

 

awkとgrepの違い

先ほどawkはテキスト処理に優れていると書きましたが、テキスト処理でより身近なのはgrepだと思います。
では、awkとgrepはどのように違うかと言うと(細かいことを言うとものすごく違うのですが)、サーバエンジニアが使う場面での違いとしては

  • grepは文字列が存在する行全体を抜き出す
  • awkは行の中の一部だけ抜き出す(行全体は表示しない)

と言う違いがあります。
(実際にはawkでも全体を表示できますが、よくある使い方としての説明です。)

しかし実際のところ、awkはgrepと組み合わせて使うことが多いと思います。
 

それぞれの使い方

以下のようなファイルがあった時に、

# num.txt
111 222 333
444 555 666
777 888 999

 
grepは

$ grep '1' num.txt
111 222 333

のように、検索文字列に引っかかった行全体が表示されますが、
awkを使う場合には

$ awk '{print $1}' num.txt
111
444
777

 
のように各行の1カラム目だけを表示するような使い方ができます。

 

実際の使用例

ここまでは違いを強調するように説明しましたが、実際のところではawkとgrepは組み合わせて使うことが多いと思います。
例えば以下のようなログファイルがあったとして

# log.txt
Sep 19 00:01:20 connected from 1.2.3.4 method GET
Sep 19 00:01:21 disconnect
Sep 19 00:01:40 connected from 2.3.4.5 method GET
Sep 19 00:01:21 disconnect
Sep 19 00:02:10 connected from 3.4.5.6 method GET
Sep 19 00:02:21 disconnect

 
0時1分に接続があったIPを抽出したい という場合には以下のように実現できます。

$ grep '00:01:' log.txt | grep 'connected' | awk '{print $6}'
1.2.3.4
2.3.4.5

このように、grepの結果をパイプでawkに渡すことで、特定行の特定カラムだけを抽出するということができます。

 
(ちなみに余談ですが、上記はawkだけでも実現できたりします。)

$ awk '$3~/^00:01:/ && $4=="connected"{print $6}' log.txt
1.2.3.4
2.3.4.5

こちらはややこしいのでまた別途。

 

まとめ

以上、awkの簡単な使い方とgrepの違いについてでした。


スポンサーリンク




コメント一覧

2022/09/18 05:00:13

Wendellunapsさん

Whoa many of wonderful facts.
mexican pharmacies pharmacy prescription canadian pharmacy review


2022/09/18 15:47:16

JoeteOaOaさん

pof dating login
ourtime login uk
plentyoffish dating sites


2022/09/19 08:23:07

Wendellunapsさん

Incredible many of amazing advice!
canada drug pharmacy canada discount drug canadian pharmacy king


2022/09/19 15:10:59

AlvinJowさん

Thank you. I like it.
essay services college essays citing a website in essay


2022/09/20 04:07:47

AlvinJowさん

Thank you. I value it!
how to write analytical essay essay writer top essay writing service


2022/09/20 19:59:42

AlvinJowさん

Cheers, Valuable information.
me as a writer essay argumentative essay cheap article writing service


2022/09/20 20:34:59

DorolisaOaOaさん

national honor society essay help
buy essay paper
college essay writing company


2022/09/20 21:41:33

AlvinJowさん

With thanks, I like this.
great college essays what is good writing essay technical writing services


2022/09/21 01:40:31

Wendellunapsさん

Nicely put, Appreciate it!
canadian online pharmacy reviews canada medication pharmacy pharmacy online


2022/09/21 06:05:17

AlvinJowさん

Effectively voiced without a doubt. !
essay for college entrance https://theessayswriters.com/ websites that write essays


2022/09/21 09:07:25

AlvinJowさん

You expressed that terrifically.
help in essay writing essay writers freelance


2022/09/21 15:30:54

Wendellunapsさん

Amazing information. Many thanks.
canadian pharmacy pharmacy tech canadian pharcharmy online no precipitation


2022/09/21 17:02:57

Michaelpedさん

Helpful write ups. Regards!
prescription pricing buy prescription drugs without doctor online pharmacy without a prescription https://canadadrugspower.com


2022/09/21 18:44:16

MartyOaOaさん

best medical school essay editing service
best websites for essays
service essays


2022/09/21 22:14:44

AlvinJowさん

You actually suggested it wonderfully!
columbia college chicago essay make money writing essays business letter writing service


2022/09/22 06:59:06

Michaelpedさん

With thanks. Loads of forum posts!

canadian online pharmacies legitimate canadian viagra discount pharmacies https://canadianpharmacylist.com


2022/09/22 11:23:33

AlvinJowさん

Terrific content. Appreciate it.
quality custom essay https://freshapps.space/ writers for hire


2022/09/22 14:20:09

MerolaOaOaさん

college essay writing help
help me essays
essay homework help


2022/09/22 20:34:42

Michaelpedさん

Nicely put. Many thanks!
best canadian prescription prices canadian pharmacy 365 canada pharma limited https://canadianpharmacylist.com


2022/09/23 00:12:48

AlvinJowさん

You actually suggested it effectively.
custom essays cheap how to write contrast essay resume writing services online


2022/09/23 08:48:55

Wendellunapsさん

Amazing all kinds of helpful tips.
publix pharmacy online ordering ordering prescriptions from canada legally my canadian pharmacy


2022/09/23 10:07:48

Michaelpedさん

Nicely voiced of course. .
medicine online shopping canada drugs direct canadapharmacy https://canadapharmacyspace.com


2022/09/23 10:11:32

AshlenOaOaさん

order cheap essay
reviews of essay writing services
college essay helper


2022/09/24 13:33:34

Michaelpedさん

Thank you. A lot of posts.

meds online without doctor prescription canadian pharmacy 365 drugstore online https://canadapharmacyspace.com


2022/09/25 00:00:04

CharitaOaOaさん

best cheap essay
essay writing service review
best college application essay service


2022/09/25 03:17:44

Michaelpedさん

Awesome forum posts, Thanks a lot!
buy viagra pharmacy 100mg walgreens online pharmacy meds online without doctor prescription https://rxpharmacyteam.com


2022/09/25 07:56:27

Williamherさん

Tempo De Ensaio De Permeabilidade
Como Hacer Un Queque De Mexico
Esercizio Di Aldo Giovanni E Giacomo

here
5 erreurs de phase 1
this site
here
Resumo academico de textos
Les nouvelles varietes de materiaux
at this website
here
Startklar fur plot
Mis primeras clases de grandes filosofos
link
Clases de la guerra de vietnam
Truques e dicas de becas
7 tipps fur interviews
3e8f5a3


2022/09/25 13:09:03

AlvinJowさん

Wow plenty of helpful facts!
essay writers https://essaywriting4you.com/ best custom writing services


2022/09/25 19:15:22

NaniceOaOaさん

custom essay papers
essay writing service legal
essay assignment help


2022/09/26 14:24:46

ChelsaeOaOaさん

professional essay help
essay write service
essay proofreading service


2022/09/27 09:46:07

PennyOaOaさん

top essay writers
buy a essay
best essay for you


2022/09/28 03:39:41

Williamherさん

V Deo Para A Disciplina De Revisar
How To Outline Your Drawings On The Same Day
Cours Complet De Zumba Complet

link Un'ora di sempre da sephora Clase introduccion a ensayos de verde Parler de france en this site at this website Comment reussir les tests de prepa at this website link here link here at this website Wie man uber pflichten at this website here at this website Pratica de dancinhas da musica link at this website Corsi e scuole di sempre 5553ddc


2022/09/28 05:43:21

TammieOaOaさん

essays on service
discount essay writing service
custom essays essay help


2022/09/29 01:45:54

AntonieOaOaさん

essay on service to humanity
us essay writing service
your essay writer


2022/09/29 21:26:22

RhiamonOaOaさん

best essay writing service reviews
definition essay help
help on essay writing


2022/09/30 16:02:11

CharoOaOaさん

best essay review services
essay help 123
hire essay writer


2022/10/01 10:38:59

DronaOaOaさん

write my essay cheap
how to write my essay
the best essay writers


2022/10/02 06:22:57

TwylaOaOaさん

best writing essay
custom essays toronto
pay to write my essay


2022/07/08 14:09:48

AurlieOaOaさん

winning slots
infinity slots
eso quick slots youtube


2022/07/13 14:59:27

ShaynaOaOaさん

dissertation introduction
[url="https://accountingdissertationhelp.com"]best rated essay writing service[/url]
cheap dissertation writing


2022/07/19 22:35:05

ChandaOaOaさん

dissertation writing
dissertation help service
dissertation help


2022/07/21 00:19:51

AvivaOaOaさん

books and dissertation about mafa people
phd dissertation help
dissertation writing memes


2022/07/21 19:21:09

DortheaOaOaさん

dissertation writing plan
electronic engineering dissertation ideas
defending dissertation


2022/07/22 14:55:57

VerieeOaOaさん

dissertation defense
dissertation write
phd dissertation example


2022/07/23 14:35:34

GlynnisOaOaさん

doctoral dissertation help reference
dissertation crossword clue
writing your doctoral dissertation


2022/07/24 09:07:53

SuzannOaOaさん

dissertation proposal example
best dissertation writing service review
defending your dissertation


2022/07/25 03:44:46

SalliOaOaさん

dissertation writing services uk
writing a master's dissertation
dissertation writing plan


2022/07/25 16:02:51

TandiOaOaさん

best real money online casino
online casino games real money
free sign up bonus


2022/07/26 00:32:05

DarsieOaOaさん

rfp writing services
phd dissertation
paper writing service


2022/07/26 16:54:07

TandiOaOaさん

online casinos free bonuses
no deposit bonus casinos
best us online casinos


2022/07/26 22:35:19

MabelleOaOaさん

professional dissertation help
dissertation writing guide
dissertation title generator


2022/07/27 20:01:30

JenneeOaOaさん

dissertation writing tutor
tips for dissertation writing
phd dissertation help


2022/07/27 18:05:50

AbagaelOaOaさん

best online casino bonus
real money casino no deposit
sign up bonus no deposit casino


2022/07/28 16:20:14

HerminaOaOaさん

buy dissertations online
dissertation online help
dissertation review


2022/07/28 17:43:59

ShaylaOaOaさん

casino signup bonus no deposit
online us casinos
casino free money


2022/07/29 12:00:02

AudreOaOaさん

dissertation writers
dissertation introduction
top servis


2022/07/29 16:43:32

VerenaOaOaさん

best us online casinos
casino sign up bonus
real casino games


2022/07/30 07:44:08

HestiaOaOaさん

acknowledgements dissertation
dissertation title examples
buy dissertation online


2022/07/30 15:16:33

DixieOaOaさん

free casino bonus
bonus online casino
online casino us


2022/07/31 13:05:42

LaureenOaOaさん

online casino sign up bonuses
wild casino online
play blackjack online money


2022/08/01 13:44:22

FranniOaOaさん

no deposit bonus casinos
real cash online casino
casino win real money


2022/08/02 12:07:25

PearlOaOaさん

internet casino bonus
free signup bonus no deposit casino
internet casino bonus


2022/08/04 08:58:03

LeslieOaOaさん

play blackjack online money
blackjack payout
us online casinos


2022/08/07 08:41:49

TrudieOaOaさん

us casino
best casino sites
online casinos real money no deposit


2022/08/08 01:14:11

Ralphskakeさん

накрутка зрителей Twitch


2022/08/08 07:55:14

JaynellOaOaさん

gambling bonuses
online casino real money usa
win real money casino


2022/08/08 22:05:20

TeddieOaOaさん

tunnelbear vpn free
best netflix vpn
vpn best value


2022/08/09 15:32:06

DulceaOaOaさん

online real money casino
usa casinos on line
no deposit casino online


2022/08/10 06:11:40

JenneeOaOaさん

how does vpn work
best free windows vpn
best vpn service of 2022


2022/08/10 15:59:10

CarolanOaOaさん

real casino online
wild casino online
casino online bonus


2022/08/11 02:19:23

JenneeOaOaさん

free vpn proxy
best free vpn for roobet
buy pia vpn


2022/08/11 15:16:39

FanyaOaOaさん

online casino bonus
online us casinos
casino online real money


2022/08/11 22:55:43

AshleighOaOaさん

free vpn mac
vpn for windows free
windscribe vpn


2022/08/12 15:15:49

CarolinOaOaさん

best mobile casinos
online real casino
online casino game real money


2022/08/12 21:19:52

KelseyOaOaさん

free vpn proxy
hotspot vpn free
best ios vpn


2022/08/14 10:07:40

FifineOaOaさん

online casinos free bonuses
online casino welcome bonus
mobile casino online


2022/08/15 06:16:30

FifineOaOaさん

casino bonus
free cash no deposit casino
free no deposit casino


2022/08/15 20:02:48

SheelaghOaOaさん

best vpn location for warzone
vpn free download
free vpn for firestick


2022/08/16 05:29:02

GwenoreOaOaさん

online casinos free money
live online casino usa
blackjack app real money


2022/08/18 09:27:54

Piniwhipsさん

stromectol ebay Levetra


2022/08/24 00:14:07

GladiOaOaさん

gay/bi dating apps
best gay dating
gay farmers dating sites


2022/08/24 09:55:32

JoannOaOaさん

granny fuck dating
professional dating sites
dating web


2022/08/25 21:48:58

BertheOaOaさん

dating single
top dating sites in usa
dating personal ads


2022/08/26 18:50:32

KaitlynnOaOaさん

the dating game
free single dating online
best adult site


2022/08/27 16:03:07

DavinaOaOaさん

find singles dating
our time dating website login
matchmaking dating


2022/08/28 07:48:10

GladiOaOaさん

gay grandpa dating
turns to gay sex dating
gay dating advice - dating a much younger man or 18 yr old in high school


2022/08/28 14:12:56

ChelsaeOaOaさん

totally free dating sites near nelson
best internet dating sites
find single women


2022/08/29 02:54:54

JeraleeOaOaさん

dating advice for gay men
sado masocism dating gay site
gay dad dating game


2022/08/29 12:20:10

EmaliaOaOaさん

free date site
online dating app
100% free dating service


2022/08/29 22:22:24

KyrstinOaOaさん

gay bareback dating
whats the best sith for findina a gay dating site
free dating gay sites


2022/08/30 10:07:36

KarryOaOaさん

lstill18 single women
online dating game
casual dating sites


2022/08/30 18:42:21

CasandraOaOaさん

online real money casinos
casino no deposit
casino no deposit


2022/08/31 08:52:27

LettiOaOaさん

100% free dating service
match dating site
plenty fish


2022/08/31 14:01:22

AmataOaOaさん

play bingo for real money
blackjack real money
best usa casinos


2022/09/01 22:13:31

LettiOaOaさん

matchmaking dating
free local dating sites
zoosk dating


2022/09/02 03:59:10

gyclidaさん

cheapest cialis available For Viagra specifically, you ll want to visit the medication s official website to check and see if any assistance is available


2022/09/02 05:25:33

Inwableさん

п»їcialis This condition can be fatal if it s not treated in a timely manner


2022/09/02 07:32:29

GennieOaOaさん

gay kink chat
gay chat rooms on aol
free chat with men - live gay cams, free gay webcams at chaturbate


2022/09/03 03:13:44

DeeynOaOaさん

gay male cam chat
gay+chat
lady gaga gay chat emoji


2022/09/03 22:29:55

EmaleeOaOaさん

gay chat rooms free
utah gay bi chat
free live gay chat rooms


2022/09/04 09:42:31

oxysleltyさん

In the hall, Mrs how to take priligy


2022/09/04 11:38:55

TiesteRatさん

priligy online Check how long they ve been in business


2022/09/04 17:34:00

StephineOaOaさん

gay roleplay chat
dos chicos se conocieron en un chat gay
gay chat cam random


2022/09/05 16:35:35

Alvenruilaさん

Awesome content. Thank you. homework what to write a persuasive essay on


2022/09/06 08:27:42

attatryさん

buy cialis uk Works very well


2022/09/06 10:59:10

Caulapyさん

best site to buy cialis online With the Cialis online buying solution today, you can shop for any generic buy Cialis online pharmacy online, to find one that has the best price, the greatest quality, and is well known for helping men improve their sexual health


2022/09/07 03:42:00

Abrahamgonさん

You actually explained that perfectly! how to write a 5 paragraph essay help dissertation thesis writing service


2022/09/07 09:53:21

RobenaOaOaさん

free live gay webcam chat rooms
gay chat led roulette
local gay chat rooms


2022/09/08 16:39:52

Gesreenueさん

, Does Topical Finasteride Work cialis super active


2022/09/08 18:46:11

kapcreaspさん

A wearable sleeve on your penis, kind of like a wearable dildo cialis vs viagra


2022/09/10 01:13:22

TallouOaOaさん

bing gay chat rooms
where to chat with gay pervs
gay webcam chat


2022/09/10 21:22:23

MarilinOaOaさん

free live gay muscle chat
free gay and bi mens chat
free gay chat lines


2022/09/13 19:06:52

BruceCandyさん

Wonderful material. Cheers.
online pharmacies uk prescription drugs from canada ordering prescriptions from canada legally


2022/09/14 07:49:48

Wendellunapsさん

Regards, A lot of facts.

canadian rx pharmacy online walmart pharmacy viagra pharmacy cost comparison


2022/09/14 08:32:14

BruceCandyさん

You actually suggested this wonderfully.
canadian rx pharmacy online canadian pharmaceuticals buy cialis


2022/09/14 11:30:57

Gresocaさん

As stated by Reiter and colleagues 86 Melatonin is per se neither antigonadotrophic nor progonadotrophic. clomid and nolvadex pct


2022/09/14 13:05:04

MavisOaOaさん

dating personals
chatting dating site
free dating online dating


2022/09/14 19:19:54

AlvinJowさん

Appreciate it. Lots of posts.

help writing a essay https://ouressays.com/ write my dissertation for me


2022/09/15 07:57:39

AlvinJowさん

Truly many of awesome knowledge!
essay about writing how to write dialogue in an essay master thesis writing service


2022/09/15 10:24:08

Wendellunapsさん

Thanks a lot. Excellent stuff!
medicine online order online drugstore buy medication without an rx


2022/09/15 10:37:14

AlvinJowさん

You reported it very well!
successful college essays essays writing service custom essay writing service


2022/09/15 14:26:57

JuanitaOaOaさん

zoosk dating
free dating site chatting
online marriage sites in usa


2022/09/16 01:03:05

AlvinJowさん

Many thanks, Excellent stuff!
how to write a narrative essay about yourself essays writing services essay writing website


2022/09/16 09:34:22

AlvinJowさん

Cheers! Numerous stuff.

help me write my college essay essays on writing professional resume writing service


2022/09/16 12:16:30

AlvinJowさん

Wonderful data. Cheers!
great college essays what to write about in a college essay essay writing service recommendation


2022/09/16 15:19:49

MaisieOaOaさん

best dating online site
asians dating site
asian dating chat


2022/09/17 16:23:33

EachelleOaOaさん

find online dating
topchatsites
free free dating sites


2022/09/17 17:15:11

Michaelpedさん

Terrific data, Many thanks.
pharmacies shipping to usa walmart pharmacy price check discount pharmacies https://rxpharmacyteam.com


2022/09/18 00:16:04

AlvinJowさん

Regards, Valuable information!
how to write a cause effect essay https://flowleadsua.com/ best essay writing websites


2022/09/18 02:08:12

NimeTrikeさん

doxycycline hyclate for std Leuprolide is a gonadotropin- releasing hormone GnRH analogue that functions as a GnRH receptor superagonist.


2022/09/18 04:06:01

Beeplelicさん

doxycycline malaria bestellen online Gamma- retroviral and lentiviral- based delivery result in high expression and transduction efficiencies, but these have a limited payload 8 kilobases kb and pose the theoretical risk of insertional oncogenesis 219.



コメントを投稿する


お名前


コメント内容





TOP back