LoginSignup
1
1

More than 3 years have passed since last update.

Linuxでファイルまたはコマンド結果の指定された行を表示する方法(sed、awk)

Posted at

Linuxのコマンド(sedawk)を使用して、ファイルまたはコマンド結果の指定された行を表示する方法を紹介します。

環境

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

1. ファイルの指定された行を表示する方法

ファイル(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. n行目を表示

ファイル(a01.txt)の3行目を表示する場合、以下のコマンドで表示できます。

sed -n 3p a01.txt

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

また、以下のコマンドでも表示できます。

awk 'NR==3' a01.txt

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

1.2. m~n行目を表示

2~4行目を表示する場合、以下のコマンドで表示できます。

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

実行結果
[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]$

また、以下のコマンドでも表示できます。

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

実行結果
[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 m行目とn行目を表示

2行目4行目を表示する場合、以下のコマンドで表示できます。

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

実行結果
[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. n行目以降を表示

2行目以降全てを表示する場合、以下のコマンドで表示できます。

tail -n +2 a01.txt

実行結果
[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]$

また、以下のコマンドでも表示できます。
sed -n '2,$p' a01.txt

実行結果
[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

実行結果
[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. コマンドの指定された行を表示する方法(|(パイプ)を使用する)

`ls -la'コマンドで以下が表示されます。

実行結果
[demo@centos8 test]$ ls -la
合計 16
drwxrwxr-x  2 demo demo  66  6月 14 16:13 .
drwx------. 5 demo demo 159  6月 14 15:38 ..
-rw-rw-r--  1 demo demo 110  6月 14 15:38 a01.txt
-rw-rw-r--  1 demo demo  56  6月 14 14:48 a02.txt
-rw-rw-r--  1 demo demo 111  6月 14 14:50 b01.txt
-rw-rw-r--  1 demo demo  55  6月 14 14:51 b02.txt
[demo@centos8 test]$

コマンドをパイプでつなぐことで、sedawkコマンドの入力とすることができます。

2.1. n行目を表示

コマンド結果の4行目を表示する場合、以下のコマンドで表示できます。

ls -la | sed -n 4p

実行結果
[demo@centos8 test]$ ls -la | sed -n 4p
-rw-rw-r--  1 demo demo 110  6月 14 15:38 a01.txt
[demo@centos8 test]$

ls -la | awk 'NR==4'

実行結果
[demo@centos8 test]$ ls -la | awk 'NR==4'
-rw-rw-r--  1 demo demo 110  6月 14 15:38 a01.txt
[demo@centos8 test]$

2.2. m~n行目を表示

コマンド結果の4~6行目を表示する場合、以下のコマンドで表示できます。

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

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

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

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

2.3 m行目とn行目を表示

コマンド結果の4行目6行目を表示する場合、以下のコマンドで表示できます。

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

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

2.4. n行目以降を表示

コマンド結果の4行目以降全てを表示する場合、以下のコマンドで表示できます。

`ls -la | tail -n +4'

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

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

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

ls -la | awk 'NR>=4'

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

以上

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1