How to display a specified column of files in Linux (awk)

You can use the Linux command ╩╗awk` to display a specified column in a file.

environment

[demo@centos8 ~]$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[demo@centos8 ~]$

1. How to display the specified columns separated by half-width spaces

There are the following files (b01.txt) separated by half-width spaces.

b01.txt


1 a ab
2 bb bcd
3 ccc cdef
4 dddd defgh
5 eeeee efghij

Only the second column can be displayed with the following command.

awk '{print $2}' b01.txt

Execution result


[demo@centos8 test]$ awk '{print $2}' b01.txt
a
bb
ccc
dddd
eeeee
[demo@centos8 test]$

2. How to display specified columns separated by ","

There are the following files (b02.txt) separated by", ".

b02.txt


1,a,ab
2,bb,bcd
3,ccc,cdef
4,dddd,defgh
5,eeeee,efghij

Only the second column can be displayed with the following command.

awk -F',' '{print $2}' b02.txt

Execution result


[demo@centos8 test]$ awk -F',' '{print $2}' b02.txt
a
bb
ccc
dddd
eeeee
[demo@centos8 test]$

3. How to display tab-delimited specified columns

There is the following tab-delimited file (b03.txt).

b03.txt


1	a	ab
2	bb	bcd
3	ccc	cdef
4	dddd	defgh
5	eeeee	efghij

Only the second column can be displayed with the following command.

awk -F'\t' '{print $2}' b03.txt

Execution result


[demo@centos8 test]$ awk -F'\t' '{print $2}' b03.txt
a
bb
ccc
dddd
eeeee
[demo@centos8 test]$

4. How to display the specified matrix

There are the following files (b01.txt) separated by half-width spaces.

b01.txt


1 a ab
2 bb bcd
3 ccc cdef
4 dddd defgh
5 eeeee efghij

You can display the 3rd row and 2nd column with the following command.

awk 'NR==3 {print $2}' b01.txt

Execution result


[demo@centos8 test]$ awk 'NR==3 {print $2}' b01.txt
ccc
[demo@centos8 test]$

The following command combined with the sed command can also display the 3rd row and 2nd column.

sed -n 3p b01.txt | awk '{print $2}'

Execution result


[demo@centos8 test]$ sed -n 3p b01.txt | awk '{print $2}'
ccc
[demo@centos8 test]$

that's all

Recommended Posts

How to display a specified column of files in Linux (awk)
How to display a specified line of a file or command result on Linux (sed, awk)
Summary of how to import files in Python 3
How to display multiple images of galaxies in tiles
How to display DataFrame as a table in Markdown
How to reference static files in a Django project
[Linux] How to put your IP in a variable
How to get a list of files in the same directory with python
How to display the modification date of a file in C language up to nanoseconds
How to list files under the specified directory in a list (multiple conditions / subdirectory search)
How to develop in a virtual environment of Python [Memo]
How to display a list of installable versions with pyenv
How to get a list of built-in exceptions in python
Summary of how to write .proto files used in gRPC
How to get a quadratic array of squares in a spiral!
How to display the regional mesh of the official statistics window (eStat) in a web browser
How to determine the existence of a selenium element in Python
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to output the output result of the Linux man command to a file
How to get the vertex coordinates of a feature in ArcPy
How to create a large amount of test data in MySQL? ??
How to delete multiple specified positions (indexes) in a Python list
[Linux] How to subdivide files and folders
How to suppress display error in matplotlib
Display a list of alphabets in Python 3
[linux] Split files to the specified size
How to get a stacktrace in python
How to display multiplication table in python
How to display emoji on Manjaro Linux
How to find large files on Linux
How to display Hello world in python
How to read CSV files in Pandas
How to get a specific column name and index name in pandas DataFrame
How to send a visualization image of data created in Python to Typetalk
[Python] How to put any number of standard inputs in a list
[Linux] Command to get a list of commands executed in the past
How to find a specific type (str, float etc) column in a DataFrame column
How to display Django static files correctly under Amazon Linux2 + Apache environment
Verification of how to periodically execute a script on a Linux server on Windows
How to format a list of dictionaries (or instances) well in Python
I tried to display the altitude value of DTM in a graph
How to sort by specifying a column in the Python Numpy array.
How to calculate the volatility of a brand
A simple example of how to use ArgumentParser
How to clear tuples in a list (Python)
How to keep track of work in Powershell
How to embed a variable in a python string
How to create a JSON file in Python
Summary of how to use MNIST in Python
How to write regular expression patterns in Linux
How to implement a gradient picker in Houdini
How to check / extract files in RPM package
How to notify a Discord channel in Python
How to create a shortcut command for LINUX
How to get the files in the [Python] folder
How to display videos inline in Google Colab
[Python] How to draw a histogram in Matplotlib
How to create a Rest Api in Django
How to write a named tuple document in 2020
How to count numbers in a specific range