June 10, 2018

How to use awk

❑ AWK(alfred Aho, peter Wenberger, brian Kemighan) : The script language for text pattern scanning and processing.
* It is mainly used to extract specific columns of text data.
* /bin/awk
* /usr/bin/awk

❑ awk program grammar : awk [Option] '/[Pattern]/ {[Action]}' [File]
* After [Pattern] scanning in [File], [Action] is performed.



❑ awk Example

< Data file >
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported.

The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.

Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.


CommandResult
$ awk '{print $1, $2}' ./file1
→ Print the first and second fields.
RegExr v3

Edit the

The side

Explore results
$ awk '/RegExr/' ./file1
→ Print records(paragragh) including "RegExr"(Case-sensitive).
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.
$ awk '/the/ {print "First column: ",$1,"\nnext column : ",$2}' ./file1First column:  Edit
next column :  the
First column:  The
next column :  side
First column:  Explore
next column :  results
$ awk '{print $NF}' ./file1
* NF : The Number of Fields in the current input record.
* '{print NF}' :  Print number of fields in each record.
* '{print $NF}' :  Print the last filed in each record.
Temple.

supported.

Patterns.

English.
$ awk '{print NR}' ./file1
→ Print the index of each records (paragraph).
* NR : The total number of records(paragraph) seen so far.
1
2
3
4
5
6
7
$ awk 'BEGIN {FS="Edit"} {print $1}' ./file1
→ Output the first field after separating the records based on "Edit".
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.

The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.

Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.
$ awk '$1 ~ /RegExr/ {print}' ./file1
→ If the first field is "RegExr", print the record.
* !~ : Not same.
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.
$ awk 'NR > 3 {print}' ./file1
→ Print the records existing after the third record.
The side bar includes a Cheatsheet, full Reference, and Help. You can also Save & Share with the Community, and view patterns you create or favorite in My Patterns.

Explore results with the Tools below. Replace & List output custom results. Details lists capture groups. Explain describes your expression in plain English.
$ awk 'NR==1, NR==3 {print}' ./file1
→ Print the records that are indexed 1~3.
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported.
$ awk 'NR<5 && $1=="RegExr" {print}' ./file1
→ If the index is less than 5 and the first field is "RegExr", print the record.
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.
$ awk 'NR<4 || $1=="RegExr" {print $0}' ./file1
→ If the index is less than 4 or the first field is "RegExr", print the record.
RegExr v3 was created by gskinner.com, and is proudly hosted by Media Temple.

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & Javascript flavors of RegEx are supported.
$ awk 'BEGIN {IGNORECASE = 1} /RegExp/' file.txt(Case-insensitive result)

※ This is tested all in [Vidio 1] and [Video 2]