What is awk print $1?

In this tutorial, we will see about what is meant by awk print $1.

awk is interpreted programming language. It is very powerful and used for text processing.Awk stands for the names of its authors “Aho, Weinberger, and Kernighan”.

awk print $1

As said before, awk can be used for text processing.awk treats tab or whitespace for file separator by default.
Awk actually uses some variables for each data field found.

  • $0 for whole line
  • $1 for first field
  • $2 for second field
  • $3 for third field
  • $n for nth field

so here print $1 represents first field in the file.

Let’s understand with the help of an example.

$ cat > sample.txt

This is sample file
Java is programming language.
This is Java tutorial
I know Java very well.

Let’s run the command now

$awk ‘{print $1}’ sample.txt

This
Java
This
I

If you notice awk ‘print $1’ prints first word of each line.
If you use $3, it will print 3rd word of each line.

$awk ‘{print $3}’ sample.txt

sample
programming
Java
Java

Let’s use csv file now for demonstration

$ cat > countries.csv

#Country,Population,Captital
India,10000,Delhi
Nepal,2000,Kathmandu
China,20000,Beijing

Let’s print first column of csv file using awk print $1 command. We need to provide explicit delimeter over here.

$awk -F’,’ ‘{print $1}’ countries.csv

#Country
India
Nepal
China

That’s all about awk print $1 command.

Was this post helpful?

Related Posts

  • 18 January

    Sed Command in unix

    Table of Contents$ cat sed_demo.txt > 1. Nike Shoes – Tag 344 – Price $ 16 2. Rebook shoes – Tag 433 – Price $ 17 3. Bata Shoes – Tag 211 – Price $ 18 4. Summer Shoes – Tag 978 – Price $ 16 $ sed ‘s/16/19/’ sed_demo.txt$ cat sed_demo.txt 1. Nike Shoes […]

  • 16 October

    Find command in linux

    Table of ContentsSyntaxSearching for Files and Directories OnlySearch based on File PermissionSearch for files that are modified at some specific time/daySearch based on UserSearch based on File sizeWas this post helpful? In this post, we will see about find command in linux. Find is an important and one of most widely used command in Unix […]

  • 15 October

    Grep command in unix

    Table of ContentsSyntaxInstalling grepWorking with Grep$grep arpit /var/logs/application.logUsing Grep with other commandsWas this post helpful? In this post, we will see about grep command in unix. The grep command stands for “Global Regular Expression Print” and is used to search text or searches the given file for lines containing a match to the given strings […]

  • 24 September

    sed replace string in file

    Table of ContentsProblemSolution$ cat > sample.txt$ sed -i -e ‘s/Java/Python/g’ sample.txt $ cat sample.txtMatch word and then find and replace$ sed -i -e ‘/tutorial/s/Python/java/g’ sample.txt $ cat sample.txtSome problems with Sed$ cat > site.txt$ sed -i -e ‘/s/https://www.google.com/https://www.bing.com/g’ site.txt$ sed -i -e ‘s+https://www.google.com+https://www.bing.com+g’ site.txt $ cat site.txtWas this post helpful? In this post, we will […]

  • 24 September

    Find file containing text in linux

    Table of ContentsProblemSolution1. Search text “https://www.google.com” in current directory$ grep -s “https://www.google.com” *2. Search text “https://www.google.com” in current directory and all subdirectories3. Search text “https://www.google.com” in current directory,all subdirectories and display matched text in color4. Search text “https://www.google.com” in /opt directory and display just file name 5. Hide warning spam6. Ignore case while searching textWas […]

  • 24 September

    find file by name in linux

    Table of ContentsFind file in the current directory$ find . -name letters.txt$ find . -iname letters.txtFind file in the specific directory1. Find file “letters.txt” in /home directory.2. Find file “letters.txt” in multiple directories /home and /opt.3. Find file “letters.txt” in directory /home and remove it4. Find all files in /opt directory with txt extension5. Find […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.