How to display a specified line of a file or command result on Linux (sed, awk)

Learn how to use Linux commands (sed, ʻawk`) to display a specified line of a file or command result.

environment

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

1. How to display the specified line in the file

There is a file (ʻa01.txt`).

a01.txt


1111 2222 3333 4444 a
2222 3333 4444 5555 b
3333 4444 5555 6666 c
4444 5555 6666 7777 d
5555 6666 7777 8888 e

1.1. Display line n

To display the ** 3rd line ** of the file (ʻa01.txt`), you can display it with the following command.

sed -n 3p a01.txt

Execution result


[demo@centos8 test]$ sed -n 3p a01.txt
3333 4444 5555 6666 c
[demo@centos8 test]$

You can also display it with the following command.

awk 'NR==3' a01.txt

Execution result


[demo@centos8 test]$ awk 'NR==3' a01.txt
3333 4444 5555 6666 c
[demo@centos8 test]$

1.2. Display lines m to n

To display ** 2nd to 4th lines **, use the following command.

sed -n '2,4p' a01.txt

Execution result


[demo@centos8 test]$ sed -n '2,4p' a01.txt
2222 3333 4444 5555 b
3333 4444 5555 6666 c
4444 5555 6666 7777 d
[demo@centos8 test]$

You can also display it with the following command.

awk 'NR==2,NR==4' a01.txt

Execution result


[demo@centos8 test]$ awk 'NR==2,NR==4' a01.txt
2222 3333 4444 5555 b
3333 4444 5555 6666 c
4444 5555 6666 7777 d
[demo@centos8 test]$

1.3 Display m and n lines

To display ** 2nd line ** and ** 4th line **, use the following command.

awk 'NR==2;NR==4' a01.txt

Execution result


[demo@centos8 test]$ awk 'NR==2;NR==4' a01.txt
2222 3333 4444 5555 b
4444 5555 6666 7777 d
[demo@centos8 test]$

1.4. Display the nth and subsequent lines

If you want to display ** all ** from the second line onward, you can display it with the following command.

tail -n +2 a01.txt

Execution result


[demo@centos8 test]$ tail -n +2 a01.txt
2222 3333 4444 5555 b
3333 4444 5555 6666 c
4444 5555 6666 7777 d
5555 6666 7777 8888 e
[demo@centos8 test]$

You can also display it with the following command. sed -n '2,$p' a01.txt

Execution result


[demo@centos8 test]$ sed -n '2,$p' a01.txt
2222 3333 4444 5555 b
3333 4444 5555 6666 c
4444 5555 6666 7777 d
5555 6666 7777 8888 e
[demo@centos8 test]$

awk 'NR>=2' a01.txt

Execution result


[demo@centos8 test]$ awk 'NR>=2' a01.txt
2222 3333 4444 5555 b
3333 4444 5555 6666 c
4444 5555 6666 7777 d
5555 6666 7777 8888 e
[demo@centos8 test]$

2. How to display the specified line of the command (using | (pipe))

The `ls -la'command displays:

Execution result


[demo@centos8 test]$ ls -la
16 in total
drwxrwxr-x 2 demo demo 66 June 14 16:13 .
drwx------.5 demo demo 159 June 14 15:38 ..
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 56 June 14 14:48 a02.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
-rw-rw-r--1 demo demo 55 June 14 14:51 b02.txt
[demo@centos8 test]$

By connecting the commands with a pipe, you can input the sed and ʻawk` commands.

2.1. Display the nth line

To display the ** 4th line ** of the command result, you can display it with the following command.

ls -la | sed -n 4p

Execution result


[demo@centos8 test]$ ls -la | sed -n 4p
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
[demo@centos8 test]$

ls -la | awk 'NR==4'

Execution result


[demo@centos8 test]$ ls -la | awk 'NR==4'
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
[demo@centos8 test]$

2.2. Display lines m to n

To display the ** 4th to 6th lines ** of the command result, you can display it with the following command.

ls -la | sed -n '4,6p'

Execution result


[demo@centos8 test]$ ls -la | sed -n '4,6p'
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 56 June 14 14:48 a02.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
[demo@centos8 test]$

ls -la | awk 'NR==4,NR==6'

Execution result


[demo@centos8 test]$ ls -la | awk 'NR==4,NR==6'
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 56 June 14 14:48 a02.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
[demo@centos8 test]$

2.3 Display m and n lines

To display the ** 4th line ** and ** 6th line ** of the command result, you can display them with the following command.

ls -la | awk 'NR==4;NR==6'

Execution result


[demo@centos8 test]$ ls -la | awk 'NR==4;NR==6'
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
[demo@centos8 test]$

2.4. Display the nth and subsequent lines

If you want to display all ** after the ** 4th line of the command result, you can display it with the following command.

`ls -la | tail -n +4'

Execution result


[demo@centos8 test]$ ls -la | tail -n +4
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 56 June 14 14:48 a02.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
-rw-rw-r--1 demo demo 55 June 14 14:51 b02.txt
[demo@centos8 test]$

ls -la | sed -n '4,$p'

Execution result


[demo@centos8 test]$ ls -la | sed -n '4,$p'
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 56 June 14 14:48 a02.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
-rw-rw-r--1 demo demo 55 June 14 14:51 b02.txt
[demo@centos8 test]$

ls -la | awk 'NR>=4'

Execution result


[demo@centos8 test]$ ls -la | awk 'NR>=4'
-rw-rw-r--1 demo demo 110 June 14 15:38 a01.txt
-rw-rw-r--1 demo demo 56 June 14 14:48 a02.txt
-rw-rw-r--1 demo demo 111 June 14 14:50 b01.txt
-rw-rw-r--1 demo demo 55 June 14 14:51 b02.txt
[demo@centos8 test]$

that's all

Recommended Posts

How to display a specified line of a file or command result on Linux (sed, awk)
How to display a specified column of files in Linux (awk)
How to output the output result of the Linux man command to a file
How to display emoji on Manjaro Linux
How to copy and paste command line content without mouse in bash on Linux or mac
How to put a line number at the beginning of a CSV file
Verification of how to periodically execute a script on a Linux server on Windows
Try to create a new command on linux
How to create a shortcut command for LINUX
#The command to check the encoding of a text file (ISO-2022-JP, Shift_JIS, EUC-JP, UTF-8 or UTF-16) on Linux is nkf --guess (forget every time)
How to access the contents of a Linux disk on a Mac (but read-only)
Push notifications to your smartphone from the Linux command line or scripts. (Introduction of a tool called ntfy)
How to delete the specified string with the sed command! !! !!
How to install Linux on a 32bit UEFI PC
(Remember quickly) How to use the LINUX command line
How to build a Python environment on amazon linux 2
How to display the modification date of a file in C language up to nanoseconds
How to pass args or environment variables with Makefile and make command on #Linux
A memo on how to easily prepare a Linux exercise environment
How to run a Python file at a Windows 10 command prompt
How to display a list of installable versions with pyenv
[Linux] How to disable automatic update of /etc/resolv.conf file (Redhat)
How to pass the execution result of a shell command in a list in Python (non-blocking version)
[sh] How to store the command execution result in a variable
On Linux, the time stamp of a file is a little past.
How to make a command to read the configuration file with pyramid
How to get a string from a command line argument in python
How to display PDF resolution and detailed information on Linux (pdfinfo)
[Linux] How to disable the automatic update of the /etc/resolv.conf file (AmazonLinux2)
A command to easily check the speed of the network on the console
Completion of docker command on Linux
How to create a config file
Understand how to display images on Jupyter (utilization of imshow / matplotlib of OpenCV)
[Linux] Command to get a list of commands executed in the past
How to draw a vertical line on a heatmap drawn with Python seaborn
[Python] How to read a csv file (read_csv method of pandas module)
How to format a list of dictionaries (or instances) well in Python
How to install aws-session-manager-plugin on Manajro Linux
[Linux] How to use the echo command
How to calculate Use% of df command
How to use the Linux grep command
How to update php on Amazon linux 2
How to install packages on Alpine Linux
How to install Anisble on Amazon Linux 2
How to update security on CentOS Linux 8
How to install php7.4 on Linux (Ubuntu)
How to test on a Django-authenticated page
A command to specify a file with a specific name in a directory with find and mv, cp, or gzip it (linux)
How to find large files on Linux
Various ways to read the last line of a csv file in Python
One-liner to create a large number of test files at once on Linux
How to achieve something like a list of void * (or variant) in Go?
I want to display only different lines of a text file with diff
Find out how to divide a file with a certain number of lines evenly
[Linux] How to read .bashrc of general user as root user on WSL2 Ubuntu20.04
LINUX: How to make arrow keys correspond to 2,4,6,8 on a notebook without a numeric keypad
A memorandum of how to execute the! Sudo magic command in Jupyter Notebook
How to make a Raspberry Pi that speaks the tweets of the specified user
linux / c> link> Get the execution result of the shell command in the C program> I was taught how to use popen ()
How to calculate the volatility of a brand
[Python] How to test command line parser click