• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
sharadchhetri

sharadchhetri

Tutorials On Linux, Unix & Open Source

  • Home
  • Linux Commands
  • Resources
    • Learn Linux
  • My WordPress plugins

awk command to print columns from file

June 7, 2014 by Sharad Chhetri Leave a Comment

In this tutorial we will learn, how to use awk command to print columns from file. This tip is often used by Unix/Linux System Administrator.

AWK Command

Awk is one of the awesome tool for processing the rows and columns.It also has feature to search keywords/strings . Awk command is widely used by Unix/Linux users for text processing the files. We can also use regex expression with awk command.

print columns from files with awk command

As per the post title, we will explain some tips on awk command to print columns from file.
For practical we will use the file called employee.list . I have created this file in /tmp and lets have a look on its content.

Figure 1:
awk command

Print single columns from file

To print the single column from file,use below given syntax

awk '{print $column-number}' /path/file-name

Example: Printing column no. 2

awk '{print $2}' /tmp/employee.list

Example: In another example, we will print column no. 4

awk '{print $4}' /tmp/employee.list

Below is the screenshot of both awk command example. (compare the column no. shown in Figure 1)

awk example

Print multiple columns from file

Printing multiple columns require comma(,) separating each defined multiple column number in awk command.

Use below given syntax for printing multiple columns by using awk command.

awk '{print $column-number1,$column-number2,$column-number-N'} /path/file-name

Example 1: Now we will print column number 2,column number 3 and column number 5

awk '{print $2,$3,$5}' /tmp/employee.list

See the output in below given screenshot

awk command

Printing single or multiple columns by removing field-separator

I hope from above examples, you learned about using awk command to print single and multiple columns from file.

What if you have field-separator in files like semicolon( ; ),colon( : ),comma( , ),Tab,space etc. ?

In this case, we have option called –field-separator . Or in short we can also use -F .

Use the given below syntax for awk with field-separator option

awk -F'separator' '{print $column-number-N}' /path/file-name 

Lets take an example of /etc/passwd file. We see colon(:) is field-separator there.

Task: Here our task is printing column no. 6 from /etc/passwd which has value of user’s home directory path .
Whereas we also have to deal with separator that is colon ( : ) .

See the result in below given screenshot (Because /etc/passwd file output was long, I have taken screenshot of upper portion only)

awk example

Similarly, We can print multiple columns with field-separator

Here, in this example we will print column no. 1 , column number 6 and column no. 7 from /etc/passwd.

awk -F':' '{print $1,$6,$7}' /etc/passwd 

Check the result in your system ๐Ÿ™‚ , it will help you to understand

Prints the entire file using awk command

This one is interesting command.Printing the entire file using awk command .
It is similar to cat file-name

Syntax:

awk '{print $0}' /path/filename

Example: We are showing this example in below given screenshot

awk example print $0

Share this:

  • Twitter
  • Facebook
  • More
  • Print
  • Email
  • LinkedIn
  • Reddit
  • Tumblr
  • Pinterest
  • Pocket
  • Telegram
  • WhatsApp
  • Mastodon

Related posts:

  1. awk command to search keyword or strings in file
  2. cat,sed and awk commands to display file contents
  3. Unix / Linux : How to print duplicate lines from file
  4. subtract integer written in columns from 2 different files
  5. How to print particular line number by using sed command
  6. print working directory ( pwd , PWD , OLDPWD ) in Linux / Unix
  7. NOTICE: nagios.cmd file not found. Please specify the location of this file in your /etc/vshell.conf file
  8. Print grep command output without seperator
  9. print new line character in Unix / Linux
  10. Convert video file into gif file through command line in linux

Filed Under: Linux, Linux Commands Tagged With: awk, linux command

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Our Social Media Presence

  • Facebook
  • GitHub
  • Twitter

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Linux Commands

semanage command not found in CentOS 6 and RHEL 6

smbpasswd command not found on CentOS 7 and RHEL 7

awk command to search keyword or strings in file

sed : find the pattern (keyword) and delete the line from file

Temporarily change the shell after login

How to print particular line number by using sed command

Linux: Set user password by using encrypted password in command

Explore 70+ Articles On Linux Commands

Always Useful Tips And Tricks

Yum Error database disk image is malformed

postgres database backup script using database user password inside

Virtual Machine inaccessible status on VirtualBox 4.3

4 Different commands to find system uptime in linux

How to download package using apt-get command in ubuntu

set and unset line number in file with vi editor

How to create a file with cat command

Explore 90+ Article On "Linux Tips And Tricks"

You Might Like These Articles!

Internal External Command

What is Linux/Unix Internal And External Command

Linux basic command

Linux Basic Commands For Every Beginner

simplecodesyntax wordpress plugin

SimpleCodeSyntax : My Another WordPress Plugin

Install Nginx

How To Install Nginx On Ubuntu 22.04 LTS

Install Latest Git package in Ubuntu Operating System

How To Always Install Latest Git Package In Ubuntu Operating System

Bash script for installing VirtualBox on Ubuntu 22.04 LTS Desktop

Install VirtualBox On Ubuntu 22.04 LTS Desktop (Bash Script)

Copyright © 2023 ยท
The material in this site cannot be republished either online or offline, without our permission but Schools and Colleges can do in their Private Network
Proudly Blogging From Bharat.

  • Contact
  • About Me
  • My WordPress plugins
  • Privacy Policy