1
0

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 5 years have passed since last update.

bashでcsvカラムに条件を指定し行数をカウント

1
Posted at

以下のCSVがあった場合

sample.csv
id,name,status,email
1,name1,1,example1.com
2,name2,2,example2.com
3,name3,1,example3.com
4,name3,,example4.com

status列の値が1の行数をカウント

count.sh
# !/bin/bash

CSV_FILE=$1

REGISTRATION_COUNT=`cat $CSV_FILE | awk -F , '$3 == 1' | wc -l`
echo $REGISTRATION_COUNT

実行

$ ./count.sh
=> 2

status列の値が存在する行をカウント

count.sh
# !/bin/bash

CSV_FILE=$1

REGISTRATION_COUNT=`cat $CSV_FILE | awk -F , '$3 != NULL' | wc -l`
echo $REGISTRATION_COUNT

実行

$ ./count.sh
=> 3
1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?