SlideShare a Scribd company logo
UNIX - awk




        Data extraction and
     formatted Reporting Tool



               Presentation By

                         Nihar R Paital
Introduction

   Developer          : Alfred Aho
                         Peter Weinberger
                         Brian Kernighan

   Appears in         : Version 7 UNIX onwards

   Developed during   : 1970 s

   Developed at       : Bell Labs

   Category           : UNIX Utility

   Supported by       : All UNIX flavors         Nihar R Paital
Definition


 The AWK utility is a data extraction and
 reporting tool that uses a data-driven
 scripting language consisting of a set of
 actions to be taken against textual data
 (either in files or data streams) for the
 purpose of producing formatted reports.


                               Nihar R Paital
It performs basic text formatting on an input
stream ( A file / input from a pipeline )

 Formatting using input file
$ awk {print $n} Filename
Example:
$ awk {print $1} awk.txt > awk.txt.bak

 Formatting using a filter in a pipeline
$ generate_data | awk {print $1}
Example:
$ cat awk.txt | awk {print $1} > awk.txt.bak


Before proceeding to next slide please create a file named awk.txt with following Contents.

07.46.199.184 [28/Sep/2010:04:08:20] "GET /robots.txt HTTP/1.1" 200 0 "msnbot"
123.125.71.19 [28/Sep/2010:04:20:11] "GET / HTTP/1.1" 304 - "Baiduspider"


                                                                        Nihar R Paital
Basic but important for awk

   Syntax :
        awk {print $n} filename
        Generate data : awk {print $n}

   Awk programs will start with a "{" and end with a "}"

   $0 is the entire line

   Awk parses the line in to fields for you automatically, using any whitespace
    (space, tab) as a delimiter.

   Fields of a regular file will be available using $1,$2,$3 … etc

   NF : It is a special Variable contains the number of fields in the current line. We
    can print the last field by printing the field $NF
   NR : It prints the row number being currently processed.          Nihar R Paital
Basic Examples

 $ awk '{print $0}' awk.txt
 It will print all the lines as they are in File
 $ echo 'this is a test' | awk '{print $3}'
  It will print 'a'
 $ echo 'this is a test' | awk '{print $NF}'
  It prints "test"
 $ awk '{print $1, $(NF-2) }' awk.txt
 It will print the last 3rd word of file awk.txt
 $ awk '{print NR ") " $1 " -> " $(NF-2)}‘
 Output:
      1) 07.46.199.184 -> 200
      2) 123.125.71.19 -> 304
                                                   Nihar R Paital
Advance use of AWK
$ awk '{print $2}' logs.txt
Output:
    [28/Sep/2010:04:08:20]
    [28/Sep/2010:04:20:11]
The date field is separated by "/" and ":" characters.
Suppose I want to print like
[28/Sep/2010
[28/Sep/2010

$ awk '{print $2}' logs.txt | awk 'BEGIN{FS=":"}{print $1}'
Output:
    [28/Sep/2010
    [28/Sep/2010
Here FS=“:” means Field Separator as colon(:)

$ awk '{print $2}' logs.txt | awk 'BEGIN{FS=":"}{print $1}' | sed 's/[//'
Output:
    28/Sep/2010
    28/Sep/2010
Here We are Substituting [ with NULL value                  Nihar R Paital
Advance Use of AWK
If I want to return only the 200 status lines
$ awk '{if ($(NF-2) == "200") {print $0}}' logs.txt

   Output:
   07.46.199.184 [28/Sep/2010:04:08:20] "GET /robots.txt HTTP/1.1" 200 0 "msnbot"


$ awk '{a+=$(NF-2); print "Total so far:", a}' logs.txt

   Output:
   Total so far: 200
   Total so far: 504


$ awk '{a+=$(NF-2)}END{print "Total:", a}' logs.txt

   Output:
   Total: 504
                                                                  Nihar R Paital
Nihar R Paital

More Related Content

What's hot

Stack c6
Stack c6Stack c6
Stack c6
Omar Al-Sabek
 
Our challenge for Bulkload reliability improvement
Our challenge for Bulkload reliability  improvementOur challenge for Bulkload reliability  improvement
Our challenge for Bulkload reliability improvement
Satoshi Akama
 
Python Programming for ArcGIS: Part II
Python Programming for ArcGIS: Part IIPython Programming for ArcGIS: Part II
Python Programming for ArcGIS: Part II
DUSPviz
 
Live coding java 8 urs peter
Live coding java 8   urs peterLive coding java 8   urs peter
Live coding java 8 urs peter
NLJUG
 
An Introduction to Reactive Cocoa
An Introduction to Reactive CocoaAn Introduction to Reactive Cocoa
An Introduction to Reactive Cocoa
SmartLogic
 
Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...
Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...
Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...
Modern Data Stack France
 
Dutch hug
Dutch hugDutch hug
Dutch hug
guest51f8ea
 
Unix interview questions
Unix interview questionsUnix interview questions
Unix interview questions
Kalyan Hadoop
 
Simseer.com - Malware Similarity and Clustering Made Easy
Simseer.com - Malware Similarity and Clustering Made EasySimseer.com - Malware Similarity and Clustering Made Easy
Simseer.com - Malware Similarity and Clustering Made Easy
Silvio Cesare
 
spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21
Anton Kasyanov
 
ELK - from zero to coding class hero
ELK - from zero to coding class heroELK - from zero to coding class hero
ELK - from zero to coding class hero
JosipKovaek
 
Streaming data to s3 using akka streams
Streaming data to s3 using akka streamsStreaming data to s3 using akka streams
Streaming data to s3 using akka streams
Mikhail Girkin
 

What's hot (12)

Stack c6
Stack c6Stack c6
Stack c6
 
Our challenge for Bulkload reliability improvement
Our challenge for Bulkload reliability  improvementOur challenge for Bulkload reliability  improvement
Our challenge for Bulkload reliability improvement
 
Python Programming for ArcGIS: Part II
Python Programming for ArcGIS: Part IIPython Programming for ArcGIS: Part II
Python Programming for ArcGIS: Part II
 
Live coding java 8 urs peter
Live coding java 8   urs peterLive coding java 8   urs peter
Live coding java 8 urs peter
 
An Introduction to Reactive Cocoa
An Introduction to Reactive CocoaAn Introduction to Reactive Cocoa
An Introduction to Reactive Cocoa
 
Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...
Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...
Hadoop meetup : HUGFR Construire le cluster le plus rapide pour l'analyse des...
 
Dutch hug
Dutch hugDutch hug
Dutch hug
 
Unix interview questions
Unix interview questionsUnix interview questions
Unix interview questions
 
Simseer.com - Malware Similarity and Clustering Made Easy
Simseer.com - Malware Similarity and Clustering Made EasySimseer.com - Malware Similarity and Clustering Made Easy
Simseer.com - Malware Similarity and Clustering Made Easy
 
spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21spaCy lightning talk for KyivPy #21
spaCy lightning talk for KyivPy #21
 
ELK - from zero to coding class hero
ELK - from zero to coding class heroELK - from zero to coding class hero
ELK - from zero to coding class hero
 
Streaming data to s3 using akka streams
Streaming data to s3 using akka streamsStreaming data to s3 using akka streams
Streaming data to s3 using akka streams
 

Similar to Unix - Class7 - awk

Awk programming
Awk programming Awk programming
Awk programming
Dr.M.Karthika parthasarathy
 
awk_intro.ppt
awk_intro.pptawk_intro.ppt
awk_intro.ppt
PrasadReddy710753
 
Linux class 15 26 oct 2021
Linux class 15   26 oct 2021Linux class 15   26 oct 2021
Linux class 15 26 oct 2021
Khawar Nehal khawar.nehal@atrc.net.pk
 
Unix day4 v1.3
Unix day4 v1.3Unix day4 v1.3
Unix day4 v1.3
xavier john
 
Awk primer and Bioawk
Awk primer and BioawkAwk primer and Bioawk
Awk primer and Bioawk
Hoffman Lab
 
Daq toolbox examples_matlab
Daq toolbox examples_matlabDaq toolbox examples_matlab
Daq toolbox examples_matlab
Arihant Jain
 
NS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt IIINS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt III
Ajit Nayak
 
101 3.2 process text streams using filters
101 3.2 process text streams using filters101 3.2 process text streams using filters
101 3.2 process text streams using filters
Acácio Oliveira
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
root_fibo
 
Linux intro 5 extra: awk
Linux intro 5 extra: awkLinux intro 5 extra: awk
Linux intro 5 extra: awk
Giovanni Marco Dall'Olio
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
Sanjay Saluth
 
Awk A Pattern Scanning And Processing Language
Awk   A Pattern Scanning And Processing LanguageAwk   A Pattern Scanning And Processing Language
Awk A Pattern Scanning And Processing Language
Crystal Sanchez
 
Linux
LinuxLinux
Linux
LinuxLinux
Linux
LinuxLinux
Linux
LinuxLinux
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
Linux com
Linux comLinux com
Awk Introduction
Awk IntroductionAwk Introduction
Awk Introduction
Anthony Magee
 
What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?
Miklos Christine
 

Similar to Unix - Class7 - awk (20)

Awk programming
Awk programming Awk programming
Awk programming
 
awk_intro.ppt
awk_intro.pptawk_intro.ppt
awk_intro.ppt
 
Linux class 15 26 oct 2021
Linux class 15   26 oct 2021Linux class 15   26 oct 2021
Linux class 15 26 oct 2021
 
Unix day4 v1.3
Unix day4 v1.3Unix day4 v1.3
Unix day4 v1.3
 
Awk primer and Bioawk
Awk primer and BioawkAwk primer and Bioawk
Awk primer and Bioawk
 
Daq toolbox examples_matlab
Daq toolbox examples_matlabDaq toolbox examples_matlab
Daq toolbox examples_matlab
 
NS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt IIINS2: AWK and GNUplot - PArt III
NS2: AWK and GNUplot - PArt III
 
101 3.2 process text streams using filters
101 3.2 process text streams using filters101 3.2 process text streams using filters
101 3.2 process text streams using filters
 
Unit 8 text processing tools
Unit 8 text processing toolsUnit 8 text processing tools
Unit 8 text processing tools
 
Linux intro 5 extra: awk
Linux intro 5 extra: awkLinux intro 5 extra: awk
Linux intro 5 extra: awk
 
Unix Tutorial
Unix TutorialUnix Tutorial
Unix Tutorial
 
Awk A Pattern Scanning And Processing Language
Awk   A Pattern Scanning And Processing LanguageAwk   A Pattern Scanning And Processing Language
Awk A Pattern Scanning And Processing Language
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Linux com
Linux comLinux com
Linux com
 
Awk Introduction
Awk IntroductionAwk Introduction
Awk Introduction
 
What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?What's new with Apache Spark's Structured Streaming?
What's new with Apache Spark's Structured Streaming?
 

More from Nihar Ranjan Paital

Oracle Select Query
Oracle Select QueryOracle Select Query
Oracle Select Query
Nihar Ranjan Paital
 
Useful macros and functions for excel
Useful macros and functions for excelUseful macros and functions for excel
Useful macros and functions for excel
Nihar Ranjan Paital
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
Nihar Ranjan Paital
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
Nihar Ranjan Paital
 
UNIX - Class2 - vi Editor
UNIX - Class2 - vi EditorUNIX - Class2 - vi Editor
UNIX - Class2 - vi Editor
Nihar Ranjan Paital
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
Nihar Ranjan Paital
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
Nihar Ranjan Paital
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
Nihar Ranjan Paital
 
Test funda
Test fundaTest funda
Csql for telecom
Csql for telecomCsql for telecom
Csql for telecom
Nihar Ranjan Paital
 
Select Operations in CSQL
Select Operations in CSQLSelect Operations in CSQL
Select Operations in CSQL
Nihar Ranjan Paital
 

More from Nihar Ranjan Paital (11)

Oracle Select Query
Oracle Select QueryOracle Select Query
Oracle Select Query
 
Useful macros and functions for excel
Useful macros and functions for excelUseful macros and functions for excel
Useful macros and functions for excel
 
UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2UNIX - Class5 - Advance Shell Scripting-P2
UNIX - Class5 - Advance Shell Scripting-P2
 
UNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming ConstructsUNIX - Class3 - Programming Constructs
UNIX - Class3 - Programming Constructs
 
UNIX - Class2 - vi Editor
UNIX - Class2 - vi EditorUNIX - Class2 - vi Editor
UNIX - Class2 - vi Editor
 
UNIX - Class1 - Basic Shell
UNIX - Class1 - Basic ShellUNIX - Class1 - Basic Shell
UNIX - Class1 - Basic Shell
 
UNIX - Class6 - sed - Detail
UNIX - Class6 - sed - DetailUNIX - Class6 - sed - Detail
UNIX - Class6 - sed - Detail
 
UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1UNIX - Class4 - Advance Shell Scripting-P1
UNIX - Class4 - Advance Shell Scripting-P1
 
Test funda
Test fundaTest funda
Test funda
 
Csql for telecom
Csql for telecomCsql for telecom
Csql for telecom
 
Select Operations in CSQL
Select Operations in CSQLSelect Operations in CSQL
Select Operations in CSQL
 

Recently uploaded

Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
Safe Software
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
Miro Wengner
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
Antonios Katsarakis
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
Ivo Velitchkov
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
LizaNolte
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
c5vrf27qcz
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
saastr
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
Fwdays
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 

Recently uploaded (20)

Essentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation ParametersEssentials of Automations: Exploring Attributes & Automation Parameters
Essentials of Automations: Exploring Attributes & Automation Parameters
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 
JavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green MasterplanJavaLand 2024: Application Development Green Masterplan
JavaLand 2024: Application Development Green Masterplan
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
Dandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity serverDandelion Hashtable: beyond billion requests per second on a commodity server
Dandelion Hashtable: beyond billion requests per second on a commodity server
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
 
Apps Break Data
Apps Break DataApps Break Data
Apps Break Data
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham HillinQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
inQuba Webinar Mastering Customer Journey Management with Dr Graham Hill
 
Y-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PPY-Combinator seed pitch deck template PP
Y-Combinator seed pitch deck template PP
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
9 CEO's who hit $100m ARR Share Their Top Growth Tactics Nathan Latka, Founde...
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota"Choosing proper type of scaling", Olena Syrota
"Choosing proper type of scaling", Olena Syrota
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 

Unix - Class7 - awk

  • 1. UNIX - awk Data extraction and formatted Reporting Tool Presentation By Nihar R Paital
  • 2. Introduction  Developer : Alfred Aho Peter Weinberger Brian Kernighan  Appears in : Version 7 UNIX onwards  Developed during : 1970 s  Developed at : Bell Labs  Category : UNIX Utility  Supported by : All UNIX flavors Nihar R Paital
  • 3. Definition The AWK utility is a data extraction and reporting tool that uses a data-driven scripting language consisting of a set of actions to be taken against textual data (either in files or data streams) for the purpose of producing formatted reports. Nihar R Paital
  • 4. It performs basic text formatting on an input stream ( A file / input from a pipeline )  Formatting using input file $ awk {print $n} Filename Example: $ awk {print $1} awk.txt > awk.txt.bak  Formatting using a filter in a pipeline $ generate_data | awk {print $1} Example: $ cat awk.txt | awk {print $1} > awk.txt.bak Before proceeding to next slide please create a file named awk.txt with following Contents. 07.46.199.184 [28/Sep/2010:04:08:20] "GET /robots.txt HTTP/1.1" 200 0 "msnbot" 123.125.71.19 [28/Sep/2010:04:20:11] "GET / HTTP/1.1" 304 - "Baiduspider" Nihar R Paital
  • 5. Basic but important for awk  Syntax :  awk {print $n} filename  Generate data : awk {print $n}  Awk programs will start with a "{" and end with a "}"  $0 is the entire line  Awk parses the line in to fields for you automatically, using any whitespace (space, tab) as a delimiter.  Fields of a regular file will be available using $1,$2,$3 … etc  NF : It is a special Variable contains the number of fields in the current line. We can print the last field by printing the field $NF  NR : It prints the row number being currently processed. Nihar R Paital
  • 6. Basic Examples $ awk '{print $0}' awk.txt It will print all the lines as they are in File $ echo 'this is a test' | awk '{print $3}' It will print 'a' $ echo 'this is a test' | awk '{print $NF}' It prints "test" $ awk '{print $1, $(NF-2) }' awk.txt It will print the last 3rd word of file awk.txt $ awk '{print NR ") " $1 " -> " $(NF-2)}‘ Output: 1) 07.46.199.184 -> 200 2) 123.125.71.19 -> 304 Nihar R Paital
  • 7. Advance use of AWK $ awk '{print $2}' logs.txt Output: [28/Sep/2010:04:08:20] [28/Sep/2010:04:20:11] The date field is separated by "/" and ":" characters. Suppose I want to print like [28/Sep/2010 [28/Sep/2010 $ awk '{print $2}' logs.txt | awk 'BEGIN{FS=":"}{print $1}' Output: [28/Sep/2010 [28/Sep/2010 Here FS=“:” means Field Separator as colon(:) $ awk '{print $2}' logs.txt | awk 'BEGIN{FS=":"}{print $1}' | sed 's/[//' Output: 28/Sep/2010 28/Sep/2010 Here We are Substituting [ with NULL value Nihar R Paital
  • 8. Advance Use of AWK If I want to return only the 200 status lines $ awk '{if ($(NF-2) == "200") {print $0}}' logs.txt Output: 07.46.199.184 [28/Sep/2010:04:08:20] "GET /robots.txt HTTP/1.1" 200 0 "msnbot" $ awk '{a+=$(NF-2); print "Total so far:", a}' logs.txt Output: Total so far: 200 Total so far: 504 $ awk '{a+=$(NF-2)}END{print "Total:", a}' logs.txt Output: Total: 504 Nihar R Paital