Play around with AWK language!

hasone profile image HasOne ・1 min read

In this tutorial, we're gonna use AWK programming language to play around with files in Unix-like OS.

Let's see what we can do by this Language, first open up your terminal.


Obviously, we'll start with Hello World! as it essential for every programming language,

awk 'BEGIN { print "Hello World!" }'

BEGIN is for generating header information, and the Curly braces the print will print something.

Print content from file:

awk ' { print } ' /etc/passwd

this is /etc/passwd, but you can specify your own file.

Real Examples

now let's filter the file.

awk 'BEGIN { FS=":" } { print $1 }' /etc/passwd

FS option for the input field separator which is : (colon), it means we'll split lines by colon like user:pass.

in the second curly braces we print the first field $1, you can specify $n n is number.

Count Lines in File:

awk 'BEGIN { FS=":" } { print NR, $1 }' /etc/passwd

NR the total number of lines. you will get each line with numbers.

Specifying Lines:

I want to get the content from line 1 to 10
awk 'NR <= 10' /etc/passwd

printin' from the line 10 to 20 will be same
awk 'NR == 10, NR == 20' /etc/passwd

I'm sorry in advance, If I made a mistake, Don't hesitate to fix me, please! Learn more about this https://gumroad.com/learnbyexample, just download awk book from there, and start learning, automate your daily task.

Posted on by:

hasone profile



A tech enthusiast


markdown guide