To print a specific column using awk, use the syntax awk '{print $N}' filename where N is the column number starting from 1. For files with non-whitespace delimiters, use the -F option or set FS in a BEGIN block. Multiple columns can be printed by listing them separated by commas or custom separators. Conditions can also be added to filter rows before printing. This approach provides flexibility for various data formats and filtering needs.
To print a specific column from a file using awk
, you just need to specify the column number using the $
symbol followed by the field number. For example, if you want to print the second column of a file, you'd use awk '{print $2}' filename
. This works well for basic text files where columns are separated by whitespace (spaces or tabs), which is the default behavior in awk
.
Basic syntax and usage
The general syntax for printing a specific column is:
awk '{print $N}' filename
Where N
is the column number you want to print. Columns are numbered starting from 1. So $1
refers to the first column, $2
the second, and so on.
Here’s how it looks in practice:
- To print the first column:
awk '{print $1}' data.txt
- To print the third column:
awk '{print $3}' data.txt
This method assumes your input file uses whitespace as the field separator. If your file uses something else, like commas or colons, keep reading — there's a way to handle that too.
Handling different delimiters
If your file uses a delimiter other than whitespace (like a comma or colon), you can tell awk
what to use with the -F
option.
For example, if you have a CSV file (data.csv
) with fields separated by commas:
name,age,location Alice,30,New York Bob,25,San Francisco
You’d print the second column like this:
awk -F',' '{print $2}' data.csv
Or for a colon-separated file (like /etc/passwd
):
awk -F':' '{print $1}' /etc/passwd
You can also set the field separator inside the script using BEGIN{FS=":"}
:
awk 'BEGIN{FS=":"} {print $1}' /etc/passwd
This gives you flexibility depending on the format of your input file.
Printing multiple columns
Sometimes you might not just want one column — maybe you want to print two or more columns together. You can do that by listing them in the print statement, separated by commas or spaces.
For example:
awk '{print $1, $3}' data.txt
This will print the first and third columns, with a space in between. If you want a custom separator (like a tab or something else), you can add it directly:
awk '{print $1 "\t" $3}' data.txt
That way, you control exactly how the output looks.
Another common case: sometimes people want to print all columns except one. That’s a bit trickier and usually involves looping through the fields. But for most everyday needs, just selecting the columns you want works fine.
Filtering rows before printing
What if you only want to print a column for lines that match a certain condition? That’s possible too.
In awk
, you can add a condition before the print command. For example, to print the second column only when the first column is "apple":
awk '$1 == "apple" {print $2}' data.txt
Or to print the fifth column only if the fourth field is greater than 100:
awk '$4 > 100 {print $5}' data.txt
This makes awk
powerful beyond just extracting columns — it becomes a quick filtering tool too.
So basically, printing a specific column with awk
is straightforward once you know the right syntax and how to adjust for delimiters and conditions. It’s not complicated, but knowing how to adapt it to different formats and needs really helps.
The above is the detailed content of How to print a specific column from a file using awk?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When encountering DNS problems, first check the /etc/resolv.conf file to see if the correct nameserver is configured; secondly, you can manually add public DNS such as 8.8.8.8 for testing; then use nslookup and dig commands to verify whether DNS resolution is normal. If these tools are not installed, you can first install the dnsutils or bind-utils package; then check the systemd-resolved service status and configuration file /etc/systemd/resolved.conf, and set DNS and FallbackDNS as needed and restart the service; finally check the network interface status and firewall rules, confirm that port 53 is not

As a system administrator, you may find yourself (today or in the future) working in an environment where Windows and Linux coexist. It is no secret that some big companies prefer (or have to) run some of their production services in Windows boxes an

Built on Chrome’s V8 engine, Node.JS is an open-source, event-driven JavaScript runtime environment crafted for building scalable applications and backend APIs. NodeJS is known for being lightweight and efficient due to its non-blocking I/O model and

In Linux systems, 1. Use ipa or hostname-I command to view private IP; 2. Use curlifconfig.me or curlipinfo.io/ip to obtain public IP; 3. The desktop version can view private IP through system settings, and the browser can access specific websites to view public IP; 4. Common commands can be set as aliases for quick call. These methods are simple and practical, suitable for IP viewing needs in different scenarios.

Linuxcanrunonmodesthardwarewithspecificminimumrequirements.A1GHzprocessor(x86orx86_64)isneeded,withadual-coreCPUrecommended.RAMshouldbeatleast512MBforcommand-lineuseor2GBfordesktopenvironments.Diskspacerequiresaminimumof5–10GB,though25GBisbetterforad

Written in C, MySQL is an open-source, cross-platform, and one of the most widely used Relational Database Management Systems (RDMS). It’s an integral part of the LAMP stack and is a popular database management system in web hosting, data analytics,

Ubuntu has long stood as a bastion of accessibility, polish, and power in the Linux ecosystem. With the arrival of Ubuntu 25.04, codenamed “Plucky Puffin”, Canonical has once again demonstrated its commitment to delivering a

MongoDB is a high-performance, highly scalable document-oriented NoSQL database built to manage heavy traffic and vast amounts of data. Unlike traditional SQL databases that store data in rows and columns within tables, MongoDB structures data in a J
