• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
sharadchhetri.com

sharadchhetri.com

Linux,Cloud Computing And DevOps

  • Home
  • Linux
  • CloudComputing
    • Introduction Cloud Computing
    • Amazon AWS
  • Free E-Books
  • About Me

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

Do'nt be greedy, share the knowledge!

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pocket (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)

Related

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

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

Help To Needy

If you like our posts and tutorials, please support/donate to ANY “Open Source Project” or do some Charity work. Hope this will make someone happy in this world. Drop me an email if you want to inspire others!

Thank You!

Follow Us

  • Facebook
  • Twitter
  • GitHub
  • YouTube

Subscribe to Blog via Email

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

Top Posts & Pages

  • How to delete mail queue in Postfix
  • How to start / stop / restart / reload iptables on CentOS 7 / RHEL 7
  • How to fix read only USB pen drive in Ubuntu
  • How to zip the directory in linux with command line
  • How to set hostname and FQDN on CentOS 7 and RHEL 7
  • 4 different commands to check the load average in linux
  • How to find absolute path of command on Linux / Unix : which command
  • How to know last command run successfully : Linux / Unix
  • Install KVM on Ubuntu 14.04 LTS Desktop
  • How to install MySQL Server 5.6 on CentOS 7 / RHEL 7

Recent Posts

  • Install and setup maven in Linux for Jenkins
  • How to setup Jenkins Credentials for Git repo access
  • How to manage Jenkins Credentials
  • Managing Jenkins plugins from command line and GUI
  • How to create Jenkins user by command line and GUI

Footer

Sharad Chhetri is an experienced Linux - Cloud Engineer & freelancer. Working on Open Source Technology since RHEL 4.0 (Red Hat Enterprise Linux). He loves sharing the knowledge which earned from real scenarios. Don't be surprised if you find him in technology seminars and meetup groups. You can contact him on email for freelance projects at admin@sharadchhetri.com. Read More…

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Our GitHub Repo

sharadchhetri @ GitHub
  • scripts
    Cassandra Backup Script
    February 19, 2019 - 8:46 am UTC
  • java-samples
    Java Sample Project For Tutorial
    December 25, 2018 - 2:29 pm UTC
  • misc
    OpenSSH rpm created from tar ball
    November 21, 2018 - 3:12 am UTC
  • packer
    Packer : Create server images in automated way
    November 21, 2018 - 3:08 am UTC
  • Nagios
    My Nagios custom plugins
    November 21, 2018 - 3:07 am UTC

Recent Comments

  • Sharad Chhetri on CentOS 7 / RHEL 7 : change OpenSSH port number ( SELINUX enabled )
  • Dave on CentOS 7 / RHEL 7 : change OpenSSH port number ( SELINUX enabled )
  • Prafulla S on Install Shotcut on Ubuntu 16.04 LTS desktop
  • Sujata on Install Telnet Server on CentOS 7 / RHEL 7
  • kishen sharma on How to install redis server on CentOS 7 / RHEL 7
  • Sharad Chhetri on Secondary Logging : save all users history command output as log
  • Anand on Secondary Logging : save all users history command output as log
  • Sharad Chhetri on Install redis 3.0 from source on Ubuntu 14.04 / CentOS 7 / RHEL 7

Copyright © 2009 - 2019 · All Rights Reserved sharadchhetri.com · · Privacy Policy ·
· sitemap.xml · ·The content is copyrighted to sharadchhetri.com and may not be reproduced on other websites without our permission. ·