Learn how to use Linux commands (sed
, ʻawk`) to display a specified line of a file or command result.
[demo@centos8 test]$ cat /etc/redhat-release
CentOS Linux release 8.1.1911 (Core)
[demo@centos8 test]$
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
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]$
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]$
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]$
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]$
|
(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.
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]$
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]$
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]$
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