LoginSignup
0
0

シェル(bash)でテキスト形式のパラメータファイルから文字を抽出する方法

Last updated at Posted at 2024-05-20

環境

  • OS : CentOS Linux release 8.5.2111
  • bash : GNU bash, バージョン 4.4.20(1)-release (x86_64-redhat-linux-gnu)
実行結果
[root@centos85 work]# cat /etc/redhat-release
CentOS Linux release 8.5.2111
[root@centos85 work]# bash --version
GNU bash, バージョン 4.4.20(1)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2016 Free Software Foundation, Inc.
ライセンス GPLv3+: GNU GPL バージョン 3 またはそれ以降 <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
[root@centos85 work]#

テキスト形式のパラメータファイル

以降では以下のテキスト形式のパラメータファイル(param.txt)に対して操作します。

param.txt
user01,host01,file01
user02,host02,file02
user03,host03,file03
user04,host04,file04
user05,host05,file05
user06,host06,file06
user07,host07,file07
user08,host08,file08
user09,host09,file09
user10,host10,file10

1. 行の抽出(grepコマンド)

「user05」の行を抽出する場合
grep user05 param.txt
cat param.txt | grep user05

実行結果
[root@centos85 work]# grep user05 param.txt
user05,host05,file05
[root@centos85 work]# cat param.txt | grep user05
user05,host05,file05
[root@centos85 work]#

2. 列の抽出

2列目を抽出
cut -d , -f 2 param.txt
cat param.txt | cut -d , -f 2

実行結果
[root@centos85 work]# cut -d , -f 2 param.txt
host01
host02
host03
host04
host05
host06
host07
host08
host09
host10
[root@centos85 work]# cat param.txt | cut -d , -f 2
host01
host02
host03
host04
host05
host06
host07
host08
host09
host10
[root@centos85 work]#

2列目と3列目を抽出
cut -d , -f 2-3 param.txt
cat param.txt | cut -d , -f 2-3

実行結果
[root@centos85 work]# cut -d , -f 2-3 param.txt
host01,file01
host02,file02
host03,file03
host04,file04
host05,file05
host06,file06
host07,file07
host08,file08
host09,file09
host10,file10
[root@centos85 work]# cat param.txt | cut -d , -f 2-3
host01,file01
host02,file02
host03,file03
host04,file04
host05,file05
host06,file06
host07,file07
host08,file08
host09,file09
host10,file10
[root@centos85 work]#

1列目と3列目を抽出
cut -d , -f 1,3 param.txt
cat param.txt | cut -d , -f 1,3

実行結果
[root@centos85 work]# cut -d , -f 1,3 param.txt
user01,file01
user02,file02
user03,file03
user04,file04
user05,file05
user06,file06
user07,file07
user08,file08
user09,file09
user10,file10
[root@centos85 work]# cat param.txt | cut -d , -f 1,3
user01,file01
user02,file02
user03,file03
user04,file04
user05,file05
user06,file06
user07,file07
user08,file08
user09,file09
user10,file10
[root@centos85 work]#

3. 行と列を指定して抽出

user05の3項目目を抽出
grep user05 param.txt | cut -d , -f 3
cat param.txt | grep user05 | cut -d , -f 3

実行結果
[root@centos85 work]# grep user05 param.txt | cut -d , -f 3
file05
[root@centos85 work]# cat param.txt | grep user05 | cut -d , -f 3
file05
[root@centos85 work]#

user05の2項目目と3項目目を抽出
grep user05 param.txt | cut -d , -f 2-3
cat param.txt | grep user05 | cut -d , -f 2-3

実行結果
[root@centos85 work]# grep user05 param.txt | cut -d , -f 2-3
host05,file05
[root@centos85 work]# cat param.txt | grep user05 | cut -d , -f 2-3
host05,file05
[root@centos85 work]#

user05の2項目目と3項目目を抽出(grepとsed)
cat param.txt | grep user05 | sed -r 's':'('.*')user05,'::
cat param.txt | grep user05 | sed -r s/\(.*\)user05,//

実行結果
[root@centos85 work]# cat param.txt | grep user05 | sed -r 's':'('.*')user05,'::
host05,file05
[root@centos85 work]# cat param.txt | grep user05 | sed -r s/\(.*\)user05,//
host05,file05
[root@centos85 work]#

その他

上記の例ではテキスト形式のパラメータファイル(param.txt)にコメント行や空行がありませんでしたが、コメントが入っている場合はコメントを取り除く必要があります。

コメントを除くコマンド例
cat param.txt | grep -v '^\s*$' | grep -v '^\s*#'


以上

0
0
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
0
0