LoginSignup
2
1

More than 3 years have passed since last update.

【Linux】awkでのテキスト処理(先頭行/最終行/行指定などの行操作)

Posted at

Linuxでテキスト処理を行う際にawkをよく使います。
awkを使うと、例えばa b c と並んでいるデータをb a c と並び変えて出力したり、数字をカンマ区切りにしたりできます。

今回はawkを使用してテキストの先頭行/最終行/行指定を行うスクリプト+αを書いていきます。

サンプルテキスト

/rootでls -l > /tmp/txt をした結果です。
12行のテキストファイルとなっています。

[root@localhost ~]# cat /tmp/txt
合計 12
-rw-------.  1 root root 1898  7 28 23:20 anaconda-ks.cfg
-rw-r--r--.  1 root root 1926  7 28 14:23 initial-setup-ks.cfg
drwxr-xr-x. 10 root root 4096  7 28 15:35 rtl8188eu
drwxr-xr-x.  2 root root    6  7 28 16:45 ダウンロード
drwxr-xr-x.  2 root root    6  7 28 16:45 テンプレート
drwxr-xr-x.  2 root root    6  7 28 16:45 デスクトップ
drwxr-xr-x.  2 root root    6  7 28 16:45 ドキュメント
drwxr-xr-x.  2 root root    6  7 28 16:45 ビデオ
drwxr-xr-x.  2 root root    6  7 28 16:45 音楽
drwxr-xr-x.  2 root root    6  7 28 16:45 画像
drwxr-xr-x.  2 root root    6  7 28 16:45 公開

先頭行を表示する

[root@localhost ~]# cat /tmp/txt | awk 'NR==1'
合計 12
NRとはNumber of Recordのこと。

headでも同じことができます。
[root@localhost ~]# cat /tmp/txt | head -n 1
合計 12

先頭行を除く

[root@localhost ~]# cat /tmp/txt | awk 'NR>1'
-rw-------.  1 root root 1898  7 28 23:20 anaconda-ks.cfg
-rw-r--r--.  1 root root 1926  7 28 14:23 initial-setup-ks.cfg
drwxr-xr-x. 10 root root 4096  7 28 15:35 rtl8188eu
drwxr-xr-x.  2 root root    6  7 28 16:45 ダウンロード
drwxr-xr-x.  2 root root    6  7 28 16:45 テンプレート
drwxr-xr-x.  2 root root    6  7 28 16:45 デスクトップ
drwxr-xr-x.  2 root root    6  7 28 16:45 ドキュメント
drwxr-xr-x.  2 root root    6  7 28 16:45 ビデオ
drwxr-xr-x.  2 root root    6  7 28 16:45 音楽
drwxr-xr-x.  2 root root    6  7 28 16:45 画像
drwxr-xr-x.  2 root root    6  7 28 16:45 公開

NR>1NR>=2に変えても同じ結果になります。

最終行を表示する

[root@localhost ~]# cat /tmp/txt | awk 'END{print $0}'
drwxr-xr-x.  2 root root    6  7 28 16:45 公開

行指定

#2行目を指定する場合
[root@localhost ~]# cat /tmp/txt | awk 'NR==2'
-rw-------.  1 root root 1898  7 28 23:20 anaconda-ks.cfg

#3行目~5行目を指定する場合
[root@localhost ~]# cat /tmp/txt | awk 'NR>2 && NR<6'
-rw-r--r--.  1 root root 1926  7 28 14:23 initial-setup-ks.cfg
drwxr-xr-x. 10 root root 4096  7 28 15:35 rtl8188eu
drwxr-xr-x.  2 root root    6  7 28 16:45 ダウンロード

テキストファイルの行数を確認する

[root@localhost ~]# cat /tmp/txt | awk 'END{print NR}'
12

テキストファイルに行数を付けて表示

[root@localhost ~]# cat /tmp/txt | awk '{print NR,$0}'
1 合計 12
2 -rw-------.  1 root root 1898  7 28 23:20 anaconda-ks.cfg
3 -rw-r--r--.  1 root root 1926  7 28 14:23 initial-setup-ks.cfg
4 drwxr-xr-x. 10 root root 4096  7 28 15:35 rtl8188eu
5 drwxr-xr-x.  2 root root    6  7 28 16:45 ダウンロード
6 drwxr-xr-x.  2 root root    6  7 28 16:45 テンプレート
7 drwxr-xr-x.  2 root root    6  7 28 16:45 デスクトップ
8 drwxr-xr-x.  2 root root    6  7 28 16:45 ドキュメント
9 drwxr-xr-x.  2 root root    6  7 28 16:45 ビデオ
10 drwxr-xr-x.  2 root root    6  7 28 16:45 音楽
11 drwxr-xr-x.  2 root root    6  7 28 16:45 画像
12 drwxr-xr-x.  2 root root    6  7 28 16:45 公開

最後に

awkでのテキスト操作はいろいろな書き方がありますのでこれ以外にも表現の仕方があると思います。
データ成型を行うシェルプログラムを行うときには非常に強い味方になります。
私自身、awkを含むテキスト処理をもっと学習しどんどん紹介(アウトプット)していきます。

2
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
2
1