Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
39
Help us understand the problem. What is going on with this article?
@SoarTec-lab

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

More than 1 year has passed since last update.

構文

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です。
39
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
SoarTec-lab
freelance software developer/rails contributor https://contributors.rubyonrails.org/contributors/soartec-lab/commits
classi
学校の先生・生徒・保護者向けのB2B2Cの学習支援Webサービス「Classi(クラッシー)」 を開発・運営している会社です。

Comments

No comments
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account Login
39
Help us understand the problem. What is going on with this article?