56
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

bashでCSVを読み込み1行ずつループ処理する

Last updated at Posted at 2018-05-27

構文

while read ブロック変数; do
  処理
done < 読み込むファイル

使用例

以下の様なCSVがあったとする

sample.csv
name,number,email
name1,0001,example1.com
name2,0002,example2.com
name3,0003,example3.com

このCSVを1行ずつループ処理する。
ここでは順番にname列とnumber列を標準出力させる。

sample.sh
#!/bin/bash

while read row; do
  column1=`echo ${row} | cut -d , -f 1`
  column2=`echo ${row} | cut -d , -f 2`

  echo "${column1}の番号は${column2}です。"
done < sample.csv
$ ./sample.sh

=> nameの番号はnumberです。
=> name1の番号は0001です。
=> name2の番号は0002です。
=> name3の番号は0003です。
56
54
1

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
56
54

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?