DEV Community

Cover image for Basic Linux command (awk)
Cheulong Sear
Cheulong Sear

Posted on

Basic Linux command (awk)

awk pattern scanning and processing language

awk command may not be pre-installed on all Linux distributions. To install it use

# Debian/Ubuntu
sudo apt install awk
Enter fullscreen mode Exit fullscreen mode

To use awk, just type awk '{command}' filename

Here I'm using movies.txt as example

cat movies.txt
----
Title   Release Year    Budget  Profit (Approx.)        Studio  Genre
Avatar  2009    $237 million    ~$2.7 billion   20th Century Fox        Sci-Fi / Action
Avengers: Endgame       2019    $356 million    ~$2.2 billion   Marvel Studios  Superhero / Action
The Dark Knight 2008    $185 million    ~$894 million   Warner Bros.    Superhero / Crime Thriller
Titanic 1997    $200 million    ~$2.1 billion   20th Century Fox        Romance / Drama
Jurassic World  2015    $150 million    ~$1.3 billion   Universal Pictures      Sci-Fi / Adventure
Enter fullscreen mode Exit fullscreen mode

Print all lines

1

Search Lines with a Keyword

2

Print Specific Columns

By default, awk uses space as separator, here I use tab as separator, so I need to set it as the separator.

3

Use of NR built-in variables to display line number

4

Use of NR built-in variables to display line 2 to 4

5

Use of NF built-in variables to display last field)

6

Print nonempty line with line number, first, second and last columns separated by -

7

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)