SlideShare ist ein Scribd-Unternehmen logo
1 von 21
Downloaden Sie, um offline zu lesen
Learn AWK
in 15 minutes
Maxime Besson
info@worteks.com
2
(vɔʁ.tɛks)
Services
Heterogeneous and complex
infrastructures, cloud, mail,
authentication, security

Studies, audit and consulting

Technical expertise

Technical support

Training

R&D
Edition
Collaboration and
application portal
Mutualized platform
for development
Identity and Access
Management
Partnership
Why learn AWK in $(date +%Y)?
•
It’s everywhere:
# Yes, everywhere:
$ docker run -it --rm alpine awk 'END{print "Hello"}' /dev/null
Hello
•
It’s powerful and versatile
•
It plays nicely with many other command line tools
•
This:
– https://adamdrake.com/command-line-tools-can-be-235x-
faster-than-your-hadoop-cluster.html
What I'm going to teach you
awk -F: '($3<1000){s++} END{print s}'
•
Don't worry, it looks scarier than it really is !
•
If you understand what this does, you can sleep until
the next talk !
Structure of an AWK command
awk [program arguments] code [input file]
•
Program arguments change how the interpreter works
•
Code is an awk script, usually quote-protected
•
Input file is optional, awk will then read the standard
input
•
Awk writes to standard output
•
You can put the script in a file if it gets too big for a one-
liner
What AWK does (pseudo-code)
foreach line
if (CONDITION) then
ACTIONS
endif
if (CONDITION) then
ACTIONS
endif
…
endfor
What you write (real AWK code)
CONDITION {
ACTIONS
}
CONDITION {
ACTIONS
}
What you write (one-liner)
awk ‘CONDITION {ACTIONS} CONDITION {ACTIONS}’
AWK conditions
•
if CONDITION empty, INSTRUCTIONS are applied to
every line
•
BEGIN / END : will be run only once
•
/regexp/
•
( expression ) : usually a test for equality
•
some others...
•
Any logical combination of the above
AWK variables
•
AWK allows you to set any variable you like
•
AWK defines some variables for you:
– NR : current „record” (line) number
– NF : how many „fields” the record has
– $0 : the current line
– $1, $2, … : n-th field on the line
Fields are delimited by tabs and spaces by default, this can
be overriden with the -F option
•
AWK is great at handling CSV files !
That's all...
•
Now you know (almost) all the theory. The rest is in man
pages
Examples
awk '{print NR}'
Examples
awk '{print NR}'
awk '{print NR, $0}'
Examples
awk '{print NR}'
awk '{print NF}'
awk 'END {print NR}'
Examples
awk '{print NR}'
awk '{print NF}'
awk 'END {print NR}'
awk ' /root/ { print $0 }'
Examples
awk -F: ' /root/ { print $3 }' /etc/passwd
Examples
awk -F: ' /root/ { print $3 }' /etc/passwd
awk -F: ' ($3==1000) { print $1 }' /etc/passwd
Examples
awk -F: ' /root/ { print $3 }' /etc/passwd
awk -F: ' ($3==1000) { print $1 }' /etc/passwd
awk -F: ' ($1==”root”) { print $6 }' /etc/passwd
Examples
awk -F: ' /root/ { print $3 }' /etc/passwd
awk -F: ' ($3==1000) { print $1 }' /etc/passwd
awk -F: ' ($1==”root”) { print $6 }' /etc/passwd
awk -F: '($6 ~ "^/home"){ c++ } END{print c}'
Conclusion
•
When should I use awk
– Shell one liners
– One shot processing of CSV data
●
'($2 == something) {sum = sum+ $3}'
●
group-by using arrays
•
When should I not use awk
– Internationalization
– Complex calculations
– Complex data structures
– Long scripts in general
22
Thanks for your
attention
More informations:
info@worteks.com
@worteks_com
linkedin.com/company/worteks

Weitere ähnliche Inhalte

Ähnlich wie [POSS 2019] Learn AWK in 15 minutes

Itb session v_memcached
Itb session v_memcachedItb session v_memcached
Itb session v_memcached
Skills Matter
 
Drupal Camp Brighton 2015: Ansible Drupal Medicine show
Drupal Camp Brighton 2015: Ansible Drupal Medicine showDrupal Camp Brighton 2015: Ansible Drupal Medicine show
Drupal Camp Brighton 2015: Ansible Drupal Medicine show
George Boobyer
 
Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013
Simon McCartney
 
Sparkling Water 5 28-14
Sparkling Water 5 28-14Sparkling Water 5 28-14
Sparkling Water 5 28-14
Sri Ambati
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con Rails
Svet Ivantchev
 
The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable Code
Baidu, Inc.
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
Dr.Ravi
 

Ähnlich wie [POSS 2019] Learn AWK in 15 minutes (20)

Itb session v_memcached
Itb session v_memcachedItb session v_memcached
Itb session v_memcached
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
 
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine YardHow I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
How I Learned to Stop Worrying and Love the Cloud - Wesley Beary, Engine Yard
 
An Introduction to Windows PowerShell
An Introduction to Windows PowerShellAn Introduction to Windows PowerShell
An Introduction to Windows PowerShell
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - Summary
 
Quick trip around the Cosmos - Things every astronaut supposed to know
Quick trip around the Cosmos - Things every astronaut supposed to knowQuick trip around the Cosmos - Things every astronaut supposed to know
Quick trip around the Cosmos - Things every astronaut supposed to know
 
Docker Security workshop slides
Docker Security workshop slidesDocker Security workshop slides
Docker Security workshop slides
 
Terraform in deployment pipeline
Terraform in deployment pipelineTerraform in deployment pipeline
Terraform in deployment pipeline
 
Perly Parallel Processing of Fixed Width Data Records
Perly Parallel Processing of Fixed Width Data RecordsPerly Parallel Processing of Fixed Width Data Records
Perly Parallel Processing of Fixed Width Data Records
 
Running Docker clusters on AWS (November 2016)
Running Docker clusters on AWS (November 2016)Running Docker clusters on AWS (November 2016)
Running Docker clusters on AWS (November 2016)
 
Juggling Chainsaws: Perl and MongoDB
Juggling Chainsaws: Perl and MongoDBJuggling Chainsaws: Perl and MongoDB
Juggling Chainsaws: Perl and MongoDB
 
#PDR15 - waf, wscript and Your Pebble App
#PDR15 - waf, wscript and Your Pebble App#PDR15 - waf, wscript and Your Pebble App
#PDR15 - waf, wscript and Your Pebble App
 
Paexec — distributes tasks over network or CPUs
Paexec — distributes tasks over network or CPUsPaexec — distributes tasks over network or CPUs
Paexec — distributes tasks over network or CPUs
 
Aprovisionamiento multi-proveedor con Terraform - Plain Concepts DevOps day
Aprovisionamiento multi-proveedor con Terraform  - Plain Concepts DevOps dayAprovisionamiento multi-proveedor con Terraform  - Plain Concepts DevOps day
Aprovisionamiento multi-proveedor con Terraform - Plain Concepts DevOps day
 
Drupal Camp Brighton 2015: Ansible Drupal Medicine show
Drupal Camp Brighton 2015: Ansible Drupal Medicine showDrupal Camp Brighton 2015: Ansible Drupal Medicine show
Drupal Camp Brighton 2015: Ansible Drupal Medicine show
 
Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013Stack kicker devopsdays-london-2013
Stack kicker devopsdays-london-2013
 
Sparkling Water 5 28-14
Sparkling Water 5 28-14Sparkling Water 5 28-14
Sparkling Water 5 28-14
 
Interface de Voz con Rails
Interface de Voz con RailsInterface de Voz con Rails
Interface de Voz con Rails
 
The Art Of Readable Code
The Art Of Readable CodeThe Art Of Readable Code
The Art Of Readable Code
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 

Mehr von Worteks

Mehr von Worteks (20)

[Open Source Experience 2021] Une infrastructure Cloud et une solution IDaaS ...
[Open Source Experience 2021] Une infrastructure Cloud et une solution IDaaS ...[Open Source Experience 2021] Une infrastructure Cloud et une solution IDaaS ...
[Open Source Experience 2021] Une infrastructure Cloud et une solution IDaaS ...
 
[Identity Days 2021] W'IDaaS - Identity as a Service
[Identity Days 2021] W'IDaaS - Identity as a Service[Identity Days 2021] W'IDaaS - Identity as a Service
[Identity Days 2021] W'IDaaS - Identity as a Service
 
[Identity Days 2021] Quel avenir pour OpenLDAP ?
[Identity Days 2021] Quel avenir pour OpenLDAP ?[Identity Days 2021] Quel avenir pour OpenLDAP ?
[Identity Days 2021] Quel avenir pour OpenLDAP ?
 
[Pass the SALT 2021] Hosting Identity in the Cloud with free softwares
[Pass the SALT 2021] Hosting Identity in the Cloud with free softwares[Pass the SALT 2021] Hosting Identity in the Cloud with free softwares
[Pass the SALT 2021] Hosting Identity in the Cloud with free softwares
 
[OW2con'21] Hosting Identity in the Cloud with OW2 free softwares
[OW2con'21] Hosting Identity in the Cloud with OW2 free softwares[OW2con'21] Hosting Identity in the Cloud with OW2 free softwares
[OW2con'21] Hosting Identity in the Cloud with OW2 free softwares
 
[AFUP Lyon 2021] LDAP Tool Box Self Service Password
[AFUP Lyon 2021] LDAP Tool Box Self Service Password[AFUP Lyon 2021] LDAP Tool Box Self Service Password
[AFUP Lyon 2021] LDAP Tool Box Self Service Password
 
[OpenDay 2021] Logiciel libre, entreprises et modèles économiques
[OpenDay 2021] Logiciel libre, entreprises et modèles économiques[OpenDay 2021] Logiciel libre, entreprises et modèles économiques
[OpenDay 2021] Logiciel libre, entreprises et modèles économiques
 
[Campus du Libre 2020] Présentation de la solution W'Sweet
[Campus du Libre 2020] Présentation de la solution W'Sweet[Campus du Libre 2020] Présentation de la solution W'Sweet
[Campus du Libre 2020] Présentation de la solution W'Sweet
 
[Identity Days 2020] Politique des mots de passe des annuaires LDAP et outils...
[Identity Days 2020] Politique des mots de passe des annuaires LDAP et outils...[Identity Days 2020] Politique des mots de passe des annuaires LDAP et outils...
[Identity Days 2020] Politique des mots de passe des annuaires LDAP et outils...
 
[Université Lyon 1] Exemples de logiciels libres : LemonLDAP::NG et W'Sweet
[Université Lyon 1] Exemples de logiciels libres : LemonLDAP::NG et W'Sweet[Université Lyon 1] Exemples de logiciels libres : LemonLDAP::NG et W'Sweet
[Université Lyon 1] Exemples de logiciels libres : LemonLDAP::NG et W'Sweet
 
[Pass the SALT 2020] Understand password policy in OpenLDAP and discover tool...
[Pass the SALT 2020] Understand password policy in OpenLDAP and discover tool...[Pass the SALT 2020] Understand password policy in OpenLDAP and discover tool...
[Pass the SALT 2020] Understand password policy in OpenLDAP and discover tool...
 
[OW2online 2020] LDAP Synchronization Connector
[OW2online 2020] LDAP Synchronization Connector[OW2online 2020] LDAP Synchronization Connector
[OW2online 2020] LDAP Synchronization Connector
 
[Aperhologramme 2020] Comment faire du logiciel libre ?
[Aperhologramme 2020] Comment faire du logiciel libre ?[Aperhologramme 2020] Comment faire du logiciel libre ?
[Aperhologramme 2020] Comment faire du logiciel libre ?
 
[POSS 2019] OVirt and Ceph: Perfect Combination.?
[POSS 2019] OVirt and  Ceph: Perfect Combination.?[POSS 2019] OVirt and  Ceph: Perfect Combination.?
[POSS 2019] OVirt and Ceph: Perfect Combination.?
 
[POSS 2019] MicroServices authentication and authorization with LemonLDAP::NG
[POSS 2019] MicroServices authentication and authorization with LemonLDAP::NG[POSS 2019] MicroServices authentication and authorization with LemonLDAP::NG
[POSS 2019] MicroServices authentication and authorization with LemonLDAP::NG
 
[POSS 2019] TLS for Dummies
[POSS 2019] TLS for Dummies[POSS 2019] TLS for Dummies
[POSS 2019] TLS for Dummies
 
[LDAPCon 2019] LemonLDAP::NG 2.0: Mutli-factor authentication, Identity Feder...
[LDAPCon 2019] LemonLDAP::NG 2.0: Mutli-factor authentication, Identity Feder...[LDAPCon 2019] LemonLDAP::NG 2.0: Mutli-factor authentication, Identity Feder...
[LDAPCon 2019] LemonLDAP::NG 2.0: Mutli-factor authentication, Identity Feder...
 
[LDAPCon 2019] The FusionIAM initiative
[LDAPCon 2019] The FusionIAM initiative[LDAPCon 2019] The FusionIAM initiative
[LDAPCon 2019] The FusionIAM initiative
 
[Identity Days 2019] Maîtrisez les accès à vos applications Web (Cloud et On...
[Identity Days 2019]  Maîtrisez les accès à vos applications Web (Cloud et On...[Identity Days 2019]  Maîtrisez les accès à vos applications Web (Cloud et On...
[Identity Days 2019] Maîtrisez les accès à vos applications Web (Cloud et On...
 
[RedHat Forum 2019] REX - COMMENT MONTER UNE OFFRE DE CLOUD EN MARQUE BLANCHE...
[RedHat Forum 2019] REX - COMMENT MONTER UNE OFFRE DE CLOUD EN MARQUE BLANCHE...[RedHat Forum 2019] REX - COMMENT MONTER UNE OFFRE DE CLOUD EN MARQUE BLANCHE...
[RedHat Forum 2019] REX - COMMENT MONTER UNE OFFRE DE CLOUD EN MARQUE BLANCHE...
 

Kürzlich hochgeladen

Kürzlich hochgeladen (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Ransomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdfRansomware_Q4_2023. The report. [EN].pdf
Ransomware_Q4_2023. The report. [EN].pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

[POSS 2019] Learn AWK in 15 minutes

  • 1. Learn AWK in 15 minutes Maxime Besson info@worteks.com
  • 2. 2 (vɔʁ.tɛks) Services Heterogeneous and complex infrastructures, cloud, mail, authentication, security  Studies, audit and consulting  Technical expertise  Technical support  Training  R&D Edition Collaboration and application portal Mutualized platform for development Identity and Access Management Partnership
  • 3. Why learn AWK in $(date +%Y)? • It’s everywhere: # Yes, everywhere: $ docker run -it --rm alpine awk 'END{print "Hello"}' /dev/null Hello • It’s powerful and versatile • It plays nicely with many other command line tools • This: – https://adamdrake.com/command-line-tools-can-be-235x- faster-than-your-hadoop-cluster.html
  • 4. What I'm going to teach you awk -F: '($3<1000){s++} END{print s}' • Don't worry, it looks scarier than it really is ! • If you understand what this does, you can sleep until the next talk !
  • 5. Structure of an AWK command awk [program arguments] code [input file] • Program arguments change how the interpreter works • Code is an awk script, usually quote-protected • Input file is optional, awk will then read the standard input • Awk writes to standard output • You can put the script in a file if it gets too big for a one- liner
  • 6. What AWK does (pseudo-code) foreach line if (CONDITION) then ACTIONS endif if (CONDITION) then ACTIONS endif … endfor
  • 7. What you write (real AWK code) CONDITION { ACTIONS } CONDITION { ACTIONS }
  • 8. What you write (one-liner) awk ‘CONDITION {ACTIONS} CONDITION {ACTIONS}’
  • 9. AWK conditions • if CONDITION empty, INSTRUCTIONS are applied to every line • BEGIN / END : will be run only once • /regexp/ • ( expression ) : usually a test for equality • some others... • Any logical combination of the above
  • 10. AWK variables • AWK allows you to set any variable you like • AWK defines some variables for you: – NR : current „record” (line) number – NF : how many „fields” the record has – $0 : the current line – $1, $2, … : n-th field on the line Fields are delimited by tabs and spaces by default, this can be overriden with the -F option • AWK is great at handling CSV files !
  • 11. That's all... • Now you know (almost) all the theory. The rest is in man pages
  • 13. Examples awk '{print NR}' awk '{print NR, $0}'
  • 14. Examples awk '{print NR}' awk '{print NF}' awk 'END {print NR}'
  • 15. Examples awk '{print NR}' awk '{print NF}' awk 'END {print NR}' awk ' /root/ { print $0 }'
  • 16. Examples awk -F: ' /root/ { print $3 }' /etc/passwd
  • 17. Examples awk -F: ' /root/ { print $3 }' /etc/passwd awk -F: ' ($3==1000) { print $1 }' /etc/passwd
  • 18. Examples awk -F: ' /root/ { print $3 }' /etc/passwd awk -F: ' ($3==1000) { print $1 }' /etc/passwd awk -F: ' ($1==”root”) { print $6 }' /etc/passwd
  • 19. Examples awk -F: ' /root/ { print $3 }' /etc/passwd awk -F: ' ($3==1000) { print $1 }' /etc/passwd awk -F: ' ($1==”root”) { print $6 }' /etc/passwd awk -F: '($6 ~ "^/home"){ c++ } END{print c}'
  • 20. Conclusion • When should I use awk – Shell one liners – One shot processing of CSV data ● '($2 == something) {sum = sum+ $3}' ● group-by using arrays • When should I not use awk – Internationalization – Complex calculations – Complex data structures – Long scripts in general
  • 21. 22 Thanks for your attention More informations: info@worteks.com @worteks_com linkedin.com/company/worteks